Only able to print once?

G

Guest

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 commented out the
pd.Print(); and it worked fine. No exceptions, and the bitmaps saved fine
each time. 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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Printing long form in C# 2005 1
Printing a Panel 4
Printing problem 4
PrintPreview margins not displaying 2

Top