Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleGet the image of a control or form, or a form's client area in Visual Basic .NET
DescriptionThis example shows how to get the image of a control or form, or a form's client area in Visual Basic .NET.
Keywordsgraphics, form image, control image, get form image, get control image
CategoriesGraphics
 
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 Function GetControlImage(ByVal ctl As Control) As _
    Bitmap
    Dim bm As New Bitmap(ctl.Width, ctl.Height)
    ctl.DrawToBitmap(bm, New Rectangle(0, 0, ctl.Width, _
        ctl.Height))
    Return bm
End Function
 
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 Function GetFormImageWithoutBorders(ByVal frm As _
    Form) As Bitmap
    ' Get the form's whole image.
    Using whole_form As Bitmap = GetControlImage(frm)
        ' See how far the form's upper left corner is
        ' from the upper left corner of its client area.
        Dim origin As Point = frm.PointToScreen(New _
            Point(0, 0))
        Dim dx As Integer = origin.X - frm.Left
        Dim dy As Integer = origin.Y - frm.Top

        ' Copy the client area into a new Bitmap.
        Dim wid As Integer = frm.ClientSize.Width
        Dim hgt As Integer = frm.ClientSize.Height
        Dim bm As New Bitmap(wid, hgt)
        Using gr As Graphics = Graphics.FromImage(bm)
            gr.DrawImage(whole_form, 0, 0, _
                New Rectangle(dx, dy, wid, hgt), _
                GraphicsUnit.Pixel)
        End Using
        Return bm
    End Using
End Function
 
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 Sub btnPage2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnPage2.Click
    Dim selected As Integer = tabControl1.SelectedIndex
    tabControl1.SelectedIndex = 1

    ShowControlImage(tabPage2)

    tabControl1.SelectedIndex = selected
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated