[VB.Net / C#] Capture (even offscreen) Window by name 05-24-2011, 06:49 AM
#1
Thanks to [link=http://www.codeproject.com/script/Membership/View.aspx?mid=2637676]rootjumper[/link] from [link=http://www.codeproject.com]CodeProject[/link] for the [link=http://www.codeproject.com/Messages/3240814/Re-Capture-Screenshot-of-a-Window-With-Handle.aspx]Capture function[/link]
C#
VB.Net
C#
Code:
public class frmMain
{
[DllImport("user32.dll")]
private static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, uint nFlags);
[DllImport("user32.dll")]
private static extern int GetWindowRect(IntPtr hWnd, ref Rectangle rect);
[DllImport("user32.dll", EntryPoint = "GetDC")]
public static extern IntPtr GetDC(IntPtr ptr);
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
public static extern IntPtr DeleteObject(IntPtr hDc);
[DllImport("user32.dll", EntryPoint = "FindWindowA")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
private new Bitmap Capture(IntPtr pHwnd)
{
// Rectobject
Rectangle r = new Rectangle(0, 0, 0, 0);
GetWindowRect(pHwnd, ref r);
// GetWindowRectangle
r.Width = r.Width - r.X;
// Set Client Rectangle only (like ALT + Print)
r.Height = r.Height - r.Y;
Bitmap bmp = new Bitmap(r.Width, r.Height);
// Create new empty Bitmap
Graphics e = Graphics.FromImage(bmp);
// Get Grapthics from Bitmap
IntPtr ptr = e.GetHdc();
// Get Graphics Handle
try {
// Get DeviceContext Handles
IntPtr hDC = GetDC(pHwnd);
// Copy Screen from Process-Handle into Bitmap handle
PrintWindow(pHwnd, ptr, 0);
// Delete the DeviceContextHandle
DeleteObject(hDC);
} catch {
}
e.ReleaseHdc(ptr);
// Apply new bitmap handle with graphics
e.Flush();
// apply changes
e.Dispose();
// free graphic object
return bmp;
}
private void timCapture_Tick(System.Object sender, System.EventArgs e)
{
// Captures Notepad Window
this.BackgroundImage = Capture(FindWindow("notepad", null));
}
}
VB.Net
Code:
Imports System.Runtime.InteropServices
Public Class frmMain
<DllImport("user32.dll")> _
Private Shared Function PrintWindow(ByVal hwnd As IntPtr, ByVal hdcBlt As IntPtr, ByVal nFlags As UInteger) As Boolean
End Function
<DllImport("user32.dll")> _
Private Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef rect As Rectangle) As Integer
End Function
<DllImport("user32.dll", EntryPoint:="GetDC")> _
Public Shared Function GetDC(ByVal ptr As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll", EntryPoint:="DeleteObject")> _
Public Shared Function DeleteObject(ByVal hDc As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="FindWindowA")> _
Public Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
Private Shadows Function Capture(ByVal pHwnd As IntPtr) As Bitmap
' Rectobject
Dim r As New Rectangle(0, 0, 0, 0)
GetWindowRect(pHwnd, r) ' GetWindowRectangle
r.Width = r.Width - r.X ' Set Client Rectangle only (like ALT + Print)
r.Height = r.Height - r.Y
Dim bmp As New Bitmap(r.Width, r.Height) ' Create new empty Bitmap
Dim e As Graphics = Graphics.FromImage(bmp) ' Get Grapthics from Bitmap
Dim ptr As IntPtr = e.GetHdc() ' Get Graphics Handle
Try
' Get DeviceContext Handles
Dim hDC As IntPtr = GetDC(pHwnd)
' Copy Screen from Process-Handle into Bitmap handle
PrintWindow(pHwnd, ptr, 0)
' Delete the DeviceContextHandle
DeleteObject(hDC)
Catch
End Try
e.ReleaseHdc(ptr) ' Apply new bitmap handle with graphics
e.Flush() ' apply changes
e.Dispose() ' free graphic object
Return bmp
End Function
Private Sub timCapture_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timCapture.Tick
Me.BackgroundImage = Capture(FindWindow("notepad", Nothing)) 'Captures Notepad Window
End Sub
End Class
![[Image: rytwG00.png]](http://i.imgur.com/rytwG00.png)
Redcat Revolution!