2

I have developed an application the continuous read image stream from a DSLR camera.

while (!liveViewExit)
        {
            // Create a Memory Stream 
            stream = new IntPtr();

            // Get the bitmap image from the DSLR
            bmp = GetEvfImage(stream);


            if (bmp != null)
            {

                // Crop the image.
                picImage = (System.Drawing.Image)bmp.Clone(new Rectangle(10, 0, 492, 768), bmp.PixelFormat);
                try
                {
                    if (picImage != null)
                        this.picLiveView.Image = (System.Drawing.Image)picImage.Clone();
                }
                catch (Exception ex)
                {
                    Utility.HandleError(ex);
                }


            }
        }

After running a while, I have this error for this line of code:

   this.picLiveView.Image = (System.Drawing.Image)picImage.Clone();


Object is currently in use elsewhere.(   at System.Drawing.Image.get_FrameDimensionsList()
   at System.Drawing.ImageAnimator.CanAnimate(Image image)
   at System.Drawing.ImageAnimator.ImageInfo..ctor(Image image)
   at System.Drawing.ImageAnimator.Animate(Image image, EventHandler onFrameChangedHandler)
   at System.Windows.Forms.PictureBox.Animate(Boolean animate)
   at System.Windows.Forms.PictureBox.Animate()
   at System.Windows.Forms.PictureBox.InstallNewImage(Image value, ImageInstallationType installationType)
   at System.Windows.Forms.PictureBox.set_Image(Image value)

I think the picLiveView PictureBox control is not yet ready to accept new image. Any idea how on detect if PictureBox is still in used.

// Added:

It is a single thread. I think the picturebox is not fast enough to process the picture object in the while loop.

Jainendra
  • 24,713
  • 30
  • 122
  • 169
billy_flow
  • 81
  • 4
  • 9
  • possible duplicate of [InvalidOperationException - object is currently in use elsewhere - red cross](http://stackoverflow.com/questions/1060280/invalidoperationexception-object-is-currently-in-use-elsewhere-red-cross) – John May 20 '11 at 10:05

2 Answers2

2

Are multiple threads updating the picLiveView image? If so that would explain this problem. Just use one thread instead, and serialize the update - alternatively you could use a lock to access picLiveView:

private readonly object myLock = new object();
...


if (picImage != null)
{
   lock(myLock)
   {
      this.picLiveView.Image = (System.Drawing.Image)picImage.Clone();
   }
} 
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • 3
    Locking won't help, especially if it's displayed on the UI. GDI+ is inherently non-thread-safe. – SLaks Jan 28 '11 at 16:32
0

I know am late...but try this, if someone have same issue..

if (bmp != null)
            {

                // Crop the image.
                picImage = (System.Drawing.Image)bmp.Clone(new Rectangle(10, 0, 492, 768), bmp.PixelFormat);

    **Bitmap img = new Bitmap(picImage);
    picImage.Dispose();
    picImage = null;**

      try
                {
                    if (picImage != null)
                        **this.picLiveView.Image = img;**
                }
                catch (Exception ex)
                {
                    Utility.HandleError(ex);
                }


            }
Wolf5370
  • 1,374
  • 11
  • 12
zb ss
  • 1