Initial commit
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
.vs
|
||||||
|
bin
|
||||||
|
obj
|
104
WindowCapture.cs
Normal file
104
WindowCapture.cs
Normal file
@@ -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 <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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
WindowCapture.csproj
Normal file
11
WindowCapture.csproj
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="System.Drawing.Common" Version="9.0.6" />
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
Reference in New Issue
Block a user