Post: SOURCE][VB.NET]How to get a Screenshot of Any Window/Desktop
05-10-2011, 12:05 AM #1
(adsbygoogle = window.adsbygoogle || []).push({});
Originally posted by kingdeath360 View Post
Title explains it all really, this uses BitBlt from GDI32 to get the bitmap of a window.

An example usage of this would be:

    
Dim SSG As New ScreenShotGrabber
Dim Bitmap1, Bitmap2, Bitmap3, Bitmap4 As Bitmap
Bitmap1 = SSG.GetScreenshotOfDesktop
Bitmap2 = SSG.GetScreenshotFromHandle(Me.Handle)
Bitmap3 = SSG.GetScreenShotByWindowText("Untitled - Notepad")
Bitmap4 = SSG.GetScreenShotByWindowTextWhichContains("Mozilla Firefox")


The actual snippet:

    
Public Structure ScreenShotGrabber
#Region "Declared Functions & Constants"
Private Declare Function FindWindowExA Lib "user32" (ByVal Parent As IntPtr, ByVal ChildAfter As IntPtr, ByVal Classname As String, ByVal WindowText As String) As IntPtr
Private Declare Function GetWindowDC Lib "user32" (ByVal Hwnd As IntPtr) As IntPtr
Private Declare Function GetWindowTextA Lib "user32" (ByVal Handle As IntPtr, ByVal Builder As System.Text.StringBuilder, ByVal MaxCharacters As Integer) As Integer
Private Declare Function ReleaseDC Lib "user32" (ByVal Hwnd As IntPtr, ByVal hd As IntPtr) As Integer
Private Declare Function BitBlt Lib "gdi32.dll" (ByVal DestHDC As IntPtr, ByVal DestX As Integer, ByVal DestY As Integer, ByVal DestWidth As Integer, ByVal DestHeight As Integer, ByVal OriginSource As IntPtr, ByVal OriginX As Integer, ByVal OriginY As Integer, ByVal Message As Integer) As Boolean
Private Declare Function GetWindowRect Lib "user32" (ByVal Hwnd As IntPtr, ByRef ReceivingRECT As RECT) As Integer
Private Const BITBLT_SRCCOPY As Integer = &HCC0020
Private Const DESKTOP As Integer = &H10010
Private Declare Function IsWindowVisible Lib "user32" (ByVal Handle As IntPtr) As Boolean
#End Region
Private Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
Public Function GetScreenShotByWindowText(ByVal WindowName As String) As Bitmap
Return GetBitmap(FindWindowExA(0, 0, vbNullString, WindowName))
End Function
Public Function GetScreenShotByWindowTextWhichContains(ByVal StringContents As String) As Bitmap
For Each I In GetVisibleWindows()
Dim TempBuilder As System.Text.StringBuilder = New System.Text.StringBuilder(255)
GetWindowTextA(I, TempBuilder, 255)
If TempBuilder.ToString.Contains(StringContents) Then
TempBuilder = Nothing
Return GetBitmap(I)
End If
TempBuilder = Nothing
Next
Return Nothing
End Function
Private Function GetVisibleWindows() As IntPtr()
Dim VisibleList As List(Of IntPtr) = New List(Of IntPtr)
Dim VisibleWindows() As IntPtr
Dim LoopHandle As IntPtr
LoopHandle = FindWindowExA(0, 0, vbNullString, vbNullString)
While LoopHandle <> 0
If IsWindowVisible(LoopHandle) Then
VisibleList.Add(LoopHandle)
End If
LoopHandle = FindWindowExA(0, LoopHandle, vbNullString, vbNullString)
End While
VisibleWindows = VisibleList.ToArray()
LoopHandle = Nothing
VisibleList = Nothing
Return VisibleWindows
End Function
Public Function GetScreenshotFromHandle(ByVal Handle As IntPtr) As Bitmap
Return GetBitmap(Handle)
End Function
Public Function GetScreenshotOfDesktop() As Bitmap
Return GetBitmap(DESKTOP, True)
End Function
Private Function GetBitmap(ByVal WindowHandle As IntPtr, Optional ByVal IsDesktop As Boolean = False) As Bitmap
Dim TempWidth As Integer
Dim WindowRect As RECT
Dim TempHeight As Integer
Dim ReceivingBitmap As Bitmap
Dim ReceivingGraphics As Graphics
Dim ReceivingHDC As IntPtr
Dim WindowHDC As IntPtr
Try
If IsDesktop Then
GetWindowRect(WindowHandle, WindowRect)
TempWidth = WindowRect.Right - WindowRect.Left
TempHeight = WindowRect.Bottom - WindowRect.Top
ReceivingBitmap = New Bitmap(TempWidth, TempHeight)
ReceivingGraphics = Graphics.FromImage(ReceivingBitmap)
ReceivingHDC = ReceivingGraphics.GetHdc
WindowHDC = GetWindowDC(WindowHandle)
BitBlt(ReceivingHDC, 0, 0, TempWidth, TempHeight, WindowHDC, 0, 0, BITBLT_SRCCOPY)
Else
GetWindowRect(WindowHandle, WindowRect)
TempWidth = WindowRect.Right - WindowRect.Left - 7
TempHeight = WindowRect.Bottom - WindowRect.Top - 7
ReceivingBitmap = New Bitmap(TempWidth - 9, TempHeight - 33)
ReceivingGraphics = Graphics.FromImage(ReceivingBitmap)
ReceivingHDC = ReceivingGraphics.GetHdc
WindowHDC = GetWindowDC(WindowHandle)
BitBlt(ReceivingHDC, 0, 0, TempWidth, TempHeight, WindowHDC, 8, 31, BITBLT_SRCCOPY)
End If
ReceivingGraphics.ReleaseHdc(ReceivingHDC)
ReleaseDC(WindowHandle, WindowHDC)
Catch
ReceivingBitmap = Nothing
End Try
ReceivingGraphics = Nothing
WindowHDC = Nothing
ReceivingHDC = Nothing
TempWidth = Nothing
TempHeight = Nothing
WindowRect = Nothing
Return ReceivingBitmap
End Function
End Structure



Please note that this will not capture the title bar, since there is issues about doing that with windows 7(Due to aero theme, it shows transparent in BitBlt if the window is not focused). You can easily change the code to fit your needs however.


source You must login or register to view this content.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo