2016-09-12 15 views
1

こんにちは、opencvsharpプログラミングの新しいです。私はpictureboxを通して私のカメラビューをストリームするプログラムを作ろうとしています。 whileループは私のプログラムをクラッシュさせます。ループがなければ、画像は表示されますが、正常に動作します。私はopencvsharp3を使用しています。C#Opencvsharp 3カムコーダーのキャプチャウィンドウのフォーム

VideoCapture capture; 
    Mat frame; 
    Bitmap image; 


public Form1() 
{ 
    InitializeComponent(); 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    if (button1.Text.Equals("Start")) 
    { 
     frame = new Mat(); 
     capture = new VideoCapture(); 
     capture.Open(2); 
     capture.Read(frame); 
     Bitmap image = BitmapConverter.ToBitmap(frame); 
     while (true) 
     { 
      pictureBox1.Image = image; 
     } 
     button1.Text = "Stop"; 
    } 
    else 
    { 
     capture.Release(); 
     button1.Text = "Start"; 
    } 
} 

更新:GuidoGのコメントのおかげで、私はそれを把握することができました。

VideoCapture capture; 
    Mat frame; 
    Bitmap image; 
    private Thread camera; 
    int isCameraRunning = 0; 

    private void CaptureCamera() 
    { 
     camera = new Thread(new ThreadStart(CaptureCameraCallback)); 
     camera.Start(); 
    } 

    private void CaptureCameraCallback() 
    { 
     frame = new Mat(); 
     capture = new VideoCapture(); 
     capture.Open(2); 
     while (isCameraRunning == 1) 
     { 
      capture.Read(frame); 
      image = BitmapConverter.ToBitmap(frame); 
      pictureBox1.Image = image; 
      image = null; 
     } 

    } 
    public Form1() 
    { 
     InitializeComponent(); 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     if (button1.Text.Equals("Start")) 
      { 
      CaptureCamera(); 
      button1.Text = "Stop"; 
      isCameraRunning = 1; 
      } 
      else 
      { 
      capture.Release(); 
      button1.Text = "Start"; 
      isCameraRunning = 0; 
      } 
    } 

} 
} 
+0

whileループは無限ループです。同じ画像をpicturebox1に追加し続けます。 – GuidoG

答えて

1
private void CaptureCameraCallback() 
{ 
    frame = new Mat(); 
    capture = new VideoCapture(); 
    capture.Open(2); 
    while (isCameraRunning == 1) 
    { 
     capture.Read(frame); 
     image = BitmapConverter.ToBitmap(frame); 
     pictureBox1.Image = image; 
     image = null; 
    } 

} 

部分)が始まりBeginInvokeメソッド(に配置する必要があります。 デリゲート関数を作成する必要があります

関連する問題