我正在尝试从后台工作人员截取屏幕截图并显示在图片框中。
我的问题是我得到了这个例外:Exception has been thrown by the target of an invocation.
内部异常:Parameter is not valid.
有人可以解释为什么吗?
我正在使用这些using()
块来防止内存泄漏。
public void DoStuff() {
var bw = new BackgroundWorker();
Bitmap b = null;
bw.DoWork += delegate {
Rectangle bounds = Screen.PrimaryScreen.Bounds;
using(Bitmap bmp = new Bitmap(bounds.Width,
bounds.Height,
PixelFormat.Format32bppArgb)) {
using(Graphics gfx = Graphics.FromImage(bmp)) {
x.CopyFromScreen(bounds.X,
bounds.Y,
0,
0,
bounds.Size,
CopyPixelOperation.SourceCopy);
b = bmp;
}
}
};
bw.RunWorkerCompleted += delegate {
pictureBox1.Image = b;
};
bw.RunWorkerAsync();
}