0

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

Community
  • 1
  • 1
Balu
  • 137
  • 2
  • 11

1 Answers1

4

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.

DMAN
  • 471
  • 2
  • 8
  • Thanks for the reply. It didn't work. Still the same exception. – Balu Aug 07 '14 at 12:12
  • Then I guess the problem lies somewhere else. Could you share more of your code, please? – DMAN Aug 07 '14 at 12:14
  • Well, I have four classes Camera (is the main class), IpCam, ImageProcessing, and Streaming. I get the above Bitmap in IpCam class, here I have two properties (CurrentFrame, CurrentFrame2) to return these Bitmaps to the other classes. In the main class I have created a static object for the IpCam class. I am using this object in the other classes to to get the Bitmaps. Camera.ipCam.CurrentFrame for Streaming class, and Camera.ipCam.CurrentFrame2 for ImageProcessing class. – Balu Aug 07 '14 at 12:24
  • Check your properties: Do they really return different references or is there maybe a typo? Secondly, try cloning your image buffer in the property and not in the event where you receive it, but in your properties. This way you garantee, that nobody outside of IpCam has access to the same object instance. – DMAN Aug 07 '14 at 12:30
  • thanks for th comment. I tried the above step, imageProcessing is working fine, but the other one (live) is throwing invalidOperationException. – Balu Aug 07 '14 at 13:38
  • Do you have any loops which run on different threads in imageProcessing? Do you have more than one event which accesses the bitmap? Comment every single usage of the bitmap out until there's only one left and then run your project again. Then uncomment one usage again and see if it still runs. This way you will find the concurrent access which causes this exception. – DMAN Aug 07 '14 at 14:15
  • 1) I have only one loop inside the imageProcessing which tries to get the Bitmap from the IpCam class. 2) I don't have any other events which access the bitmap. – Balu Aug 07 '14 at 14:47