secile / UsbCamera

C# source code for using usb camera and web camera in WinForms/WPF. With only single CSharp source code. No external library required.
MIT License
179 stars 55 forks source link

happens ArgumentException when I dispose PictureBox's old image. #34

Closed secile closed 6 months ago

secile commented 6 months ago

This issue is related to issue#27

To reduce memory consumption, when I replace code from

var timer = new System.Timers.Timer(1000 / 30) { SynchronizingObject = this };
timer.Elapsed += (s, ev) => pictureBox1.Image = camera.GetBitmap();
timer.start();

to

var timer = new System.Timers.Timer(1000 / 30) { SynchronizingObject = this };
timer.Elapsed += (s, ev) =>
{
    var oldImage = pictureBox1.Image;
    pictureBox1.Image = camera.GetBitmap();
    oldImage?.Dispose();
};
timer.Start();

It happens ''ArgumentException" the line below.

pictureBox1.Image = camera.GetBitmap(); // happens exception.
secile commented 6 months ago