一尘不染

将屏幕捕获到位图中

c#

我想用代码捕获屏幕以获取图像,就像使用键盘上的“打印屏幕”按钮一样。

有谁知道如何做到这一点?我没有起点。


阅读 232

收藏
2020-05-19

共1个答案

一尘不染

如果使用.NET 2.0(或更高版本)框架,则可以使用CopyFromScreen()此处详细介绍的方法:

http://www.geekpedia.com/tutorial181_Capturing-screenshots-using-
Csharp.html

//Create a new bitmap.
var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                               Screen.PrimaryScreen.Bounds.Height,
                               PixelFormat.Format32bppArgb);

// Create a graphics object from the bitmap.
var gfxScreenshot = Graphics.FromImage(bmpScreenshot);

// Take the screenshot from the upper left corner to the right bottom corner.
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                            Screen.PrimaryScreen.Bounds.Y,
                            0,
                            0,
                            Screen.PrimaryScreen.Bounds.Size,
                            CopyPixelOperation.SourceCopy);

// Save the screenshot to the specified path that the user has chosen.
bmpScreenshot.Save("Screenshot.png", ImageFormat.Png);
2020-05-19