Get the image of a control or form, or a form's client area in C#

A control's DrawToBitmap method makes the control draw itself into a bitmap. A Form is a type of control so you can use its DrawToBitmap method, too.

The GetControlImage method shown in the following code makes a bitmap big enough to hold a control's image and then gets the image.


// Return a Bitmap holding an image of the control.
private Bitmap GetControlImage(Control ctl)
{
Bitmap bm = new Bitmap(ctl.Width, ctl.Height);
ctl.DrawToBitmap(bm, new Rectangle(0, 0, ctl.Width, ctl.Height));
return bm;
}

The GetFormImageWithoutBorders method shown in the following code gets a form's image without its borders and title bar.

// Return the form's image without its borders and decorations.
private Bitmap GetFormImageWithoutBorders(Form frm)
{
// Get the form's whole image.
using (Bitmap whole_form = GetControlImage(frm))
{
// See how far the form's upper left corner is
// from the upper left corner of its client area.
Point origin = frm.PointToScreen(new Point(0, 0));
int dx = origin.X - frm.Left;
int dy = origin.Y - frm.Top;

// Copy the client area into a new Bitmap.
int wid = frm.ClientSize.Width;
int hgt = frm.ClientSize.Height;
Bitmap bm = new Bitmap(wid, hgt);
using (Graphics gr = Graphics.FromImage(bm))
{
gr.DrawImage(whole_form, 0, 0,
new Rectangle(dx, dy, wid, hgt),
GraphicsUnit.Pixel);
}
return bm;
}
}

This method calls GetControlImage to get an image of the full form. It then uses the form's PointToScreen method to see where the upper left corner of its client area is in screen coordinates. The difference between that point and the form's upper left corner tells you how far inset the form's client area is from the corner of its borders.

The code makes a new bitmap that is the right size to hold the form's client area and then copies the client part of the form's whole image into the new bitmap.

This example does one other interesting thing. If you click the Page 1 or Page 2 buttons, it displays an image of the corresponding page in its TabControl. Unfortunately if a tab page isn't visible, then its DrawToBitmap method may not be able to draw the page.

To solve this problem, the code first selects the page, then gets its image, and then restores the TabControl's originally selected page. There may be a flicker when the page switches but at least this seems to work.

private void btnPage2_Click(object sender, EventArgs e)
{
int selected = tabControl1.SelectedIndex;
tabControl1.SelectedIndex = 1;

ShowControlImage(tabPage2);

tabControl1.SelectedIndex = selected;
}

Note that DrawToBitmap may fail for other kinds of controls (notably the RichTextBox and ActiveX controls) and for controls that are hidden.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.