I have a picturebox in my windows form that renders a stream from my camera it uses a worker thread as you can see :
VideoFileReader reader = new VideoFileReader();
Thread Proceso1;
Proceso1 = new Thread(new ThreadStart(updateui));
public void updateui()
{
try
{
reader.Open(RTSPAddress);
while (true)
{
var previousFrame = pictureRTSP.BackgroundImage;
Bitmap currentFrame = reader.ReadVideoFrame();
pictureRTSP.BackgroundImage = currentFrame;
if (previousFrame != null)
this.Invoke(new MethodInvoker(delegate () {
previousFrame.Dispose();
}));
}
reader.Close();
}
catch(ArgumentException ee)
{
//Text = ee.ToString();
}
}
I dispose the previous frame because of memory usage .In my form i have a button that use pictureRTSP.BackgroundImage
to detect the plate in the video.But when i clicked on button for several time i get this error :
Application: nMCR.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
at <Module>.av_read_frame(libffmpeg.AVFormatContext*, libffmpeg.AVPacket*)
at Accord.Video.FFMPEG.VideoFileReader.readVideoFrame(Int32, System.Drawing.Imaging.BitmapData)
at Accord.Video.FFMPEG.VideoFileReader.ReadVideoFrame()
at nMCR.form.MainForm.updateui()
at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()
My button code:
BitmapImage bmImage = null;
if (IsRTSP)
{
//FinalImage = new Bitmap(_snapshotHandler.TakeSnapshot().ToImage());
FinalImage = new Bitmap(pictureRTSP.BackgroundImage);
//Bitmap img = (Bitmap)Image.FromStream(FinalImage);
bmImage = new BitmapImage();
using (MemoryStream memStream2 = new MemoryStream())
{
FinalImage.Save(memStream2, System.Drawing.Imaging.ImageFormat.Png);
memStream2.Position = 0;
bmImage.BeginInit();
bmImage.CacheOption = BitmapCacheOption.OnLoad;
bmImage.UriSource = null;
bmImage.StreamSource = memStream2;
bmImage.EndInit();
}
First you have a problem in opening the video reader, I think there is a problem in the Platform target of your solution, try making it 32 bit, some libraries that are using C++ native dlls inside may be 32 bit, and doesn't work with AnyCPU.
Secondly You are changing the background image of the picturebox
from a thread other than the main thread :
try calling Invoke when changing picturebox
image :
this.Invoke(new Action(()=> pictureRTSP.BackgroundImage = currentFrame ));