Search code examples
c#concurrencybitmapinvalidoperationexception

How to use Bitmap for live and processing at a time?


I get Bitmap (25 per 1 second) from a camera using imageAvailable envent. I have to show it on a GUI and also procees that Bitmap. So I am using two bitmaps, one for the live and the other for the processing. The problem is I am getting the invalidOperationException (and it saying "Object is in use currently elsewhere") while I am processing "currentFrame2". For live I am uisng "currentFrame", there is nor problem.

currentFrame = (Bitmap)e.ImageBuffer.Bitmap.Clone();
currentFrame2 = currentFrame;

I was looking at

  1. Bitmap.Save "Object is currently in use elsewhere" Threading Issue
  2. Accessing an object on 2 threads at the same time

The above links couldn't help.

Please give me a suggesion to overcome this problem.

Thank you


Solution

  • Try:

    currentFrame = (Bitmap)e.ImageBuffer.Bitmap.Clone();
    currentFrame2 = (Bitmap)e.ImageBuffer.Bitmap.Clone();
    

    The problem is that currentFrame and currentFrame2 are referencing the same object. By using a copy for the second object reference, you shouldn't get an exception anymore.