System.InvalidOperationException'在System.Drawing.dll中发生,
本文关键字:System dll InvalidOperationException Drawing | 更新日期: 2023-09-27 18:15:48
我正在用c#制作一个带有3,2,1倒计时的简单相机,使用Windows窗体打开一个新窗体来显示捕获的图像。当第一个Form空闲运行时,它最终会报错:"类型'System '的异常。InvalidOperationException'在System.Drawing.dll中发生,但未在用户代码中处理' .
程序显示错误是pictureBox1。图片正在其他地方使用:
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
我不确定是什么原因造成的。以下是与相机相关的所有代码:
private static object locker = new Object();
private FilterInfoCollection CaptureDevice;
private VideoCaptureDevice FinalFrame;
public Image File { get; private set; }
public void Form1_Load(object sender, EventArgs e)
{
FormBorderStyle = FormBorderStyle.None;
if (CaptureDevice == null)
{
lock (locker)
{
CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
}
}
foreach (FilterInfo Device in CaptureDevice)
{
lock(locker)
{
comboBox1.Items.Add(Device.Name);
}
}
lock (locker)
{
comboBox1.SelectedIndex = 0;
FinalFrame = new VideoCaptureDevice();
button1.PerformClick();
button2.BringToFront();
}
}
private void button1_Click(object sender, EventArgs e)
{
lock (locker)
{
button1.SendToBack();
FinalFrame = new VideoCaptureDevice(CaptureDevice[comboBox1.SelectedIndex].MonikerString);
FinalFrame.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame);
FinalFrame.Start();
}
}
private void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
lock (locker)
{
try
{
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone(); // <- Here is where the program indicates there is an error
}
catch (Exception exec)
{
Console.Write(exec);
}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (FinalFrame.IsRunning == true)
{
FinalFrame.Stop();
}
}
private async void button2_Click(object sender, EventArgs e)
{
button2.SendToBack();
button2.Hide();
customLabel1.Show();
await Task.Delay(200);
customLabel1.BringToFront();
customLabel1.Refresh();
await Task.Delay(800);
customLabel1.Refresh();
customLabel1.Text = "2";
await Task.Delay(800);
customLabel1.Refresh();
customLabel1.Text = "1";
await Task.Delay(800);
customLabel1.Refresh();
customLabel1.Text = "3";
customLabel1.Hide();
button2.Show();
button2.BringToFront();
button2.Text = "CAPTURE";
Form2 myPic = new Form2();
myPic.pictureBox1.Image = (Bitmap)pictureBox1.Image.Clone() as Image;
myPic.ShowDialog();
}
必须处理所有以前使用过的图像。
//if (pictureBox1.Image != null)
pictureBox1.Image.Dispose();
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
我也遇到过类似的问题。解决方案是只在UI线程中设置Image
。
所以最优的方法是这样的:
void SetImageThreadSafe(PictureBox pb, Image img)
{
if (pb.InvokeRequired)
{
BeginInvoke((Action) delegate
{
SetImageThreadSafe(pb, img);
});
return;
}
pb.Image?.Dispose();
pb.Image = img;
}