?
Solved

Print Preview and printing a windows form

Posted on 2011-04-25
29
Medium Priority
?
401 Views
Last Modified: 2012-05-11
C# VS 2008
Windows forms

I have the following code which has a listView on it, Im looking to take a screenshot of the window (or another idea if there is one) of the form and then when the print button is clicked have a print preview dialog appear which I currently have but nothing shows up. So any help how to to capture the screen and have it show a preview would be great thanks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FinalProject
{
    public partial class frmReceipt : Form
    {
        public frmReceipt()
        {
            InitializeComponent();
        }
    
       [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
        private Bitmap memoryImage;
        private void CaptureScreen()
        {
           Graphics mygraphics = this.CreateGraphics();
           Size s = this.Size;
           memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
           Graphics memoryGraphics = Graphics.FromImage(memoryImage);
           IntPtr dc1 = mygraphics.GetHdc();
           IntPtr dc2 = memoryGraphics.GetHdc();
           BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
           mygraphics.ReleaseHdc(dc1);
           memoryGraphics.ReleaseHdc(dc2);
        }
        private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
           e.Graphics.DrawImage(memoryImage, 0, 0);
        }
        private void printButton_Click(System.Object sender, System.EventArgs e)
        {
           CaptureScreen();
           printPreviewDialog1.ShowDialog();
           //printDocument1.Print();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
            frmMain frm = new frmMain();
            frm.Show();
        }

        }

Open in new window

0
Comment
Question by:linkcube1
  • 18
  • 11
29 Comments
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 2000 total points
ID: 35461642
Hi, maybe you are missing this line:

private void printButton_Click(System.Object sender, System.EventArgs e)
{
    printPreviewDialog1.Document = printDocument1;

    CaptureScreen();
    printPreviewDialog1.ShowDialog();
    //printDocument1.Print();
}
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35461655
Or from the designer set the Document property of printPreviewDialog1 to printDocument1
0
 

Author Comment

by:linkcube1
ID: 35461694
Preview still shows up blank  rtr
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35461754
mmm that is strange, it work for me, can you test this project?:
http://dl.dropbox.com/u/13237304/PrintFormTest.zip
0
 

Author Comment

by:linkcube1
ID: 35462645
wow.... runs fine in VS 2010 Maybe if I recreate the form and test it that will work.
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35462697
What VS version are you using? .net framework version?
0
 

Author Comment

by:linkcube1
ID: 35462715
2008 3.5 network
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35462826
mmm, can you test this exe?
http://dl.dropbox.com/u/13237304/PrintFormTestExe.zip

I set it to use the framework version to 3.5, in my system runs fine, like if I use the 4.0 framework.
0
 

Author Comment

by:linkcube1
ID: 35462846
yup that .exe works fine
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35462856
then your VS is interfering with something I think...
0
 

Author Comment

by:linkcube1
ID: 35462885
hmm.. any suggestions?
0
 
LVL 17

Assisted Solution

by:Carlos Villegas
Carlos Villegas earned 2000 total points
ID: 35462990
Please update your CaptureScreen function with this:
private void CaptureScreen()
{
    using (Graphics mygraphics = this.CreateGraphics())
    {
        Size s = this.Size;
        memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
        IntPtr dc1 = mygraphics.GetHdc();
        IntPtr dc2 = memoryGraphics.GetHdc();
        BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
        mygraphics.ReleaseHdc(dc1);
        memoryGraphics.ReleaseHdc(dc2);
    }
}

Open in new window

0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35463039
Hello! better try this, it not requires the BitBlt function from gdi32.dll:
private void CaptureScreen()
{
    Graphics myGraphics = this.CreateGraphics();
    Size s = this.Size;
    memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
    Graphics memoryGraphics = Graphics.FromImage(memoryImage);
    memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
}

Open in new window

Sure this fix your problem.
0
 

Author Comment

by:linkcube1
ID: 35463047
Still nothing..
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35463061
I forgot to dispose the Graphics object:
private void CaptureScreen()
{
    using (Graphics myGraphics = this.CreateGraphics())
    {
        Size s = this.Size;
        memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
        memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
    }
}

Open in new window

0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35463116
What if you compile your application, then run manually the exe from the bin/debug directory? it run fine? you know what I meant?
0
 

Author Comment

by:linkcube1
ID: 35463117
Still not showing anything... I hope this doesn't require a new VS install
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35463123
Did you see my last comment?
0
 

Author Comment

by:linkcube1
ID: 35463148
no still doesn't grab anything, want me to send the program I made and see if it works on yours?
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35463154
yes please
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35463158
send to my email, yv989c at gmail dot com
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35463171
send it in a zip or rar folder
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35463192
if your email provider refuses to send it, put a password on it
0
 

Author Comment

by:linkcube1
ID: 35463822
ok sent.
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35464551
Hi, post your file here, use the File link below your comment text area.
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35466850
Hi buddy, you forgot to set the Print event of your printDocument1... that is the problem...
0
 
LVL 17

Assisted Solution

by:Carlos Villegas
Carlos Villegas earned 2000 total points
ID: 35466867
Sorry is the PrintPage event, you got it?
0
 

Author Comment

by:linkcube1
ID: 35467340
yup makes sense thanks!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question