1

我正在使用 A Forge.NET 库使用 c# 制作网络摄像头,我想打开网络摄像头来拍摄 rubix 立方体图片。我使用图片框来处理网络摄像头框架,我想在图片框内制作一个 3*3 的网格。

它可以工作,但在运行 3 秒后会产生异常:

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

这是我的代码:

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

}

4

2 回答 2

0

如果这没有帮助,您应该尝试使用

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

     }        

  }
于 2013-02-09T23:14:49.743 回答
0

这个异常与 ~Object 当前在别处使用有关。~ 可能是因为没有释放...

你应该试试:

    (...)

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

}
于 2013-02-09T23:04:32.543 回答