在 Windows Form Picturebox C# 中插入截图
Insert a screenshot in Windows Form Picturebox C#
我在我的 WindowsForm 应用程序中编写了这个方法来从屏幕的一部分截取屏幕截图:
public static Image Preview(int startX, int startY, int width, int height)
{
Rectangle bounds = new Rectangle(startX, startY, width, height);
using (Bitmap scr = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(scr))
{
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}
return scr;
}
}
然后我在 PictureBox 中输入 return 值:
MyPictureBox.Image = Preview(0, 0, 1080, 720);
但是当我启动它时出现这个错误:
System.ArgumentException: 'Parameter is not valid.'
有什么问题吗?
(对于语法错误,我深表歉意,我不是英语母语者)
如果删除 using
:
,我将不再收到错误消息
Bitmap scr = new Bitmap(bounds.Width, bounds.Height);
{
using (Graphics g = Graphics.FromImage(scr))
{
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}
}
return scr;
我在我的 WindowsForm 应用程序中编写了这个方法来从屏幕的一部分截取屏幕截图:
public static Image Preview(int startX, int startY, int width, int height)
{
Rectangle bounds = new Rectangle(startX, startY, width, height);
using (Bitmap scr = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(scr))
{
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}
return scr;
}
}
然后我在 PictureBox 中输入 return 值:
MyPictureBox.Image = Preview(0, 0, 1080, 720);
但是当我启动它时出现这个错误:
System.ArgumentException: 'Parameter is not valid.'
有什么问题吗? (对于语法错误,我深表歉意,我不是英语母语者)
如果删除 using
:
Bitmap scr = new Bitmap(bounds.Width, bounds.Height);
{
using (Graphics g = Graphics.FromImage(scr))
{
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}
}
return scr;