Capture Active Window in VB .NET(Help me)

Wilson

New member
Joined
Nov 8, 2005
Messages
3
hi people
I wanted to now how can I capture the active window, like I could do on VB6, calling to gdi32, what happens is that when I update my project to VB .NET, doesnt update the hdc method

Thanks
 
Could you post the code you are attempting to use with .Net? Its a lot easier to fix problems if you give a bit more detail...
Also be aware data types have changed since vb6: longs are now integers, integers are now short for example.
 
If I understand you correctly, this should help.

In order to get the hDC of a System.Windows.Form object (since forms do not have a hDC property anymore) you must get the handle of the window and call the API function GetDC.
[VB]
Declare Function GetDC Lib "user32.dll" ( _
ByVal hwnd As IntPtr) As IntPtr

Public Function GetAFormshDC(Form As System.Windows.Forms) As IntPtr
Return GetDC(Form.Handle)
End Sub
[/VB]
 
Hi again, well, I resolved the problem :p
the code is here
[VB]
Private Declare Function BitBlt Lib "gdi32" (ByVal hObject As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hObjectSource As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As Integer) As Boolean
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As IntPtr, ByVal nWidth As Integer, ByVal nHeight As Integer) As IntPtr
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As IntPtr) As IntPtr
Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As IntPtr) As Boolean
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As IntPtr) As Boolean
Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As IntPtr, ByVal hObject As IntPtr) As IntPtr
Private Declare Function GetDesktopWindow Lib "user32" () As IntPtr
Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As IntPtr) As IntPtr
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As IntPtr
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As IntPtr, ByRef rect As RECT) As IntPtr
Private Const SRCCOPY As Integer = &HCC0020
Public Structure RECT
Public left As Integer
Public top As Integer
Public right As Integer
Public bottom As Integer
End Structure RECT
Public Function CaptureWindow(ByVal handle As IntPtr) As Image
get te hDC of the target window
Dim hdcSrc As IntPtr = GetWindowDC(handle)
get the size
Dim windowRect As New RECT
GetWindowRect(handle, windowRect)
Dim width As Integer = windowRect.right - windowRect.left
Dim height As Integer = windowRect.bottom - windowRect.top
create a device context we can copy to
Dim hdcDest As IntPtr = CreateCompatibleDC(hdcSrc)
create a bitmap we can copy it to,
using GetDeviceCaps to get the width/height
Dim hBitmap As IntPtr = CreateCompatibleBitmap(hdcSrc, width, height)
select the bitmap object
Dim hOld As IntPtr = SelectObject(hdcDest, hBitmap)
bitblt over
BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY)
restore selection
SelectObject(hdcDest, hOld)
clean up
DeleteDC(hdcDest)
ReleaseDC(handle, hdcSrc)
get a .NET image object for it
Dim img As Image = Image.FromHbitmap(hBitmap)
free up the Bitmap object
DeleteObject(hBitmap)
Return img
End Function CaptureWindow
[/VB]

Thanks Again :)
 
Last edited by a moderator:
in vb2005:

Code:
        Dim bm As New Bitmap(Me.Width, Me.Height)
        Dim g As Graphics = Graphics.FromImage(bm)
        g.CopyFromScreen(Me.Location, New Point(0, 0), New Size(Me.Width, Me.Height))
        bm.Save("C:\formgrab.bmp", Drawing.Imaging.ImageFormat.Bmp)

For some reason Im losing the \ from the path up there once I post the messgae :confused:
 
Last edited by a moderator:
Back
Top