在WINDOWS下做WEBCAM 影像的擷取,比較常見的就是用DirectShow或EmguCV,
最近突然發現有一個還不錯的open souce可以擷取webcam影像,而且大小不到50mb
它就是 aforge,這次就決定用它來示範怎麼做影像擷取與存圖!
編譯環境:
windows 7
visual studio 2012 express
aforge .net Framework-2.2.5
1.到 aforge 官網下載最新版.net Framework 並安裝
2.開啟新專案,選擇 Windows Form Application
PROJECT -> Add Reference -> Browse
到 AForge.NET 的安裝目錄下,AForge.NET\Framework\Release,
將 AForge.Video.DirectShow.dll 和 AForge.Video.dll 加進來
4.UI介面設計
5.Code
// Created by vince on 13/11/10. // Copyright (c) 2013年 vince. All rights reserved. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using AForge.Video; //記得加入1 using AForge.Video.DirectShow; //記得加入2 namespace aforge_webcam_tutorial { public partial class Form1 : Form { public VideoCaptureDevice cam = null; //global public FilterInfoCollection usbCams; //global Bitmap image_from_cam; //global public Form1() { InitializeComponent(); } void got_frame(object sender, NewFrameEventArgs eventArgs) { image_from_cam = (Bitmap)eventArgs.Frame.Clone(); pictureBox1.Image = image_from_cam; } private void startButton_Click(object sender, EventArgs e) { usbCams = new FilterInfoCollection(FilterCategory.VideoInputDevice); cam = new VideoCaptureDevice(usbCams[0].MonikerString); cam.NewFrame += new NewFrameEventHandler(got_frame);//註冊事件(got_frame) cam.Start(); } private void stopButton_Click(object sender, EventArgs e) { cam.Stop(); } private void saveButton_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); if (sfd.ShowDialog(this) == DialogResult.Cancel) //按下取消鍵時返回 { return; } image_from_cam.Save(sfd.FileName);//存圖,副檔名記得給他.bmp } } }
請問大師知道c# 如何讀取 binary image file ? 必須依照指定 width and height= 640x480去讀取, 可否建議的code? 感謝
回覆刪除