C#實現調用本機網路攝影機執行個體

來源:互聯網
上載者:User

標籤:程式   using   button   sys   語言   col   清空   bsp   content   

本文執行個體源自一個項目,其中需要調用原生網路攝影機進行拍照,分享給大家供大家參考之用。具體步驟如下:

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

編寫環境:vs2010

語言:C# WPF


實現步驟:

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

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

在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#程式設計有所協助。

除聲明外, 跑步客文章均為原創,轉載請以連結形式標明本文地址
  C#實現調用本機網路攝影機執行個體

本文地址:  http://www.paobuke.com/develop/c-develop/pbk23585.html






相關內容C#實現小截屏軟體功能C# 利用IRawPixels介面遍曆柵格資料C#實現JSON字串序列化與還原序列化的方法C#利用正則判斷輸入是否為純數字、容器類
C#編寫ActiveX網頁控制項C#中如何擷取檔案表徵圖常用C#Regex匯總介紹C#中非同步回呼函數用法執行個體

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.