Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a screen capture of a .Net WinForms control programmatically?

People also ask

How do I take a screenshot of a form?

In this case % is used to press Alt and {PRTSC} obviously to press the Print Screen button on your keyboard. Hope this helps someone!

What is UserControl in WinForms?

A UserControl is a collection of controls placed together to be used in a certain way. For example you can place a GroupBox that contains Textbox's, Checkboxes, etc. This is useful when you have to place the same group of controls on/in multiple forms or tabs.

How do I turn off WinForms full screen?

The only thing you can do is completely disable resizing your window. In WinForms, you do that by setting the FormBorderStyle property to one of the following: FormBorderStyle. FixedSingle , FormBorderStyle. Fixed3D , or FormBorderStyle.


There's a method on every control called DrawToBitmap. You don't need to p/invoke to do this.

Control c = new TextBox();
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(c.Width, c.Height);
c.DrawToBitmap(bmp, c.ClientRectangle);

You can get a picture of a .NET control programmatically pretty easily using the DrawToBitmap method of the Control class starting in .NET 2.0

Here is a sample in VB

    Dim formImage As New Bitmap("C:\File.bmp")
    Me.DrawToBitmap(formImage, Me.Bounds)

And here it is in C#:

 Bitmap formImage = New Bitmap("C:\File.bmp")
 this.DrawToBitmap(formImage, this.Bounds)

Control.DrawToBitmap will let you draw most controls to a bitmap. This does not work with RichTextBox and some others.

If you want to capture these, or a control that has one of them, then you need to do PInvoke like described in this CodeProject article: Image Capture

Take care that some of these methods will capture whatever is on the screen, so if you have another window covering your control you will get that instead.


For WinForms controls that support it, there is a method in the System.Windows.Forms.Control class:

public void DrawToBitmap(Bitmap bitmap, Rectangle targetBounds);

This does not work with all controls, however. Third party component vendors have more comprehensive solutions.