我只是想制作一个简单的程序,从我的相机制作截图并关闭。我正在使用Aforge.net,但我的相机没有任何SnapshotCapabilities,所以我必须使用VideoCapabilities。我有这个简单的代码:
namespace Make_picture
{
public partial class Form1 : Form
{
private FilterInfoCollection videoDevices;
private VideoCaptureDevice cam;
private Image img;
private int ind = 1;
public Form1()
{
InitializeComponent();
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
}
private void CameraStart()
{
cam = new VideoCaptureDevice(videoDevices[ind].MonikerString);
cam.NewFrame += new NewFrameEventHandler(cam_NewFrame);
cam.Start();
}
private void CameraStop()
{
if (cam != null)
{
if (cam.IsRunning)
{
cam.Stop();
}
}
}
void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
img = (Bitmap)eventArgs.Frame.Clone();
img.Save("image.png", System.Drawing.Imaging.ImageFormat.Png);
cam.NewFrame -= new NewFrameEventHandler(cam_NewFrame);
this.Close();
//this.Invoke((MethodInvoker)delegate() { this.Close(); });
}
private void Form1_Shown(object sender, EventArgs e)
{
CameraStart();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
CameraStop();
}
}
}
在第一个新帧被触发后,我有关闭程序的问题。它说我无法在此线程上关闭它,因为表单是在另一个(Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on
)上创建的。搜索堆栈交换我也尝试了这个:
this.Invoke((MethodInvoker)delegate() { this.Close(); });
但它也不起作用(没有例外,但形式一直悬挂,我不能对他做任何事)。你能帮帮我解决这个问题吗? 非常感谢。
晏
PS:我不是程序员,所以我猜这可能会有很多不同。但我认为这可行。如果我知道如何杀死表格:)