Click here to Skip to main content
15,881,793 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
See more:
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. <br />
A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll


Here's my code:

For capturing active screen:
C#
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:
C#
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

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[^]
 
Share this answer
 
Hi all,

I promised to get back to this question, and I found myself an answer:
C#
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:

C#
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();
        }
 
Share this answer
 
v2
Comments
Member 11148509 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
I found a better solution to this. Please check my article:
Convert a Windows Form to PDF[^]
 
Share this answer
 
Comments
Member 11148509 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900