Hi,
I have a tabpage with a print button. This button tries to do a print
screen, and it works fine the first time, but if I try to press print again,
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 going about print-screening by first capturing a bitmap of the screen,
saving it, and then calling for the print. Here is the code that I have when
the print button is pressed:
(CurrentPage is declared as a private Bitmap)
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(Prin tCurrentPage);
pd.Print();
}
pd.Dispose();
The PrintCurrentPage event handler follows:
private void PrintCurrentPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
CurrentPage = new Bitmap(@"C:\sCurrentPage.bmp");}
e.Graphics.DrawImage(CurrentPage, 0, 0);
}
I'm guessing there's a problem using Print Document pd more than once. Any
suggestions on what to do?
Thanks,
Mel