Wilson Posted November 8, 2005 Posted November 8, 2005 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 Quote
Administrators PlausiblyDamp Posted November 9, 2005 Administrators Posted November 9, 2005 Could you post the code you are attempting to use with .Net? It's 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders snarfblam Posted November 9, 2005 Leaders Posted November 9, 2005 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 Quote [sIGPIC]e[/sIGPIC]
Wilson Posted November 12, 2005 Author Posted November 12, 2005 hi, thanks for help me I've found one code, but i can't make it work can help me again, with this :rolleyes:ScreenCapture.zip Quote
Leaders snarfblam Posted November 12, 2005 Leaders Posted November 12, 2005 What is the problem you are having? Quote [sIGPIC]e[/sIGPIC]
Wilson Posted November 14, 2005 Author Posted November 14, 2005 (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 November 14, 2005 by Wilson Quote
jo0ls Posted November 16, 2005 Posted November 16, 2005 (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 November 16, 2005 by jo0ls Quote
Nate Bross Posted November 17, 2005 Posted November 17, 2005 I believe it's a known issue, in the mean time use \\ Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.