1

I'm working on making a webcam with c# using the A Forge.NET library and I want to open the webcam to take a rubix cube picture. I use a picture box to handle the webcam frames and I wanted to make a grid 3*3 inside the picture box.

It works but after 3 sec of the running it generates an exception :

g = Graphics.FromImage(videoBox.Image);  ----> InvalidOperationException

Here is my code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;

namespace WebcamTester
{
    public partial class Form1 : Form
    {
        public Form1()
        {
        InitializeComponent();
        }

    private FilterInfoCollection webcam;
    private VideoCaptureDevice cam;
    private Bitmap bit = new Bitmap(640, 480);
    private Graphics g;
    private int cellsNumber;
    private int cellSize;

    private void Form1_Load(object sender, EventArgs e)
    {
        webcam = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        foreach (FilterInfo VideoCaptureDevice in webcam)
        {
            comboBox1.Items.Add(VideoCaptureDevice.Name);
        }
        comboBox1.SelectedIndex = 0;

        cam = new VideoCaptureDevice(webcam[comboBox1.SelectedIndex].MonikerString);
        cam.NewFrame += new NewFrameEventHandler(cam_NewFrame);
        cam.Start();

    }


    void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        bit = (Bitmap)eventArgs.Frame.Clone();
        videoBox.Image = bit;
        g = Graphics.FromImage(videoBox.Image);
        Pen p = new Pen(Color.Black, 2);

        cellSize = 100;
        cellsNumber = 4;

        for (int y = 0; y <= cellsNumber; ++y)
        {
            g.DrawLine(p, 0, y * cellSize, cellsNumber * cellSize, y * cellSize);
        }

        for (int x = 0; x <= cellsNumber; ++x)
        {
            g.DrawLine(p, x * cellSize, 0, x * cellSize, cellsNumber * cellSize);
        }

    }

    private void button2_Click(object sender, EventArgs e)
    {
        saveFileDialog1.InitialDirectory = @"d:\picture";
        cam.Stop();
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {

            videoBox.Image.Save(saveFileDialog1.FileName);
            cam.Start();
        }

        else
            cam.Start();
    }
}

}

2

2 Answers 2

0

This exception is related to ˜Object is currently in use elsewhere.˜ Maybe its because is not released...

You should try:

    (...)

    for (int x = 0; x <= cellsNumber; ++x)
    {
        g.DrawLine(p, x * cellSize, 0, x * cellSize, cellsNumber * cellSize);
    }
    g.Dispose();

}
2
  • If this does not help, you should try use Feb 9, 2013 at 23:08
  • still generating the same exception after 3 sec :/
    – AouledIssa
    Feb 9, 2013 at 23:12
0

If this does not help, you should try use

private object Lock = new object();

  lock (Lock) {

      using (var g = Graphics.FromImage(videoBox.Image) {  

        Pen p = new Pen(Color.Black, 2);

        cellSize = 100;
        cellsNumber = 4;

        for (int y = 0; y <= cellsNumber; ++y)
        {
            g.DrawLine(p, 0, y * cellSize, cellsNumber * cellSize, y * cellSize);
        }

        for (int x = 0; x <= cellsNumber; ++x)
        {
            g.DrawLine(p, x * cellSize, 0, x * cellSize, cellsNumber * cellSize);
        }

        g.Dispose();

     }        

  }
5
  • This is incorrect on so many levels: g is out of scope down where you want to Dispose() it. Feb 9, 2013 at 23:15
  • -1 thiagovinicius, I would suggest looking up what using(){} does so calling this g.Dispose(); defeats the purpose of the using clause
    – MethodMan
    Feb 9, 2013 at 23:19
  • g is out of scope and if i place it inside the lock boject it still generating the same error
    – AouledIssa
    Feb 9, 2013 at 23:50
  • you already tried to do the same thing you did with "bit" in videoBox.Image too, example: (Bitmap)videoBox.Image.Clone() It is something with thread, a thread is using the image, and another is trying to modifies it Feb 18, 2013 at 15:12
  • you can try put a sleep(); in cam_NewFrame maybe... just wondering.... I am not testing it, just wondering to find a solution Feb 18, 2013 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.