
C# 区域屏幕截图功能
5星
- 浏览量: 0
- 大小:None
- 文件类型:None
简介:
本工具利用C#编程语言实现区域屏幕截图功能,用户可自定义选择截取屏幕上的任意矩形区域,并保存为图片文件。
在C#中实现屏幕截图功能,并支持区域截图的代码如下:
```csharp
public static Image CaptureScreen()
{
return CaptureWindow(User32.GetDesktopWindow());
}
public static Image CaptureWindow(IntPtr handle)
{
IntPtr hdcSrc = User32.GetWindowDC(handle);
RECT windowRect = new RECT();
User32.GetWindowRect(handle, ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
IntPtr hdcDest = Gdi32.CreateCompatibleDC(hdcSrc);
IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height);
IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap);
Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY);
Gdi32.SelectObject(hdcDest, hOld);
Gdi32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc);
Image image = Image.FromHbitmap(hBitmap);
Gdi32.DeleteObject(hBitmap);
return image;
}
```
这段代码定义了两个方法`CaptureScreen()`和`CaptureWindow()`,用于获取整个屏幕或指定窗口的截图。其中使用到了GDI+ API来创建兼容设备上下文、位图等,并通过BitBlt函数进行图像复制操作。
全部评论 (0)


