我正在尝试进行全屏截屏并将其加载到pictureBox中,但这给了我这个错误:System.Drawing.dll中发生了类型'System.ArgumentException'的第一次机会异常。其他信息:Ongeldige参数。
码:
using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height))
{
using (Graphics g = Graphics.FromImage(bmpScreenCapture))
{
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0, 0,
bmpScreenCapture.Size,
CopyPixelOperation.SourceCopy);
}
pictureBox1.Image = bmpScreenCapture;
}
乔莉。
发生异常是因为该using
语句在将位图分配给后处理了它pictureBox1.Image
,因此PictureBox在需要重新绘制自身时无法显示位图:
using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height))
{
// ...
pictureBox1.Image = bmpScreenCapture;
} // <== bmpScreenCapture.Dispose() gets called here.
// Now pictureBox1.Image references an invalid Bitmap.
要解决此问题,请保留Bitmap变量声明和初始化程序,但删除以下代码using
:
Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
// ...
pictureBox1.Image = bmpScreenCapture;
您仍应确保最终处理完位图,但前提是您确实真正不再需要它时(例如,如果以后再替换pictureBox1.Image
另一个位图)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句