简体   繁体   中英

how to webcam surrounded by a grid using in c#

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();
    }
}

}

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();

}

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();

     }        

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM