What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleGrab part of the desktop image in VB .NET
DescriptionThis example shows how to grab part of the desktop image in VB .NET.
Keywordsdesktop, grab pixels, image, background, VB.NET
CategoriesGraphics, Windows
The DesktopImage function uses the GetDesktopWindow API function to get the hWnd of the desktop window. It uses GetDC to get a device context for that window. It uses Screen.GetBounds to see how big the desktop is.

Next the function makes a Bitmap big enough to hold the desktop's image, creates a Graphics object attached to it, and gets the Bitmap's device context.

The function then uses StretchBlt to copy the desktop window's image onto the Bitmap. It releases the Bitmap's and desktop's device context, and returns the Bitmap.

' Return an image of the desktop.
Private Function DesktopImage() As Bitmap
    ' Get the desktop size in pixels.
    Dim desktop_win As Int32 = GetDesktopWindow()
    Dim desktop_dc As Int32 = GetDC(desktop_win)
    Dim desktop_bounds As Rectangle = Screen.GetBounds(New _
        Point(1, 1))
    Dim desktop_wid As Int32 = desktop_bounds.Width
    Dim desktop_hgt As Int32 = desktop_bounds.Height

    ' Make a Bitmap to hold the image.
    Dim bm As New Bitmap(desktop_wid, desktop_hgt)
    Dim bm_gr As Graphics = Graphics.FromImage(bm)
    Dim bm_hdc As IntPtr = bm_gr.GetHdc

    ' Copy the desktop's image.
    StretchBlt( _
        bm_hdc, 0, 0, desktop_wid, desktop_hgt, _
        desktop_dc, 0, 0, desktop_wid, desktop_hgt, _

    ' Release the bitmap's  and desktop's DCs.
    ReleaseDC(desktop_win, desktop_dc)

    ' Return the result.
    Return bm
End Function
When the program starts, it grabs the complete desktop image, copies it into the picDesktop PictureBox, and displays its form. The form is maximized and picDesktop is docked to fill the form so the desktop image fills the form.

When the user presses the mouse down on picDesktop, the MouseDown event handler starts drawing a rubberband box. It saves the mouse's current position and makes a Graphics object to draw on the PictureBox.

When the user moves the mouse, the MouseMove event handler updates the location of the rubberband box's second corner, redraws the desktop image, and then draws the new rubberband box.

When the user releases the mouse, the MouseUp event handler hides the program's form. It uses the GetFolderPath function to get the user's desktop folder and then loops through a series of numbers until it finds a filename of the form "Screen Grab123.jpg" that is not in use.

Next it copies the selected part of the desktop image into a new Bitmap and uses the Bitmap's Save method to save the it in the file as a JPEG image.

Finally the program ends.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load

    ' Get the desktop image.
    m_bmDesktop = DesktopImage()

    ' Display the image.
    picDesktop.Image = DirectCast(m_bmDesktop.Clone(), _
End Sub

' Start dragging to select an area.
Private Sub picDesktop_MouseDown(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
    m_Drawing = True

    ' Save the point's coordinates.
    m_X1 = e.X
    m_X2 = e.X
    m_Y1 = e.Y
    m_Y2 = e.Y

    ' Make a Graphics object to draw on.
    m_grDesktop = picDesktop.CreateGraphics()
End Sub

' Continue drawing.
Private Sub picDesktop_MouseMove(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
    If Not m_Drawing Then Exit Sub

    ' Save the new point.
    m_X2 = e.X
    m_Y2 = e.Y

    ' Redraw the desktop image.
    m_grDesktop.DrawImage(m_bmDesktop, 0, 0)

    ' Draw the rubberband rectangle.
    Dim rect As New Rectangle( _
        Min(m_X1, m_X2), Min(m_Y1, m_Y2), _
        Abs(m_X1 - m_X2), Abs(m_Y1 - m_Y2))
    m_grDesktop.DrawRectangle(Pens.Blue, rect)
End Sub

' Stop drawing.
Private Sub picDesktop_MouseUp(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
    If Not m_Drawing Then Exit Sub
    m_Drawing = False

    ' Hide the form.

    ' Get the name of a file on the desktop.
    Dim file_path As String = _
    If Not file_path.EndsWith("\") Then file_path &= "\"
    file_path &= "Screen Grab"

    Dim file_name As String
    For i As Integer = 0 To 1000000
        file_name = file_path & i.ToString & ".jpg"
        Dim file_info As New FileInfo(file_name)
        If Not file_info.Exists() Then Exit For
    Next i

    ' Grab the selected piece of the image.
    Dim rect As New Rectangle( _
        Min(m_X1, m_X2), Min(m_Y1, m_Y2), _
        Abs(m_X1 - m_X2), Abs(m_Y1 - m_Y2))
    Dim bm As Bitmap = DirectCast( _
        m_bmDesktop.Clone(rect, m_bmDesktop.PixelFormat), _

    ' Save the image into the file.
    bm.Save(file_name, ImageFormat.Jpeg)

    ' Unload.
End Sub
Because this program grabs an image of the screen as it is shown when you start the program, you may want to compile it into an executable and place it on your desktop. Then you can start the executable without obscuring much of the desktop. If you run it in the Visual Basic IDE, most of the desktop is covered by the IDE.
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.