在C#中,要实现屏幕拷贝(即截图)操作,可以使用System.Drawing命名空间中的Graphics和Bitmap类
using System.Drawing;using System.Windows.Forms;创建一个方法来执行屏幕拷贝操作:public Bitmap CaptureScreen(){ // 获取屏幕尺寸 Rectangle screenBounds = Screen.PrimaryScreen.Bounds; // 创建一个与屏幕尺寸相同的位图对象 Bitmap screenshot = new Bitmap(screenBounds.Width, screenBounds.Height); // 创建一个Graphics对象,用于绘制位图 using (Graphics graphics = Graphics.FromImage(screenshot)) { // 将屏幕内容复制到位图上 graphics.CopyFromScreen(0, 0, 0, 0, screenBounds.Size); } // 返回截图位图 return screenshot;}调用CaptureScreen方法并保存截图:Bitmap screenshot = CaptureScreen();screenshot.Save("screenshot.png", System.Drawing.Imaging.ImageFormat.Png);这样就完成了屏幕拷贝操作。你可以根据需要修改代码,例如截取指定区域的屏幕,或者将截图保存为其他格式。




