104 lines
3.0 KiB
C#
104 lines
3.0 KiB
C#
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 <window_handle> <output_path>");
|
|
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;
|
|
}
|
|
}
|
|
} |