Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Visual-Studio PDF
Hi,
 
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. Next problem, how do I save it with the values of textboxes? When I leave them epmty, there's no problem and image en pdf are saved. 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 visual studio output window: 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();
 
As I said, the exporting with empty textboxes goes fine, but when the have a value, I get the error messages.
Posted 21-Sep-12 5:14am
Frans Jan2.1K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I haven't tried this myself, but I have a question. If I recall correctly, printing a Windows.Form to a printer will by default result in the form printing without values as well, so the solution may be to use some of the techniques used to achieve that as well.
 
I found this article (I think this is one I referenced a while back for forms printing, but I'm not entirely sure anymore): http://msdn.microsoft.com/en-us/magazine/cc188767.aspx[^]
 
The best answer I could find that may help you is this one See Solution 1 from Sergey for the concept: Print Windows form in C#.Net[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi all,
 
I promised to get back to this question, and I found myself an answer:
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size, CopyPixelOperation.SourceCopy);
 
I just had to add the CopyPixelOperation.SourceCopy as last parameter to the CopyFromScreen method.
 
So the working code looks like this:
 
private void captureScreen()
        {
            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, CopyPixelOperation.SourceCopy);
                    }
                    bitmap.Save("C://Rectangle.bmp", ImageFormat.Bmp);
                }
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message.ToString());
            }
 

        }
 
private void btnMenuExport_Click(object sender, EventArgs e)
        {
            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();
        }
 
  Permalink  
v2
Comments
Member 11148509 at 14-Oct-14 6:11am
   
Thank you for posting the code....i have tried the code was getting errors in button click ..and what all namespaces did you add???

Thank you
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I found a better solution to this. Please check my article:
Convert a Windows Form to PDF[^]
  Permalink  
Comments
Member 11148509 at 14-Oct-14 6:10am
   
Thank you for posting the code....i have tried the code was getting errors in button click ..and what all namespaces did you add???
 
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Marcin Kozub 310
1 OriginalGriff 248
2 Sergey Alexandrovich Kryukov 215
3 Richard MacCutchan 202
4 Praneet Nadkar 197
0 OriginalGriff 8,048
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 27 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100