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.