What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleGrab the image of another program's form in Visual Basic .NET
DescriptionThis example shows how to grab the image of another program's form in Visual Basic .NET.
Keywordsform image, background, clipboard, PrntScrn
CategoriesVB.NET, Controls, Windows
This program uses the SetForegroundWindow, keybd_event, and GetWindowRect API functions so it declares them.
Imports System.Runtime.InteropServices

Private Declare Function SetForegroundWindow Lib "user32" _
    (ByVal hWnd As IntPtr) As Boolean
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As _
    Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, _
    ByVal dwExtraInfo As Integer)
Private Const VK_SNAPSHOT As Short = &H2CS

Public Overloads Declare Function GetWindowRect Lib _
    "User32" Alias "GetWindowRect" (ByVal hWnd As IntPtr, _
    ByRef lpRect As RECT) As Int32
<StructLayout(LayoutKind.Sequential)> _
    Public Structure RECT
    Public Left As Integer
    Public Top As Integer
    Public Right As Integer
    Public Bottom As Integer
End Structure
When you click the Go button, the program reads the name of a target application from the txtProgram text box. It loops through the processes running on the system to find the target program and passes its main window handle to function GetProgramImage.
Private Sub btnGo_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnGo.Click
    Dim target As String = txtProgram.Text.ToLower()
    For Each proc As Process In Process.GetProcesses()
        ' See if this is the target program.
        If proc.ProcessName.ToLower().Contains(target) Then
            ' We found it. Take its picture.
            Dim bm as Bitmap = _
            picImage.Image = bm
            Exit For
        End If
    Next proc
End Sub
Function GetprogramImage calls SetForegroundWindow to move the target application to the top. It then calls GetDesktopImage to grab the desktop's image.

The program then uses GetWindowRect to get the target program's location, copies that part of the desktop image into a Bitmap, and returns the result.

' Return a program's image.
Private Function GetProgramImage(ByVal handle As IntPtr) As _
    ' Bring it to the top.

    ' Get the desktop image.
    Dim desktop_bm As Bitmap = GetDesktopImage()

    ' Find the program's window.
    Dim program_rect As RECT
    GetWindowRect(handle, program_rect)

    ' Copy that part of the desktop image.
    Dim wid As Integer = program_rect.Right - _
    Dim hgt As Integer = program_rect.Bottom - _
    Dim dest_rect As New Rectangle(0, 0, wid, hgt)
    Dim src_rect As New Rectangle(program_rect.Left, _
        program_rect.Top, wid, hgt)
    Dim new_bm As New Bitmap(wid, hgt)
    Using gr As Graphics = Graphics.FromImage(new_bm)
        gr.DrawImage(desktop_bm, dest_rect, src_rect, _
    End Using

    Return new_bm
End Function
Function GetDesktopImage uses the keybd_event API function to simulate pressing PrntScrn to copy the desktop image to the clipboard.
' Return the desktop image.
Private Function GetDesktopImage() As Image
    keybd_event(System.Windows.Forms.Keys.Snapshot, 0, 0, 0)

    If Clipboard.ContainsImage() Then Return _
    Return Nothing
End Function
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.