0

UPDATE: I have changed the code and removed anything that could be writing simultaneously with pictureBox1 and I still get the same error but at a different place. I will attach a picture of the error. I have updated the code.

Okay so I have tried looking everywhere for answers to this error but I got nowhere. I found answers such as g.Dispose(); and "acquiring lock on an image". However, my project is kind of different because it is not a still image. It crashes after around 504 frames. I will attach a picture of where I get the error and I will link the code as well for the webcam display. I have no idea how is pictureBox1 is being used somewhere else when all I am doing in the Start button is just starting the webcam and saving frames to a folder. I am using AForge library for webcam access. And when I simply press the Start webcam button and wait a couple of minutes I get this error:

System.InvalidOperationException was unhandled HResult=-2146233079
Message=Object is currently in use elsewhere. Source=System.Drawing StackTrace: at System.Drawing.Graphics.CheckErrorStatus(Int32 status) at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height) at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect) at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe) at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer) at System.Windows.Forms.Control.WmPaint(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) InnerException:

Picture of error message: https://i.stack.imgur.com/s4Vts.jpg

Code:

//Initializing Webcam
private FilterInfoCollection webcam;
private VideoCaptureDevice cam;


 //Start Webcam 
    private void button1_Click(object sender, EventArgs e)
    {
        cam = new VideoCaptureDevice(webcam[comboBox1.SelectedIndex].MonikerString);
        cam.NewFrame += new NewFrameEventHandler(Cam_NewFrame);
        cam.Start();
    }

    //Displaying Video from webcam in Picture box
    void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        Bitmap image = (Bitmap)eventArgs.Frame.Clone();
        Bitmap image1 = (Bitmap)eventArgs.Frame.Clone();
        pictureBox1.Image = image;

        //Drawing lines across the pictureBox
        g1 = Graphics.FromImage(image);

        Pen gridPen = new Pen(Color.Black, 5);
        g1.DrawLine(gridPen, 0, 5, 805, 5);
        g1.DrawLine(gridPen, 0, 100, 805, 100);
        g1.DrawLine(gridPen, 0, 195, 805, 195);
        g1.DrawLine(gridPen, 0, 290, 805, 290);
        g1.DrawLine(gridPen, 0, 385, 805, 385);
        g1.DrawLine(gridPen, 0, 480, 805, 480);
    }
  • `I get that error "Object is in use elsewhere"` What is the exact error (type and `StackTrace`)? What line of code throws it? – mjwills Jun 06 '18 at 23:22
  • What is this line of code trying to do - `pictureBox1.Image = (Bitmap)pictureBox1.Image.Clone();`? – mjwills Jun 06 '18 at 23:22
  • @mjwills Mb. I have edited the question and added the exact error. – Salma Kassem Jun 06 '18 at 23:39
  • @mjwills This line makes the video appear in the picturebox. I tried using only one of the codes since I thought `pictureBox1.Image = bit;` would be enough. But it crashes right away when I comment it. – Salma Kassem Jun 06 '18 at 23:46
  • Have a squiz at https://stackoverflow.com/a/45129088/34092 . – mjwills Jun 06 '18 at 23:54
  • I have tried the solution in the post. However I used `bit.Dispose();` as I don't have a graphics object. But I still get the error on the line of `pictureBox1.Image = (Bitmap)pictureBox1.Image.Clone();`.. – Salma Kassem Jun 07 '18 at 00:16
  • You will need to show us your code with `Dispose` in it - since your question doesn't have one. – mjwills Jun 07 '18 at 00:18
  • @mjwills I have added the `bit.Dispose()` part in my code now. Sorry for this inconvenience. – Salma Kassem Jun 07 '18 at 00:20
  • Can you reproduce it if you comment out the bit.Save() line? – Victor Havin Jun 07 '18 at 00:23
  • @VictorHavin after commenting out that line and writing `bit.Dispose();` right after `pictureBox1.Image = (Bitmap)pictureBox1.Image.Clone();`I got a different error that causes a crash. "Out of Memory" – Salma Kassem Jun 07 '18 at 00:34
  • You create a new thread for each frame, which runs forever. Say, 30 threads per second. Of course you are out of memory. What is the purpose of this thread? Try to comment out the "new Thread" line. You need a different delay mechanism. – Victor Havin Jun 07 '18 at 00:51
  • @VictorHavin Tbh I am using this threading technique as a way to get over the error of object in use. But the real project should work in real time. Image processing right from the webcam video. – Salma Kassem Jun 07 '18 at 01:12
  • 1
    So, since you do not get the "object in use" error anymore, can you remove the threading and see if it solves the memory problem? If you want it to work in real time, you can't save each frame into a file. You will have to come up with some sort of buffering. The "object" in use problem could be happening because you do not finish writing in time for another frame. – Victor Havin Jun 07 '18 at 01:30
  • @VictorHavin Hello again Victor. So I have done as you said and removed the Threading technique but I still got the object in use problem. Not sure what to do to finish writing an image in time for another frame. Any suggestions? – Salma Kassem Jun 08 '18 at 14:22
  • Allow me to clarify: Are you saying you removed both thread creation and file writing from your event handler and still getting "object in use" error? Can you post the updated code, if possible? – Victor Havin Jun 08 '18 at 15:12
  • @VictorHavin Yes it is exactly like you said. :( I have updated the question with the new modified code. Also please check the imgur link to get an exact idea of where the error appears. – Salma Kassem Jun 08 '18 at 23:48
  • Salma, it is a little different now. This time the exception happens in the Picture Control. Last time, if I remember correctly, it was in your code. There was a line number on the stack. To make debugging easier, try to minimize the number of changes you make in the code. Try to comment out all non-essential code. Line drawing etc. It is also not clear what image1 is for. Try leaving just two lines:Frame.Clone() and image assignment - then let me know what happens. – Victor Havin Jun 09 '18 at 00:57
  • One more observation: You are using a 3d party video library from AForge, I suppose. I am not an expert here and I never used this library myself. However, copying individual video frames as images looks like a very expensive operation. Most libraries provide video streaming facilities for directing video stream into a window. You probably should look into this. – Victor Havin Jun 09 '18 at 01:13
  • 1
    @VictorHavin Looks like I have saved the problem :D I used `image.LockBits` and `image.UnlockBits()`. It fixed everything :D – Salma Kassem Jun 09 '18 at 22:59
  • Glad to hear it. – Victor Havin Jun 10 '18 at 22:34

0 Answers0