Screen Dumps Made Easy with DrawToBitmap

Posted Wednesday, January 18, 2006 8:28 AM by C-Dog's .NET Tip of the Day

In Windows Form 2.0, they have made it easier to do a screen dump of a Windows Form. Now, just about any control along with anything that inherits from Form has a DrawToBitmap method. This method allows you to easily save a copy of what is on the current form to an image.

// declare a new bitmap
Bitmap myBitmap = new Bitmap(this.Width, this.Height);

// get the bitmap
this.DrawToBitmap(myBitmap, this.ClientRectangle);

// save the bitmap
myBitmap.Save("c:\screendump.bmp");

Screendump, huh huh. Ok, basically this code is pretty simple. You create a bitmap that set to the width and height of the form. In this case the use of this is assuming that you are executing it from some method inside of a form class somewhere. The DrawToBitmap method grabs the bitmap and then you just save it do disk or whatever.

There are a few things to note about it, it doesn't work with Ink controls for Tablet PCs (not like any of you are doing that). It also doesn't work quite right with the RichTextBox (it only shows the border). Lastly, it doesn't work with ActiveX controls.

Read the complete post at http://www.dotnettipoftheday.com/blog.aspx?id=236