33

How do you programmatically obtain a picture of a .Net control?

Alex Wiese
  • 8,142
  • 6
  • 42
  • 71
user34787
  • 353
  • 1
  • 3
  • 7
  • This is a bit too vague, try to explain what are you trying to do. No wander the down votes. – Pop Catalin Nov 05 '08 at 18:22
  • 1
    No wander? I think the meaning is perfectly clear. He wants to get a bitmap representation of a control. –  Nov 05 '08 at 18:24
  • 4
    I agree with Will. It's a simple and clear question. – Bogdan Nov 05 '08 at 18:33
  • 1
    I hope you don't mind that I reworded the question a bit. It's a good question and a good answer by Will. – raven Nov 05 '08 at 18:59

7 Answers7

49

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);
7

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)
sabisabi
  • 1,501
  • 5
  • 22
  • 40
Joey
  • 2,901
  • 21
  • 22
5

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.

Jimi
  • 29,621
  • 8
  • 43
  • 61
Hallgrim
  • 15,143
  • 10
  • 46
  • 54
  • For WebBrowser, I just cast to Control and call it there. I've done it and know it works. Not sure about RichTextBox – Nick Nov 05 '08 at 18:47
3

This is how to do it for an entire Form, not just the Client area (which doesn't have the title bar and other dressing)

        Rectangle r = this.Bounds;
        r.Offset(-r.X,-r.Y);
        Bitmap bitmap = new Bitmap(r.Width,r.Height);
        this.DrawToBitmap(bitmap, r);
        Clipboard.SetImage(bitmap);
Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
3

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.

Alan
  • 6,501
  • 1
  • 28
  • 24
1

if it's not on the control you're trying to do, you can usually cast it to the base Control class and call the DrawToBitmap method there.

Nick
  • 9,113
  • 4
  • 25
  • 29
1
Panel1.Dock = DockStyle.None ' If Panel Dockstyle is in Fill mode
Panel1.Width = 5000  ' Original Size without scrollbar
Panel1.Height = 5000 ' Original Size without scrollbar

Dim bmp As New Bitmap(Me.Panel1.Width, Me.Panel1.Height)
Me.Panel1.DrawToBitmap(bmp, New Rectangle(0, 0, Me.Panel1.Width, Me.Panel1.Height))
'Me.Panel1.DrawToBitmap(bmp, Panel1.ClientRectangle)
bmp.Save("C:\panel.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

Panel1.Dock = DockStyle.Fill

Note: Its working fine

GSerg
  • 76,472
  • 17
  • 159
  • 346