Only able to print once?
-
Hi, I have a form with tab pages on it, and I'd like to print each page whenever a button is pressed (all the tabpages share the same print button - I just give it a different parent when another tab is pressed). But I can only print once. When the print button is pressed the second time, I get error An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in system.drawing.dll Additional information: A generic error occurred in GDI+. I'm not sure what to do here. My code is below. I also tried giving the PrintDocument a different name each time, and it still isn't happy.
private void printClick(object sender, System.EventArgs e) { Graphics currentTab = this.CreateGraphics(); Size s = this.Size; Bitmap memoryImage = new Bitmap(s.Width - 10, s.Height - 36, currentTab); Graphics memoryGraphics = Graphics.FromImage(memoryImage); IntPtr dc1 = currentTab.GetHdc(); IntPtr dc2 = memoryGraphics.GetHdc(); BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 2, 2, 13369376); CurrentPage = memoryImage; currentTab.ReleaseHdc(dc1); memoryGraphics.ReleaseHdc(dc2); CurrentPage.Save("sCurrentPage.bmp",System.Drawing.Imaging.ImageFormat.Bmp); PrintDocument pd = new PrintDocument(); PageSetupDialog pg = new PageSetupDialog(); printDialog1.Document = pd; pg.Document = pd; pg.PageSettings.Landscape = true; DialogResult result = printDialog1.ShowDialog(); if (result==DialogResult.OK) {pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintCurrentPage); pd.Print();} pd.Dispose(); }
Thanks for any help!!! Mel -
Hi, I have a form with tab pages on it, and I'd like to print each page whenever a button is pressed (all the tabpages share the same print button - I just give it a different parent when another tab is pressed). But I can only print once. When the print button is pressed the second time, I get error An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in system.drawing.dll Additional information: A generic error occurred in GDI+. I'm not sure what to do here. My code is below. I also tried giving the PrintDocument a different name each time, and it still isn't happy.
private void printClick(object sender, System.EventArgs e) { Graphics currentTab = this.CreateGraphics(); Size s = this.Size; Bitmap memoryImage = new Bitmap(s.Width - 10, s.Height - 36, currentTab); Graphics memoryGraphics = Graphics.FromImage(memoryImage); IntPtr dc1 = currentTab.GetHdc(); IntPtr dc2 = memoryGraphics.GetHdc(); BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 2, 2, 13369376); CurrentPage = memoryImage; currentTab.ReleaseHdc(dc1); memoryGraphics.ReleaseHdc(dc2); CurrentPage.Save("sCurrentPage.bmp",System.Drawing.Imaging.ImageFormat.Bmp); PrintDocument pd = new PrintDocument(); PageSetupDialog pg = new PageSetupDialog(); printDialog1.Document = pd; pg.Document = pd; pg.PageSettings.Landscape = true; DialogResult result = printDialog1.ShowDialog(); if (result==DialogResult.OK) {pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintCurrentPage); pd.Print();} pd.Dispose(); }
Thanks for any help!!! MelHaving a GDI exception occur tends to make me think that your problem is not with the printing, but rather in creating the bitmap. Where in your code does the stack trace point at? Does the bitmap get saved and have you looked at it to make sure it is the page you expected to print? Two other things: You can save yourself one object creation and a little bit of GC stress by changing the last four lines of code to this:
if ( printDialog1.ShowDialog() == DialogResult.OK ) { pd.PrintPage += ..... } // pd.Dispose(); // our MS consultants say never do this!
}
-
Having a GDI exception occur tends to make me think that your problem is not with the printing, but rather in creating the bitmap. Where in your code does the stack trace point at? Does the bitmap get saved and have you looked at it to make sure it is the page you expected to print? Two other things: You can save yourself one object creation and a little bit of GC stress by changing the last four lines of code to this:
if ( printDialog1.ShowDialog() == DialogResult.OK ) { pd.PrintPage += ..... } // pd.Dispose(); // our MS consultants say never do this!
}
I commented out just the
pd.Print();
and it worked fine. No exceptions, correct bitmap saved each time. I'm assuming the stack trace is the green arrow/highlight? If so, it points at the linePrintDocument pd = new PrintDocument();
. Any ideas? Mel :confused: -
I commented out just the
pd.Print();
and it worked fine. No exceptions, correct bitmap saved each time. I'm assuming the stack trace is the green arrow/highlight? If so, it points at the linePrintDocument pd = new PrintDocument();
. Any ideas? Mel :confused:In case anyone needs it, the problem is in this line:
pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintCurrentPage);
PrintCurrentPage looked like this:private void PrintCurrentPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { if (CurrentPage != null){CurrentPage = new Bitmap(@"C:\sCurrentPage.bmp");} }
I found that "When a bitmap image is loaded from a file the file remains locked open. This may be the root of the problem. To overcome the file locking open the file on a stream, read the image from the stream and explicitly close the stream." So I changed PrintCurrentPage to look like this:if (CurrentPage != null) { FileStream fs = File.Open(@"C:\sCurrentPage.bmp", FileMode.Open, FileAccess.Read); Bitmap bm = (Bitmap)Bitmap.FromStream(fs); CurrentPage = bm; e.Graphics.DrawImage(CurrentPage, 0, 0); fs.Close(); bm.Dispose(); }
And it all worked fine. Cheers, Mel :cool: -
In case anyone needs it, the problem is in this line:
pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintCurrentPage);
PrintCurrentPage looked like this:private void PrintCurrentPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { if (CurrentPage != null){CurrentPage = new Bitmap(@"C:\sCurrentPage.bmp");} }
I found that "When a bitmap image is loaded from a file the file remains locked open. This may be the root of the problem. To overcome the file locking open the file on a stream, read the image from the stream and explicitly close the stream." So I changed PrintCurrentPage to look like this:if (CurrentPage != null) { FileStream fs = File.Open(@"C:\sCurrentPage.bmp", FileMode.Open, FileAccess.Read); Bitmap bm = (Bitmap)Bitmap.FromStream(fs); CurrentPage = bm; e.Graphics.DrawImage(CurrentPage, 0, 0); fs.Close(); bm.Dispose(); }
And it all worked fine. Cheers, Mel :cool: