Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Only able to print once?

Only able to print once?

Scheduled Pinned Locked Moved C#
graphicshelpwinformsquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    melanieab
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • M melanieab

      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

      L Offline
      L Offline
      LongRange Shooter
      wrote on last edited by
      #2

      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!
      

      }

      M 1 Reply Last reply
      0
      • L LongRange Shooter

        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!
        

        }

        M Offline
        M Offline
        melanieab
        wrote on last edited by
        #3

        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 line PrintDocument pd = new PrintDocument();. Any ideas? Mel :confused:

        M 1 Reply Last reply
        0
        • M melanieab

          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 line PrintDocument pd = new PrintDocument();. Any ideas? Mel :confused:

          M Offline
          M Offline
          melanieab
          wrote on last edited by
          #4

          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:

          M 1 Reply Last reply
          0
          • M melanieab

            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:

            M Offline
            M Offline
            Martin 0
            wrote on last edited by
            #5

            Thank you! Just needed it.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups