C# DirectShow预览摄像头并截图
public partial class FormWinCamera : Form, ISampleGrabberCB { private int _previewWidth = 640; private int _previewHeight = 480; private int _previewStride = 0; private int _previewFPS = 30; private volatile bool isGrab = false; IVideoWindow videoWindow = null; IMediaControl mediaControl = null; IFilterGraph2 graphBuilder = null; ICaptureGraphBuilder2 captureGraphBuilder = null; DsROTEntry rot = null; public FormWinCamera() { InitializeComponent(); } private void FormWinCamera_Load(object sender, EventArgs e) { } private void StartCamera() { DsDevice[] devices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice); if (devices.Length == 0) { MessageBox.Show("No USB webcam connected"); button3.Text = "Start Webcam"; return; } else { CaptureVideo(devices[0]); } } private void StopCamera() { button3.Text = "Start Webcam"; CloseInterfaces(); } public void CaptureVideo(DsDevice device) { pictureBox1.Image = null; int hr = 0; IBaseFilter sourceFilter = null; ISampleGrabber sampleGrabber = null; try { // Get DirectShow interfaces GetInterfaces(); // Attach the filter graph to the capture graph hr = captureGraphBuilder.SetFiltergraph(graphBuilder); DsError.ThrowExceptionForHR(hr); // Use the system device enumerator and class enumerator to find // a video capture/preview device, such as a desktop USB video camera. sourceFilter = SelectCaptureDevice(device); // Add Capture filter to graph. hr = graphBuilder.AddFilter(sourceFilter, "Video Capture"); DsError.ThrowExceptionForHR(hr); // Initialize SampleGrabber. sampleGrabber = new SampleGrabber() as ISampleGrabber; // Configure SampleGrabber. Add preview callback. ConfigureSampleGrabber(sampleGrabber); // Add SampleGrabber to graph. hr = graphBuilder.AddFilter(sampleGrabber as IBaseFilter, "Frame Callback"); DsError.ThrowExceptionForHR(hr); // Configure preview settings. SetConfigParams(captureGraphBuilder, sourceFilter, _previewFPS, _previewWidth, _previewHeight); // Render the preview hr = captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, sourceFilter, (sampleGrabber as IBaseFilter), null); DsError.ThrowExceptionForHR(hr); SaveSizeInfo(sampleGrabber); // Set video window style and position SetupVideoWindow(); // Add our graph to the running object table, which will allow // the GraphEdit application to "spy" on our graph rot = new DsROTEntry(graphBuilder); // Start previewing video data hr = mediaControl.Run(); DsError.ThrowExceptionForHR(hr); } catch { MessageBox.Show("An unrecoverable error has occurred."); } finally { if (sourceFilter != null) { Marshal.ReleaseComObject(sourceFilter); sourceFilter = null; } if (sampleGrabber != null) { Marshal.ReleaseComObject(sampleGrabber); sampleGrabber = null; } } } public void CaptureVideo() { pictureBox1.Image = null; int hr = 0; IBaseFilter sourceFilter = null; ISampleGrabber sampleGrabber = null; try { // Get DirectShow interfaces GetInterfaces(); // Attach the filter graph to the capture graph hr = captureGraphBuilder.SetFiltergraph(graphBuilder); DsError.ThrowExceptionForHR(hr); // Use the system device enumerator and class enumerator to find // a video capture/preview device, such as a desktop USB video camera. sourceFilter = FindCaptureDevice(); // Add Capture filter to graph. hr = graphBuilder.AddFilter(sourceFilter, "Video Capture"); DsError.ThrowExceptionForHR(hr); // Initialize SampleGrabber. sampleGrabber = new SampleGrabber() as ISampleGrabber; // Configure SampleGrabber. Add preview callback. ConfigureSampleGrabber(sampleGrabber); // Add SampleGrabber to graph. hr = graphBuilder.AddFilter(sampleGrabber as IBaseFilter, "Frame Callback"); DsError.ThrowExceptionForHR(hr); // Configure preview settings. SetConfigParams(captureGraphBuilder, sourceFilter, _previewFPS, _previewWidth, _previewHeight); // Render the preview hr = captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, sourceFilter, (sampleGrabber as IBaseFilter), null); DsError.ThrowExceptionForHR(hr); SaveSizeInfo(sampleGrabber); // Set video window style and position SetupVideoWindow(); // Add our graph to the running object table, which will allow // the GraphEdit application to "spy" on our graph rot = new DsROTEntry(graphBuilder); // Start previewing video data hr = mediaControl.Run(); DsError.ThrowExceptionForHR(hr); } catch { MessageBox.Show("An unrecoverable error has occurred."); } finally { if (sourceFilter != null) { Marshal.ReleaseComObject(sourceFilter); sourceFilter = null; } if (sampleGrabber != null) { Marshal.ReleaseComObject(sampleGrabber); sampleGrabber = null; } } } public IBaseFilter SelectCaptureDevice(DsDevice device) { object source = null; Guid iid = typeof(IBaseFilter).GUID; device.Mon.BindToObject(null, null, ref iid, out source); return (IBaseFilter)source; } public IBaseFilter FindCaptureDevice() { int hr = 0; #if USING_NET11 UCOMIEnumMoniker classEnum = null; UCOMIMoniker[] moniker = new UCOMIMoniker[1]; #else IEnumMoniker classEnum = null; IMoniker[] moniker = new IMoniker[1]; #endif object source = null; // Create the system device enumerator ICreateDevEnum devEnum = (ICreateDevEnum)new CreateDevEnum(); // Create an enumerator for the video capture devices hr = devEnum.CreateClassEnumerator(FilterCategory.VideoInputDevice, out classEnum, 0); DsError.ThrowExceptionForHR(hr); // The device enumerator is no more needed Marshal.ReleaseComObject(devEnum); // If there are no enumerators for the requested type, then // CreateClassEnumerator will succeed, but classEnum will be NULL. if (classEnum == null) { throw new ApplicationException("No video capture device was detected.\r\n\r\n" + "This sample requires a video capture device, such as a USB WebCam,\r\n" + "to be installed and working properly. The sample will now close."); } // Use the first video capture device on the device list. // Note that if the Next() call succeeds but there are no monikers, // it will return 1 (S_FALSE) (which is not a failure). Therefore, we // check that the return code is 0 (S_OK). #if USING_NET11 int i; if (classEnum.Next (moniker.Length, moniker, IntPtr.Zero) == 0) #else if (classEnum.Next(moniker.Length, moniker, IntPtr.Zero) == 0) #endif { // Bind Moniker to a filter object Guid iid = typeof(IBaseFilter).GUID; moniker[0].BindToObject(null, null, ref iid, out source); } else { throw new ApplicationException("Unable to access video capture device!"); } // Release COM objects Marshal.ReleaseComObject(moniker[0]); Marshal.ReleaseComObject(classEnum); // An exception is thrown if cast fail return (IBaseFilter)source; } public void GetInterfaces() { int hr = 0; // An exception is thrown if cast fail graphBuilder = (IFilterGraph2)new FilterGraph(); captureGraphBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2(); mediaControl = (IMediaControl)graphBuilder; videoWindow = (IVideoWindow)graphBuilder; DsError.ThrowExceptionForHR(hr); } public void CloseInterfaces() { if (mediaControl != null) { int hr = mediaControl.StopWhenReady(); DsError.ThrowExceptionForHR(hr); } if (videoWindow != null) { videoWindow.put_Visible(OABool.False); videoWindow.put_Owner(IntPtr.Zero); } // Remove filter graph from the running object table. if (rot != null) { rot.Dispose(); rot = null; } // Release DirectShow interfaces. Marshal.ReleaseComObject(mediaControl); mediaControl = null; Marshal.ReleaseComObject(videoWindow); videoWindow = null; Marshal.ReleaseComObject(graphBuilder); graphBuilder = null; Marshal.ReleaseComObject(captureGraphBuilder); captureGraphBuilder = null; } public void SetupVideoWindow() { int hr = 0; // Set the video window to be a child of the PictureBox hr = videoWindow.put_Owner(pictureBox1.Handle); DsError.ThrowExceptionForHR(hr); hr = videoWindow.put_WindowStyle(WindowStyle.Child); DsError.ThrowExceptionForHR(hr); // Make the video window visible, now that it is properly positioned hr = videoWindow.put_Visible(OABool.True); DsError.ThrowExceptionForHR(hr); // Set the video position Rectangle rc = pictureBox1.ClientRectangle; hr = videoWindow.SetWindowPosition(0, 0, _previewWidth, _previewHeight); DsError.ThrowExceptionForHR(hr); } private void SetConfigParams(ICaptureGraphBuilder2 capGraph, IBaseFilter capFilter, int iFrameRate, int iWidth, int iHeight) { int hr; object config; AMMediaType mediaType; // Find the stream config interface hr = capGraph.FindInterface( PinCategory.Capture, MediaType.Video, capFilter, typeof(IAMStreamConfig).GUID, out config); IAMStreamConfig videoStreamConfig = config as IAMStreamConfig; if (videoStreamConfig == null) { throw new Exception("Failed to get IAMStreamConfig"); } // Get the existing format block hr = videoStreamConfig.GetFormat(out mediaType); DsError.ThrowExceptionForHR(hr); // copy out the videoinfoheader VideoInfoHeader videoInfoHeader = new VideoInfoHeader(); Marshal.PtrToStructure(mediaType.formatPtr, videoInfoHeader); // if overriding the framerate, set the frame rate if (iFrameRate > 0) { videoInfoHeader.AvgTimePerFrame = 10000000 / iFrameRate; } // if overriding the width, set the width if (iWidth > 0) { videoInfoHeader.BmiHeader.Width = iWidth; } // if overriding the Height, set the Height if (iHeight > 0) { videoInfoHeader.BmiHeader.Height = iHeight; } // Copy the media structure back Marshal.StructureToPtr(videoInfoHeader, mediaType.formatPtr, false); // Set the new format hr = videoStreamConfig.SetFormat(mediaType); DsError.ThrowExceptionForHR(hr); DsUtils.FreeAMMediaType(mediaType); mediaType = null; } private void SaveSizeInfo(ISampleGrabber sampleGrabber) { int hr; // Get the media type from the SampleGrabber AMMediaType media = new AMMediaType(); hr = sampleGrabber.GetConnectedMediaType(media); DsError.ThrowExceptionForHR(hr); if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero)) { throw new NotSupportedException("Unknown Grabber Media Format"); } // Grab the size info VideoInfoHeader videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, typeof(VideoInfoHeader)); _previewStride = _previewWidth * (videoInfoHeader.BmiHeader.BitCount / 8); DsUtils.FreeAMMediaType(media); media = null; } private void ConfigureSampleGrabber(ISampleGrabber sampleGrabber) { AMMediaType media; int hr; // Set the media type to Video/RBG24 media = new AMMediaType(); media.majorType = MediaType.Video; media.subType = MediaSubType.RGB24; media.formatType = FormatType.VideoInfo; hr = sampleGrabber.SetMediaType(media); DsError.ThrowExceptionForHR(hr); DsUtils.FreeAMMediaType(media); media = null; hr = sampleGrabber.SetCallback(this, 1); DsError.ThrowExceptionForHR(hr); } public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen) { if (isGrab) { Bitmap v = new Bitmap(_previewWidth, _previewHeight, _previewStride, PixelFormat.Format24bppRgb, pBuffer); //v.RotateFlip(RotateFlipType.Rotate180FlipX); v.RotateFlip(RotateFlipType.Rotate90FlipX); BeginInvoke((MethodInvoker)delegate { pictureBox2.BackgroundImage = v; isGrab = false; }); } return 0; } public int SampleCB(double SampleTime, IMediaSample pSample) { //throw new NotImplementedException(); return 0; } private void button2_Click(object sender, EventArgs e) { //截图 isGrab = true; } private void button3_Click(object sender, EventArgs e) { string button_text = button3.Text; if (button_text.Equals("Start Webcam")) { button3.Text = "Stop Webcam"; StartCamera(); } else { button3.Text = "Start Webcam"; StopCamera(); } } }
namespace ScanQRCode { partial class FormWinCamera { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.pictureBox2 = new System.Windows.Forms.PictureBox(); this.button3 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit(); this.SuspendLayout(); // // pictureBox1 // this.pictureBox1.Location = new System.Drawing.Point(12, 12); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(939, 616); this.pictureBox1.TabIndex = 7; this.pictureBox1.TabStop = false; // // pictureBox2 // this.pictureBox2.Location = new System.Drawing.Point(976, 50); this.pictureBox2.Name = "pictureBox2"; this.pictureBox2.Size = new System.Drawing.Size(404, 357); this.pictureBox2.TabIndex = 8; this.pictureBox2.TabStop = false; // // button3 // this.button3.Location = new System.Drawing.Point(1162, 12); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(140, 23); this.button3.TabIndex = 9; this.button3.Text = "Start Webcam"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); // // button2 // this.button2.Location = new System.Drawing.Point(1021, 12); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 10; this.button2.Text = "截图"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // FormWinCamera // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1392, 640); this.Controls.Add(this.button2); this.Controls.Add(this.button3); this.Controls.Add(this.pictureBox2); this.Controls.Add(this.pictureBox1); this.Name = "FormWinCamera"; this.Text = "FormWinCamera"; this.Load += new System.EventHandler(this.FormWinCamera_Load); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.PictureBox pictureBox2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button2; } }