using AForge.Video; using AForge.Video.DirectShow; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Windows.Forms; namespace SOH.BasicSettings { public partial class frm_cjk_test : Form { public frm_cjk_test() { InitializeComponent(); } VideoCaptureDevice videoDevice; public frm_cjk_test(string sb, string fbl) : this() { videoDevice = new VideoCaptureDevice(sb); var videoCapabilities = videoDevice.VideoCapabilities; int h = int.Parse(fbl.Split('x')[1]); int w = int.Parse(fbl.Split('x')[0]); this.Width = w; this.Height = h; foreach (var vc in videoCapabilities) { if (vc.FrameSize.Width == w && vc.FrameSize.Height == h) videoDevice.VideoResolution = vc; } /* if ((snapshotCapabilities != null) && (snapshotCapabilities.Length != 0)) { videoDevice.ProvideSnapshots = true; videoDevice.SnapshotResolution = snapshotCapabilities[snapshotResolutionsCombo.SelectedIndex]; videoDevice.SnapshotFrame += new NewFrameEventHandler(videoDevice_SnapshotFrame); } EnableConnectionControls(false); */ videoSourcePlayer.VideoSource = videoDevice; videoSourcePlayer.Start(); } private void frm_cjk_test_Load(object sender, EventArgs e) { } private void Disconnect() { if (videoSourcePlayer.VideoSource != null) { // stop video device videoSourcePlayer.SignalToStop(); videoSourcePlayer.WaitForStop(); videoSourcePlayer.VideoSource = null; if (videoDevice.ProvideSnapshots) { //videoDevice.SnapshotFrame -= new NewFrameEventHandler(videoDevice_SnapshotFrame); } //EnableConnectionControls(true); } } private void frm_cjk_test_FormClosing(object sender, FormClosingEventArgs e) { Disconnect(); } public void getpic() { Bitmap bmp = null; bmp = videoSourcePlayer.GetCurrentVideoFrame(); if (bmp == null) { MessageBox.Show("捕获图像失败!", "提示"); return; } bmp.Save(Application.StartupPath + "\\" + Guid.NewGuid() + ".jpg", ImageFormat.Jpeg); } } }