Exporting Windows Form to an Image

Windows Forms has a lot of controls like datagridview, charting controls and others that make it useful to display complex reports directly on a windows form. Sometimes it is desirable to print this windows form as an image or a pdf for the purpose of reporting.

This can be done pretty easily using the following code –

Rectangle formRect = this.Bounds;
bitmap = new Bitmap(formRect.Width, formRect.Height);
graphic = Graphics.FromImage(bitmap);
graphic.CopyFromScreen(formRect.Location, Point.Empty, formRect.Size);

In case you have a situation where the panel to be printed is bigger than the form, you might have to do this twice, once with the scroll on top, once with the panel scrolled down (assuming the entire panel can be covered in two scrolls – else more runs will be required).

You can modify this to any extent – for instance instead of using the bounds of the form, you might want to take bounds of only the panel for taking the print – either ways, this will allow you to export the form/panel to an image for reporting.

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax