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);
    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 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),
                GraphicsUnit.Pixel);
        }
        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;

    ShowControlImage(tabPage2);
    
    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.

One Response 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

Leave a Reply

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