DirectShow.NET2.0でwebcamキャプチャ、JPEG保存
しばらく見ないうちに2.0になって神ライブラリになってた。神。C#からカメラ使うのほんと大変だったのよ…
http://directshownet.sourceforge.net/
DirectShow.NETとsourceforgeにあったサンプルに入ってたCapture.csを一緒に使った。
環境はVS2008+C#3.0
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using DirectShowLib; using SnapShot; using System.Runtime.InteropServices; using System.Drawing.Imaging; namespace CaptureTest { public partial class Form1 : Form { private Capture cam; public Form1() { InitializeComponent(); cam = new Capture(0, 640, 480, 24, panelPreview); // device, x, y, 24FPS, preview pictureBoxCaptured.SizeMode = PictureBoxSizeMode.Zoom; } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { cam.Dispose(); } private void buttonCapture_Click(object sender, EventArgs e) { IntPtr m_ip = cam.Click(); Bitmap b = new Bitmap(cam.Width, cam.Height, cam.Stride, PixelFormat.Format24bppRgb, m_ip); b.RotateFlip(RotateFlipType.RotateNoneFlipY); // 上下逆さになる事あるので pictureBoxCaptured.Image = b; } private void buttonSaveFile_Click(object sender, EventArgs e) { pictureBoxCaptured.Image.Save("captured.jpg", ImageFormat.Jpeg); // jpeg保存 } } }