WinForms Screen Capture

For a recent project we’ve been working on we needed to get a screen shot of a form and it’s contents. After much checking on the net for some sample code, all of which used GDI32 and User32 calls, we gave up looking and decided to investigate ourselves.

In the end it was so simple and clean that we just can’t believe that this isn’t documented that well anywhere on the web. The code below is an extract of our code that takes the forms position on the screen, creates a bitmap of the contents of the screen from co-ordinates passed to it and then saves the resultant screen shot to a file

using (Bitmap bitmap = new Bitmap(ParentForm.Size.Width, ParentForm.Size.Height))
using (Graphics g = Graphics.FromImage(bitmap))
g.CopyFromScreen(new Point(ParentForm.DesktopLocation.X, ParentForm.DesktopLocation.Y), new Point(0, 0), ParentForm.Size);

bitmap.Save(@”C:\test.jpg”, ImageFormat.Jpeg);

Enjoy šŸ™‚

Tagged . Bookmark the permalink.

Leave a Reply