Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleGet a form's image with or without decorations in Visual Basic .NET
DescriptionThis example shows how to get a form's image with or without decorations in Visual Basic .NET.
Keywordsform image, background, clipboard, PrntScrn
CategoriesVB.NET, Controls
 
The GetFormImage function gets the form's image. It starts by using the form's DrawToBitmap method to make the form draw itself into a bitmap. Then if the routine should remove the decorations (title bar and borders), it maps the point (0, 0) in the form's client area to screen coordinates. It subtracts the form's X and Y position from the mapped point's coorrdinates to see how big the title bar is and copies the rest of the form's image into a new bitmap.
 
Private Function GetFormImage(Optional ByVal _
    with_decorations As Boolean = True) As Image
    Dim bm As New Bitmap(Me.Width, Me.Height)
    Me.DrawToBitmap(bm, New Rectangle(0, 0, Me.Width, _
        Me.Height))

    If Not with_decorations Then
        ' Trim off the decorations.
        Dim client_origin As Point = Me.PointToScreen(New _
            Point(0, 0))
        Dim deco_left As Integer = client_origin.X - Me.Left
        Dim deco_top As Integer = client_origin.Y - Me.Top
        Dim wid As Integer = Me.ClientRectangle.Width
        Dim hgt As Integer = Me.ClientRectangle.Height
        Dim new_bm As New Bitmap(wid, hgt)
        Dim gr As Graphics = Graphics.FromImage(new_bm)

        gr.DrawImage(bm, New Rectangle(0, 0, wid, hgt), New _
            Rectangle(deco_left, deco_top, wid, hgt), _
            GraphicsUnit.Pixel)
        bm = new_bm
    End If

    Return bm
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated