2014-01-17 4 views
1

AFheck.NET을 사용하여 디렉토리에 웹캠 이미지를 저장하려고합니다.그림 상자를 특정 디렉토리에 저장

여기 내 코드입니다 :

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

    FilterInfoCollection webcam; 
    VideoCaptureDevice cam; 
    Bitmap bitmap; 

    private void Form1_Load(object sender, EventArgs e) 
    {  
     webcam = new FilterInfoCollection(FilterCategory.VideoInputDevice); 
     cam = new VideoCaptureDevice(webcam[0].MonikerString); 
     cam.NewFrame += new NewFrameEventHandler(cam_NewFrame); 
     cam.Start(); 
    } 

    void cam_NewFrame(object sender, NewFrameEventArgs eventArgs) 
    { 
     bitmap = (Bitmap)eventArgs.Frame.Clone(); 
     pictureBox1.Image = bitmap; 
     pictureBox1.Image.Save("c:\\image\\image1.jpg"); 
    } 

하지만 난이 예외를 얻고있다 : 사전에

InvalidOperationException was unhandled Object is currently in use elsewhere. If you are using Graphic objects after the GetHdc method, call the RealseHdc method.

감사합니다. 아직 제대로로드되지 않은 이미지를 저장하려고하고 또한 크로스 스레딩에 직면하고있다

pictureBox1.Image = bitmap; 
pictureBox1.Image.Save("c:\\image\\image1.jpg"); 

:

+0

이것 좀 http://msmvps.com/blogs/peterritchie/archive/2008/01/28/quot-object-is-currently-in-use-elsewhere-quot-error.aspx를 타고 – Marek

답변

1

문제는이 라인입니다.

이 경우 솔루션은 드로잉 할 때 다중 스레드를 사용하지 않는 것입니다.

void cam_NewFrame(object sender, NewFrameEventArgs eventArgs) 
{ 
    bitmap = (Bitmap)eventArgs.Frame.Clone(); 
    pictureBox1.Image = bitmap; 

     try 
     { 
      this.Invoke((MethodInvoker)delegate 
      { 
       //saves image on its thread 

       pictureBox1.Image.Save("c:\\image\\image1.jpg"); 

      }); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(""+ex); 
     } 
} 
+0

완벽하게 작동했습니다. . 감사합니다. :) – user2517610