Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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, doesn't update the hdc method

 

Thanks

  • Leaders
Posted

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.

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

[sIGPIC]e[/sIGPIC]
Posted (edited)

Hi again, well, I resolved the problem :p

the code is here

   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

 

Thanks Again :)

Edited by Wilson
Posted (edited)

in vb2005:

 

'
       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 I'm losing the \ from the path up there once I post the messgae :confused:

Edited by jo0ls

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...