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 AForge.Video; using AForge.Video.DirectShow; using System.Drawing.Imaging; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { private bool DeviceExist = false; private FilterInfoCollection videoDevices; private VideoCaptureDevice videoSource = null; public Form1() { InitializeComponent(); } private void getCamList()//ค้นหาว่ามี webcam ไหม { try { videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); comboBox1.Items.Clear(); if (videoDevices.Count == 0) throw new ApplicationException(); DeviceExist = true; foreach (FilterInfo device in videoDevices) { comboBox1.Items.Add(device.Name); } comboBox1.SelectedIndex = 0; //make dafault to first cam } catch (ApplicationException) { DeviceExist = false; comboBox1.Items.Add("No capture device on your system"); } } private void button1_Click(object sender, EventArgs e)//Refresh webcam { label1.Text = "Refresh"; getCamList(); } private void Form1_Load(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e)//Start webcam { if (button2.Text == "&Start") { if (DeviceExist) { videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString); videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame); CloseVideoSource(); videoSource.DesiredFrameSize = new Size(); //videoSource.DesiredFrameRate = 10; videoSource.Start(); label1.Text = "Device running..."; button2.Text = "&Stop"; timer1.Enabled = true; } else { label1.Text = "Error: No Device selected."; } } else { try { if (videoSource.IsRunning) { timer1.Enabled = false; CloseVideoSource(); label1.Text = "Device stopped."; button2.Text = "&Start"; } } catch { videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString); videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame); CloseVideoSource(); videoSource.DesiredFrameSize = new Size(); //videoSource.DesiredFrameRate = 10; videoSource.Start(); label1.Text = "Device running..."; button2.Text = "&Stop"; timer1.Enabled = true; } } } private void timer1_Tick(object sender, EventArgs e) { label1.Text = "Device running... " + videoSource.FramesReceived.ToString() + " FPS"; } private void CloseVideoSource() { if (!(videoSource == null)) if (videoSource.IsRunning) { videoSource.SignalToStop(); videoSource = null; } } private void video_NewFrame(object sender, NewFrameEventArgs eventArgs) { Bitmap img = (Bitmap)eventArgs.Frame.Clone(); //do processing here pictureBox1.Image = img; } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { CloseVideoSource(); } } }
public static void SaveImageCapture(System.Drawing.Image image) { SaveFileDialog s = new SaveFileDialog(); s.FileName = "Image"; // Default file name s.DefaultExt = ".Jpg"; // Default file extension s.Filter = "Image (.jpg)|*.jpg"; // Filter files by extension // Show save file dialog box // Process save file dialog box results if (s.ShowDialog()==DialogResult.OK) { // Save Image s.FileName = new FileName(); s.FileName = "Image"; string filename = s.FileName; FileStream fstream = new FileStream(filename, FileMode.Create); image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg); fstream.Close(); } }
เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง