1

I have created a Windows Application in Visual Studio 2005. In this application, the user is able to export the current active form to a PDF file.

The problem is how do I save it with the values of textboxes? When I leave them empty, there's no problem. Then I give some input and get output in textboxes, I click export, and I get a message "A generic error occurred in GDI+". And in Output window of Visual Studio:

A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll

Here's my code:

For capturing active screen:

try
{
    Rectangle bounds = this.Bounds;
    using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
    {
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
        }
        bitmap.Save("C://Rectangle.bmp", ImageFormat.Bmp);
    }
}
catch(Exception e)
{
    MessageBox.Show(e.Message.ToString());
}

For exporting to pdf:

captureScreen();

PdfDocument doc = new PdfDocument();

PdfPage oPage = new PdfPage();

doc.Pages.Add(oPage);
oPage.Rotate = 90;
XGraphics xgr = XGraphics.FromPdfPage(oPage);
XImage img = XImage.FromFile(@"C://Rectangle.bmp");

xgr.DrawImage(img, 0, 0);

doc.Save("C://RectangleDocument.pdf");
doc.Close();

Please help me on exporting the form to pdf including values of textboxes.

Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
FJPoort
  • 225
  • 2
  • 7
  • 16
  • Try to use [function of capturing active window](http://stackoverflow.com/questions/1163761/c-sharp-capture-screenshot-of-active-window/9087955#9087955) described by me. – Ivan Kochurkin Sep 21 '12 at 14:38
  • @KvanTTT I have to go home now, and will only be able to work on it after the weekend again.. I'll let you guys know whether this solved the case or not! – FJPoort Sep 21 '12 at 14:57

1 Answers1

1

I am assuming this exception is thrown when you try to call CopyFromScreen? I have written very similar code before with no problems, but there are a few minor differences.

The first thing I see is that the Bounds property returns a point as a relative position, but the CopyFromScreen method expects the point as an absolute position. You probably want to call this.PointToScreen to get the absolute screen position of the point before calling CopyFromScreen.

Second, it should "just work" but just for my own sanity I usually use CopyPixelOption.SourceCopy in the CopyFromScreen call.

I'm not sure about the implementation of CopyFromScreen and Bitmap.Save, but you might want to move the Save call so it happens before the graphics object is disposed. This is probably not necessary, but if there's any sort of delayed computation going on that could cause you problems.

Tim Copenhaver
  • 3,282
  • 13
  • 18
  • Where should I place this CopyPixelOption.SourceCopy? The first block of code in my question is my captureScreen() method. + I moved the Save call so it happens before the graphics object is disposed. No error anymore, but pdf shows a black screen. – FJPoort Sep 21 '12 at 14:41
  • CopyPixelOption.SourceCopy should be added as the last parameter for the CopyFromScreen method. That should fix the black screen problem. – Tim Copenhaver Sep 21 '12 at 15:56
  • Thanks Tim! To add the CopyFormPixelOperation solved my problem! – FJPoort Sep 24 '12 at 07:59