I am just trying to make a simple program that starts, makes screenshot from my camera and closes. I am using Aforge.net but my camera does not have any SnapshotCapabilities, so I have to use VideoCapabilities. I have this simple code:
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();
}
}
}
I have issue to close program after first new frame is fired. It says that I cannot close it on this thread because form was created on another (Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on
). Searching stack exchange I have tried also this:
this.Invoke((MethodInvoker)delegate() { this.Close(); });
but it does not work either (no exception, but form keeps hanging and I cannot do nothing with him). Could you help me with this issue? Really thanks.
Yann
PS: I am not a programmer, so this could be done a lot differently I guess. But I think this could work If I knew how to kill the form :)