在C#中实现Windows桌面截图功能可以通过多种方式实现。下面我将介绍几种常见的方法:
![图片[1]_基于C#实现Windows桌面截图功能_知途无界](https://zhituwujie.com/wp-content/uploads/2025/05/d2b5ca33bd20250512115234.png)
方法一:使用.NET内置的Graphics类
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
public class ScreenCapture
{
public static void CaptureScreen(string filePath)
{
try
{
// 获取主显示器的大小
Rectangle bounds = Screen.GetBounds(Point.Empty);
// 创建位图对象
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
// 创建Graphics对象
using (Graphics g = Graphics.FromImage(bitmap))
{
// 截取整个屏幕
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
// 保存截图
bitmap.Save(filePath, ImageFormat.Png);
}
Console.WriteLine($"截图已保存到: {filePath}");
}
catch (Exception ex)
{
Console.WriteLine($"截图失败: {ex.Message}");
}
}
}
// 使用示例
ScreenCapture.CaptureScreen("screenshot.png");
方法二:使用Windows API (user32.dll)
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
public class ScreenCapture
{
[DllImport("user32.dll")]
private static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("gdi32.dll")]
private static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
[DllImport("gdi32.dll")]
private static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll")]
private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hObject);
[DllImport("gdi32.dll")]
private static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSrc, int xSrc, int ySrc, int rasterOp);
[DllImport("gdi32.dll")]
private static extern bool DeleteObject(IntPtr hObject);
[DllImport("user32.dll")]
private static extern int ReleaseDC(IntPtr hWnd, IntPtr hdc);
private const int SRCCOPY = 0x00CC0020;
public static void CaptureScreen(string filePath)
{
IntPtr hWnd = GetDesktopWindow();
IntPtr hdcSrc = GetWindowDC(hWnd);
IntPtr hdcDest = CreateCompatibleDC(hdcSrc);
Rectangle bounds = Screen.GetBounds(Point.Empty);
IntPtr hBitmap = CreateCompatibleBitmap(hdcSrc, bounds.Width, bounds.Height);
IntPtr hOld = SelectObject(hdcDest, hBitmap);
BitBlt(hdcDest, 0, 0, bounds.Width, bounds.Height, hdcSrc, 0, 0, SRCCOPY);
SelectObject(hdcDest, hOld);
using (Bitmap bitmap = Image.FromHbitmap(hBitmap))
{
bitmap.Save(filePath, ImageFormat.Png);
}
DeleteObject(hBitmap);
ReleaseDC(hWnd, hdcSrc);
DeleteObject(hdcDest);
}
}
方法三:捕获特定窗口(需要窗口句柄)
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
public class WindowCapture
{
[DllImport("user32.dll")]
private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rectangle rect);
[DllImport("user32.dll")]
private static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags);
public static void CaptureWindow(IntPtr handle, string filePath)
{
Rectangle rect = new Rectangle();
GetWindowRect(handle, ref rect);
using (Bitmap bitmap = new Bitmap(rect.Width, rect.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
IntPtr hdc = g.GetHdc();
PrintWindow(handle, hdc, 0);
g.ReleaseHdc(hdc);
}
bitmap.Save(filePath, ImageFormat.Png);
}
}
}
高级功能:添加鼠标指针到截图中
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class ScreenCaptureWithCursor
{
[StructLayout(LayoutKind.Sequential)]
struct CURSORINFO
{
public int cbSize;
public int flags;
public IntPtr hCursor;
public Point ptScreenPos;
}
[DllImport("user32.dll")]
static extern bool GetCursorInfo(out CURSORINFO pci);
[DllImport("user32.dll")]
static extern bool DrawIcon(IntPtr hDC, int X, int Y, IntPtr hIcon);
const int CURSOR_SHOWING = 0x00000001;
public static void CaptureScreenWithCursor(string filePath)
{
Rectangle bounds = Screen.GetBounds(Point.Empty);
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
// 添加鼠标指针
CURSORINFO cursorInfo = new CURSORINFO();
cursorInfo.cbSize = Marshal.SizeOf(typeof(CURSORINFO));
if (GetCursorInfo(out cursorInfo) &&
(cursorInfo.flags & CURSOR_SHOWING) == CURSOR_SHOWING)
{
DrawIcon(g.GetHdc(), cursorInfo.ptScreenPos.X, cursorInfo.ptScreenPos.Y, cursorInfo.hCursor);
g.ReleaseHdc();
}
}
bitmap.Save(filePath, ImageFormat.Png);
}
}
}
注意事项
- 这些代码需要在Windows窗体应用程序或控制台应用程序中使用
- 需要添加对
System.Drawing和System.Windows.Forms程序集的引用 - 对于高DPI显示器,可能需要调整代码以正确处理缩放
- 如果要捕获多显示器环境中的特定显示器,可以使用
Screen.AllScreens数组
以上方法提供了从简单到高级的截图功能实现,你可以根据具体需求选择合适的方法。
© 版权声明
文中内容均来源于公开资料,受限于信息的时效性和复杂性,可能存在误差或遗漏。我们已尽力确保内容的准确性,但对于因信息变更或错误导致的任何后果,本站不承担任何责任。如需引用本文内容,请注明出处并尊重原作者的版权。
THE END

























暂无评论内容