I have an app that contains a picture box which updates images from a live camera every time a new image gets loaded into the camera buffer. My problem is that whenever I’m getting this live feed, the whole app becomes very slow and (sometimes) unresponsive. I have a separate thread which basically does all the imaging steps and then puts the new image in the picture box. I’m kind of stuck on how to fix the issue and I’m wondering if anyone has any ideas? I’m not sure what kind of code you need but here is the ImageUpdated event that gets the image and sticks it on the PictureBox. Thanks for any help!
void CurrentCamera_ImageUpdated(object sender, EventArgs e)
{
try
{
lock (CurrentCamera.image)
{
if (CurrentCamera != null && CurrentCamera.image != null && !changeCam)
{
videoImage = CurrentCamera.videoImage;
if (CurrentCamera.videoImage != null && this.IsHandleCreated)
{
Bitmap tmp = new Bitmap(CurrentCamera.image.Width, CurrentCamera.image.Height);
//Creates a crosshair on the image
using (Graphics g = Graphics.FromImage(tmp))
{
g.DrawImage(CurrentCamera.image, new Point(0, 0));
g.DrawLine(crosshairPen, new Point(CurrentCamera.image.Width / 2, 0), new Point(CurrentCamera.image.Width / 2, (CurrentCamera.image.Height)));
g.DrawLine(crosshairPen, new Point(0, CurrentCamera.image.Height / 2), new Point((CurrentCamera.image.Width), CurrentCamera.image.Height / 2));
g.DrawEllipse(crosshairPen, (CurrentCamera.image.Width / 2) - crosshairRadius, (CurrentCamera.image.Height / 2) - crosshairRadius, crosshairRadius * 2, crosshairRadius * 2);
}
pictureBox1.BeginInvoke((MethodInvoker)delegate
{
pictureBox1.Image = tmp;
});
}
}
}
}
catch { }
}
In addition to the comment, I think that this might speed up a little bit. First, if the image is’t set on the pictureBox1, then don’t set another. Second, because of using pictureBox, it is optimized not to have flicker so you can draw the crosshair in your PaintEvent method handler for the pictureBox1. This is a little bit improved code that avoids double Bitmap drawing, then you should speed up as much as you can
CurrentCamera_ImageUpdatedmethod execution because new images come very fast, C# code can’t handle that much drawing even on a high performance machine. Start from here, and keep on improving.