2011-05-26 2 views
10

मैं अपने कैमरे का उपयोग करके एक वेबकैम फ़ीड कैप्चर करना चाहता हूं। इसके लिए मैं 2 संदर्भों का उपयोग कर रहा हूं: AForge.Video.dll और AForge.Video.DirectShow.dllसी # में Aforge.NET का उपयोग करके वेबकैम स्ट्रीम प्राप्त करें और WPF

Here's एक टुकड़ा मैंने पाया:

public FilterInfoCollection CamsCollection; 
public VideoCaptureDevice Cam = null; 

void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs) 
{ 
    frameholder.Source = (Bitmap)eventArgs.Frame.Clone(); 
    /*^
    * Here it cannot convert implicitly from System.Drawing.Bitmap to 
    * System.Windows.Media.ImageSource 
    */ 

} 

private void startcam_Click(object sender, RoutedEventArgs e) 
{ 
    CamsCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice); 

    Cam = new VideoCaptureDevice(CamsCollection[1].MonikerString); 
    Cam.NewFrame += new NewFrameEventHandler(Cam_NewFrame); 
    Cam.Start(); 
} 

private void stopcam_Click(object sender, RoutedEventArgs e) 
{ 
    Cam.Stop(); 
} 

}

वे एक PictureBox का उपयोग तख्ते प्रदर्शित करने के लिए। जैसा कि मैं डब्ल्यूपीएफ में काम कर रहा हूं, मैंने this

यहां बताया गया है कि मेरा कोड वर्तमान में कैसा दिखता है।

public FilterInfoCollection CamsCollection; 
public VideoCaptureDevice Cam = null; 


void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs) 
{ 

    System.Drawing.Image imgforms = (Bitmap)eventArgs.Frame.Clone(); 


    BitmapImage bi = new BitmapImage(); 
    bi.BeginInit(); 

    MemoryStream ms = new MemoryStream(); 

    imgforms.Save(ms, ImageFormat.Bmp); 

    ms.Seek(0, SeekOrigin.Begin); 
    bi.StreamSource = ms; 
    frameholder.Source = bi; 
    /*^runtime error here because `bi` is occupied by another thread. 
    */ 
    bi.EndInit(); 
} 

private void startcam_Click(object sender, RoutedEventArgs e) 
{ 

    CamsCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice); 

    Cam = new VideoCaptureDevice(CamsCollection[1].MonikerString); 
    Cam.NewFrame += new NewFrameEventHandler(Cam_NewFrame); 
    Cam.Start(); 
} 

private void stopcam_Click(object sender, RoutedEventArgs e) 
{ 
    Cam.Stop(); 
} 

उत्तर

7

Edit1:

यहाँ एक सिल्वरलाइट 4 ट्यूटोरियल है एक विस्तृत विवरण देखने के लिए मेरी blogpost एक ही विषय पर। उम्मीद और मैं एफपीएस में किसी भी बूंद के बिना अच्छा प्रदर्शन प्राप्त के रूप में

void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs) 
    { 

     System.Drawing.Image imgforms = (Bitmap)eventArgs.Frame.Clone(); 

     BitmapImage bi = new BitmapImage(); 
     bi.BeginInit(); 

     MemoryStream ms = new MemoryStream(); 
     imgforms.Save(ms, ImageFormat.Bmp); 
     ms.Seek(0, SeekOrigin.Begin); 

     bi.StreamSource = ms; 
     bi.EndInit(); 

     //Using the freeze function to avoid cross thread operations 
     bi.Freeze(); 

     //Calling the UI thread using the Dispatcher to update the 'Image' WPF control   
     Dispatcher.BeginInvoke(new ThreadStart(delegate 
     { 
      frameholder.Source = bi; /*frameholder is the name of the 'Image' WPF control*/ 
     }));  

    } 

अब इसे चलाता:


मैं एक म्युटेक्स रूप Dispatcher वर्ग का उपयोग कर त्रुटि ठीक कर दी।

1

आप, Silverlight समर्थन करने के लिए वेब या स्टैंडअलोन या WP7 के लिए यह हो चाहते हैं, आप WPF के साथ शुरू नहीं करना चाहिए, के रूप में WPF से कई सुविधाओं सिल्वरलाइट में कमी कर रहे हैं।

http://www.silverlightshow.net/items/Capturing-the-Webcam-in-Silverlight-4.aspx

+0

हे। धन्यवाद ... अभी तक मुझे उस एप्लिकेशन के लिए बहुत सारी छवि प्रसंस्करण की आवश्यकता होगी जिस पर मैं काम कर रहा हूं। तो मैं इसके लिए अफोर का उपयोग कर रहा हूं (यानी वीडियो कैप्चर करने के लिए और सभी) – Sagar

संबंधित मुद्दे