欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實現(xiàn)調(diào)用本機攝像頭實例

 更新時間:2014年08月27日 09:11:16   投稿:shichen2014  
這篇文章主要介紹了C#實現(xiàn)調(diào)用本機攝像頭的方法,可以實現(xiàn)調(diào)用本機攝像頭進(jìn)行拍照,具有不錯的實用價值,需要的朋友可以參考下

本文實例源自一個項目,其中需要調(diào)用本機的攝像頭進(jìn)行拍照,分享給大家供大家參考之用。具體步驟如下:

硬件環(huán)境:聯(lián)想C360一體機,自帶攝像頭

編寫環(huán)境:vs2010

語言:C# WPF

實現(xiàn)步驟:

下載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();

關(guān)閉攝像頭:

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);
  }
}

項目中要求是攝像頭處于監(jiān)控狀態(tài),拍照后畫面固定存儲,不滿意可以清空再次進(jìn)行拍照,直到滿意為止。

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

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

2)image控件的source已經(jīng)綁定,但是照片需要清空刪除該照片資源,系統(tǒng)提示的大致意思是資源已經(jīng)被占用無法刪除。解決途徑:

聲明:

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#程序設(shè)計有所幫助。

相關(guān)文章

最新評論