From fbf9152128e64afd97b57c8a8a8aaf39af39c64a Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Tue, 17 Jun 2025 00:24:42 +0200 Subject: [PATCH] Initial commit --- .gitignore | 3 ++ WindowCapture.cs | 104 +++++++++++++++++++++++++++++++++++++++++++ WindowCapture.csproj | 11 +++++ 3 files changed, 118 insertions(+) create mode 100644 .gitignore create mode 100644 WindowCapture.cs create mode 100644 WindowCapture.csproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e3a6880 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vs +bin +obj diff --git a/WindowCapture.cs b/WindowCapture.cs new file mode 100644 index 0000000..e2a77fd --- /dev/null +++ b/WindowCapture.cs @@ -0,0 +1,104 @@ +using System; +using System.Drawing; +using System.Runtime.InteropServices; + +class Program { + [StructLayout(LayoutKind.Sequential)] + private struct RECT { + public int Left; + public int Top; + public int Right; + public int Bottom; + } + + [DllImport("user32.dll")] + private static extern IntPtr GetDC(IntPtr hWnd); + + [DllImport("user32.dll")] + private static extern int GetClientRect(IntPtr hWnd, out RECT rect); + + [DllImport("user32.dll")] + private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); + + [DllImport("gdi32.dll")] + private static extern IntPtr CreateCompatibleDC(IntPtr hDC); + + [DllImport("gdi32.dll")] + private static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int width, + int height); + + [DllImport("gdi32.dll")] + private static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject); + + [DllImport("gdi32.dll")] + private static extern bool DeleteObject(IntPtr hObject); + + [DllImport("gdi32.dll")] + private static extern bool DeleteDC(IntPtr hDC); + + [DllImport("user32.dll")] + private static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, + uint nFlags); + + private const uint PW_RENDERFULLCONTENT = 0x00000002; + + static bool CaptureWindow(IntPtr windowHandle, string outputPath) { + try { + // Get the window dimensions + GetClientRect(windowHandle, out RECT windowRect); + int width = windowRect.Right - windowRect.Left; + int height = windowRect.Bottom - windowRect.Top; + + // Create compatible DC and bitmap + IntPtr sourceContext = GetDC(windowHandle); + IntPtr destContext = CreateCompatibleDC(sourceContext); + IntPtr bitmap = CreateCompatibleBitmap(sourceContext, width, height); + + // Select the bitmap into the DC + IntPtr oldBitmap = SelectObject(destContext, bitmap); + + // Use PrintWindow to capture the window content + PrintWindow(windowHandle, destContext, PW_RENDERFULLCONTENT); + + // Create the final bitmap + using (Bitmap screenshot = Bitmap.FromHbitmap(bitmap)) { + screenshot.Save(outputPath, System.Drawing.Imaging.ImageFormat.Png); + } + + // Clean up + SelectObject(destContext, oldBitmap); + DeleteObject(bitmap); + DeleteDC(destContext); + ReleaseDC(windowHandle, sourceContext); + + return true; + } catch (Exception) { + return false; + } + } + + static int Main(string[] args) { + if (args.Length != 2) { + Console.WriteLine( + "Usage: WindowCapture.exe "); + Console.WriteLine("Example: WindowCapture.exe 123456 screenshot.png"); + return 1; + } + + if (!IntPtr.TryParse(args[0], out IntPtr windowHandle)) { + Console.WriteLine( + "Invalid window handle. Please provide a valid number."); + return 1; + } + + string outputPath = args[1]; + + if (CaptureWindow(windowHandle, outputPath)) { + Console.WriteLine($"Screenshot saved to: {outputPath}"); + return 0; + } else { + Console.WriteLine("Failed to capture screenshot"); + return 1; + } + } +} \ No newline at end of file diff --git a/WindowCapture.csproj b/WindowCapture.csproj new file mode 100644 index 0000000..88e2043 --- /dev/null +++ b/WindowCapture.csproj @@ -0,0 +1,11 @@ + + + Exe + net8.0 + enable + enable + + + + + \ No newline at end of file