gpt4 book ai didi

c# - 截图并在表格中显示

转载 作者:行者123 更新时间:2023-11-30 23:09:12 26 4
gpt4 key购买 nike

我尝试像描述的那样创建屏幕截图 here :

private Graphics takeScreenshot()
{
//Create a new bitmap.
var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
System.Drawing.Imaging.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);

return gfxScreenshot;
}

拍摄后如何在我的表格中显示?我试图在 pictureBox 中显示它:

Graphics screenshot = takeScreenshot();
pictureBox1.Image = screenshot;

但是我得到:

Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type 'System.Drawing.Graphics' to 'System.Drawing.Image' SRAT C:\Users\Edd\documents\visual studio 2017\Projects\SRAT\SRAT\Form1.cs 20 Active

this answer说不可能转换它

最佳答案

Graphics 对象是一种图像包装器,可让您在图像上绘图。它们通常是临时的,实际上并不拥有您正在绘制的像素。

在您的情况下,gfxScreenshot 只是提供绘制到 bmpScreenshot 上的能力,这是图像实际存在于内存中的位置。

你应该扔掉 Graphics 并返回 Bitmap:

private Bitmap TakeScreenshot()
{
//Create a new bitmap.
var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);

// Create a graphics object from the bitmap.
using (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);
}

return bmpScreenshot;
}

然后您可以将位图分配给 PictureBox:

Bitmap screenshot = TakeScreenshot();
pictureBox1.Image = screenshot;

关于c# - 截图并在表格中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45968878/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com