C#調用本機網路攝影機

來源:互聯網
上載者:User

標籤:des   winform   style   blog   http   io   color   ar   os   

原文:C#調用本機網路攝影機

這段時間一個小項目中需要調用原生網路攝影機進行拍照,網上搜集了一些資料以及解決的一些小問題,在此記錄以便後續使用。

硬體環境:聯想C360一體機,內建網路攝影機

編寫環境:vs2010

語言:C# WPF

 

下載AForge類庫,並添加引用:

using AForge;using AForge.Controls;using AForge.Video;using AForge.Video.DirectShow;using Size = System.Drawing.Size;
View Code

 

在xaml介面中添加VideoSourcePlayer控制項,此次稍微解釋如何添加外來控制項:

在工具箱中添加新的選項卡,右鍵添加選擇項,瀏覽選擇控制項dll確定,引用控制項即可添加到工具箱中。

 

枚舉所有的網路攝影機:

FilterInfoCollection videoDevices;videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);foreach (FilterInfo device in videoDevices)                {                    //可以做出處理                }

 

串連網路攝影機:

聲明:FileterInfo info;
info = videoDevices[0];//選取第一個,此處可作靈活改動

VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[info.MonikerString); videoSource.DesiredFrameSize = new System.Drawing.Size(214, 281); videoSource.DesiredFrameRate = 1; videoSourcePlayer.VideoSource = videoSource; videoSourcePlayer.Start();

 

關閉網路攝影機:

videoSourcePlayer.SignalToStop();            videoSourcePlayer.WaitForStop();

 

拍照:

if (videoSourcePlayer.IsRunning)                {
            string path = "e:\" BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( videoSourcePlayer.GetCurrentVideoFrame().GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); PngBitmapEncoder pE = new PngBitmapEncoder(); pE.Frames.Add(BitmapFrame.Create(bitmapSource)); string picName = path + "paizhao" + ".jpg"; if (File.Exists(picName)) { File.Delete(picName); } using (Stream stream = File.Create(picName)) { pE.Save(stream); } }

 

 

項目中要求是網路攝影機處於監控狀態,拍照後畫面固定儲存,不滿意可以清空再次進行拍照,直到滿意為止。

做法是在videoSourcePlayer的上面添加一個image控制項,因為項目是WPF做的,所有照片顯示只能添加image控制項,有兩點需要注意:

1)WPF引用winform控制項需要使用WindowsFormsHost控制項,所以監控視頻和照片顯示時是控制項WindowsFormsHost和image控制項的顯示和隱藏,此處走了一段彎路所以記錄下來。

2)image控制項的source已經綁定,但是照片需要清空刪除該照片資源,系統提示的大致意思是資源已經被佔用無法刪除。解決途徑:

聲明:BitmapImage bmi = new System.Windows.Media.Imaging.BitmapImage();

 

使用時:bmi.BgeinInit();

bmi.UriSource = new Uri(picName);

bmi.CacheOption = BitmapCacheOption.OnLoad;

bmi.EndInit();

綁定:this.image.Source = bmi;

 

C#調用本機網路攝影機

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.