2013-02-09 42 views
1

我正在使用A Forge.NET庫製作一個網絡攝像頭與C#,我想打開攝像頭採取rubix立方體圖片。我使用圖片框來處理網絡攝像頭幀,並且我想在圖片框內製作3 * 3的網格。如何網絡攝像頭包圍在網格中使用C#

它的工作原理,但之後的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(); 
    } 
} 

}

+1

[你應該總是調用Dispose方法以釋放圖形和由FromImage方法創建的相關資源。] (http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx) – 2013-02-09 22:51:38

+0

也有可能您從圖像盒創建圖形時收到一幀圖像。嘗試使用本地方法處理所有對象,並僅使用鎖定將結果設置爲ui。 – 2013-02-09 22:55:26

回答

0

此異常有關~Object是目前正在其他地方使用。〜 也許是因爲沒有發佈...

你應該嘗試:

(...) 

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

} 
+0

如果這沒有幫助,你應該嘗試使用 – thiagovinicius 2013-02-09 23:08:15

+0

仍然在3秒後產生相同的異常:/ – 2013-02-09 23:12:27

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

    }   

    } 
+0

在如此多的級別上這是不正確的:g在你想要Dispose()它的範圍之外。 – 2013-02-09 23:15:45

+0

-1 thiagovinicius,我會建議查找什麼'使用(){}'這樣做調用這個'g.Dispose();'擊敗使用子句的目的 – MethodMan 2013-02-09 23:19:02

+0

g是超出範圍,如果我把它放在鎖住它仍然生成相同的錯誤 – 2013-02-09 23:50:30

相關問題