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

Get the image of a control

This example shows how to get the image of a control. Because a form is a type of control, the same technique lets you get a form’s image.

A control’s DrawToBitmap method makes the control draw itself into a bitmap. The GetControlImage method shown in the following uses DrawToBitmap to return a bitmap holding a control’s image.

// Return a Bitmap holding an image of the control.
private Bitmap GetControlImage(Control ctl)
    Bitmap bm = new Bitmap(ctl.Width, ctl.Height);
        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 or 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),
        return bm;

This method calls GetControlImage to get an image of the entire 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 the form’s client area is inset 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.

The example program does one other interesting thing. If you click the Page 1 or Page 2 buttons, it captures an image of the corresponding TabControl page. The following code shows how the program captures page 2.

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

    tabControl1.SelectedIndex = selected;

Unfortunately if a tab page isn’t visible, then its DrawToBitmap method may not be able to draw the page because it may not be available in off-screen memory. To solve this problem, the code saves the currently selected page index, selects the desired page, and then calls ShowControlImage to get the control’s image and display it in a dialog. When you close the dialog, the code restores the TabControl‘s originally selected page.

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

Download Example   Follow me on Twitter   RSS feed

This entry was posted in graphics, image processing and tagged , , , , , , , , , , , . Bookmark the permalink.

6 Responses to Get the image of a control or form, or a form’s client area in C#

  1. Pingback: Print a form or control in C# - C# HelperC# Helper

  2. Marlene says:

    Dear Mr. Stephens – Thank you for this example; it has given me a start toward what I want to accomplish. Perhaps you can help me complete it. I am trying to print a calendar control similar to what one sees in Outlook. So imagine I have a calendar page with Sunday on the far left and Saturday on the far right and weekdays in between. With this code, for some reason I am only capturing Tues, Wed and Thurs. I don’t care about Sunday and Saturday (they are always blank) but I do want Monday and Friday. The way this calendar control works (it’s the DevExpress scheduler control), is if I maximize the form it allows me to see more of each box; that is, more lines going down and more text going across. So I’d like to capture the image in this maximized state. Does this make sense and do you have any words of wisdom to help me make the capture image larger? Thanks again for posting a topic on this!

    • RodStephens says:

      Unfortunately Windows doesn’t always get the whole picture if the control it too big. You could try capturing the whole screen. Maybe it will let you see the part that you want to capture.

      If that doesn’t work, you can send me your project and I’ll take a look at it. but I’m not sure I can help.

      • Intersektionaalinen feministi says:

        Set control size temporarily to Preferred.Size and DrawToBitmap(). Then restore original size of control.

  3. Pingback: Let the user save an image of a smooth curve in C# - C# HelperC# Helper

  4. Pingback: Save a RichTextBox image in C# - C# HelperC# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.