I have to use a camera SDK for a microscope that comes with a sample C# Windows Forms application. The SDK functions are imported to the app via DLLImport. In this sample application the single frames from the camera are read from RAM via a callback function. This callback function then updates a PictureBox to show the live image.
For my useage however I want to not only show the frames, but also record them. I think the easiest way to do this is by using SharpAvi. However I don't know how to initialize the AviWriter (I obviously can't initialize it everytime the callback fires) and then add the single frames to the stream from the callback function.
At the moment I solved my problem by using another function that waits in a seperate thread for a public counter to go up - the counter itself is updated in the callback function. Everytime the counter is updated this function then also reads the frame from RAM and adds it to the AviWriterStream. To top it of I had to make the thread block my UI by using
var resetEvent = new ManualResetEvent(false);
System.Threading.ThreadPool.QueueUserWorkItem(delegate
{
WriteAVI()
resetEvent.Set();
}, null);
resetEvent.WaitOne();
This is due to the fact that I need to record several videos at different XY positions - so everytime my for loop arrives at a new XY position the recording is triggered and I have to make sure to only move to the next position once the recording is actually finished.
My way of doing this is obviously hackish and very bad practice (I already feel bad but just don't know better) and I am wondering what the correct solution would be. So far it works - just the UI freeze is really annoying.
Edit: These are probably two questions: 1) How to correctly write frames to the AVI and 2) How to wait for the recording to be completed without making it freeze my UI