using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Runtime.InteropServices.ComTypes;
public class Form1 : System.Windows.Forms.Form
public const int WM_GRAPHNOTIFY = 0x8000 + 1;
IVideoWindow videoWindow = null;
IMediaControl mediaControl = null;
IMediaEventEx mediaEventEx = null;
IGraphBuilder graphBuilder = null;
ICaptureGraphBuilder2 captureGraphBuilder = null;
PlayState currentState = PlayState.Stopped;
private PictureBox pictureBox1;
protected override void Dispose( bool disposing )
base.Dispose( disposing );
private void InitializeComponent()
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.pictureBox1.Location = new System.Drawing.Point(51, 12);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(322, 249);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
this.button1.Location = new System.Drawing.Point(140, 267);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(153, 41);
this.button1.TabIndex = 1;
this.button1.Text = "Запустить";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(432, 330);
this.Controls.Add(this.button1);
this.Controls.Add(this.pictureBox1);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.ForeColor = System.Drawing.SystemColors.ControlText;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "WebcamCapture";
this.Load += new System.EventHandler(this.Form1_Load);
this.Resize += new System.EventHandler(this.Form1_Resize);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
public void CaptureVideo()
IBaseFilter sourceFilter = null;
hr = this.captureGraphBuilder.SetFiltergraph(this.graphBuilder);
DsError.ThrowExceptionForHR(hr);
sourceFilter = FindCaptureDevice();
hr = this.graphBuilder.AddFilter(sourceFilter, "Video Capture");
DsError.ThrowExceptionForHR(hr);
hr = this.captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, sourceFilter, null, null);
DsError.ThrowExceptionForHR(hr);
Marshal.ReleaseComObject(sourceFilter);
rot = new DsROTEntry(this.graphBuilder);
hr = this.mediaControl.Run();
DsError.ThrowExceptionForHR(hr);
this.currentState = PlayState.Running;
MessageBox.Show("An unrecoverable error has occurred.");
public IBaseFilter FindCaptureDevice()
UCOMIEnumMoniker classEnum = null;
UCOMIMoniker[] moniker = new UCOMIMoniker[1];
IEnumMoniker classEnum = null;
IMoniker[] moniker = new IMoniker[1];
ICreateDevEnum devEnum = (ICreateDevEnum) new CreateDevEnum();
hr = devEnum.CreateClassEnumerator(FilterCategory.VideoInputDevice, out classEnum, 0);
DsError.ThrowExceptionForHR(hr);
Marshal.ReleaseComObject(devEnum);
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.");
if (classEnum.Next (moniker.Length, moniker, IntPtr.Zero) == 0)
if (classEnum.Next (moniker.Length, moniker, IntPtr.Zero) == 0)
Guid iid = typeof(IBaseFilter).GUID;
moniker[0].BindToObject(null, null, ref iid, out source);
throw new ApplicationException("Unable to access video capture device!");
Marshal.ReleaseComObject(moniker[0]);
Marshal.ReleaseComObject(classEnum);
return (IBaseFilter) source;
public void GetInterfaces()
this.graphBuilder = (IGraphBuilder) new FilterGraph();
this.captureGraphBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();
this.mediaControl = (IMediaControl) this.graphBuilder;
this.videoWindow = (IVideoWindow) this.graphBuilder;
this.mediaEventEx = (IMediaEventEx) this.graphBuilder;
hr = this.mediaEventEx.SetNotifyWindow(this.Handle, WM_GRAPHNOTIFY, IntPtr.Zero);
DsError.ThrowExceptionForHR(hr);
public void CloseInterfaces()
if (this.mediaControl != null)
int hr = mediaControl.StopWhenReady();
DsError.ThrowExceptionForHR(hr);
this.currentState = PlayState.Stopped;
if (this.mediaEventEx != null)
this.mediaEventEx.SetNotifyWindow(IntPtr.Zero, WM_GRAPHNOTIFY, IntPtr.Zero);
if(this.videoWindow != null)
this.videoWindow.put_Visible(OABool.False);
this.videoWindow.put_Owner(IntPtr.Zero);
Marshal.ReleaseComObject(mediaControl); mediaControl = null;
Marshal.ReleaseComObject(mediaEventEx); mediaEventEx = null;
Marshal.ReleaseComObject(videoWindow); videoWindow = null;
Marshal.ReleaseComObject(this.graphBuilder); this.graphBuilder = null;
Marshal.ReleaseComObject(this.captureGraphBuilder); this.captureGraphBuilder = null;
public void SetupVideoWindow()
hr = this.videoWindow.put_Owner(pictureBox1.Handle);
DsError.ThrowExceptionForHR(hr);
hr = this.videoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren);
DsError.ThrowExceptionForHR(hr);
hr = this.videoWindow.put_Visible(OABool.True);
DsError.ThrowExceptionForHR(hr);
public void ResizeVideoWindow()
if (this.videoWindow != null)
this.videoWindow.SetWindowPosition(0, 0, this.ClientSize.Width, this.ClientSize.Height);
public void ChangePreviewState(bool showVideo)
if (this.mediaControl == null)
if (this.currentState != PlayState.Running)
hr = this.mediaControl.Run();
this.currentState = PlayState.Running;
hr = this.mediaControl.StopWhenReady();
this.currentState = PlayState.Stopped;
public void HandleGraphEvent()
IntPtr evParam1, evParam2;
if (this.mediaEventEx == null)
while(this.mediaEventEx.GetEvent(out evCode, out evParam1, out evParam2, 0) == 0)
hr = this.mediaEventEx.FreeEventParams(evCode, evParam1, evParam2);
DsError.ThrowExceptionForHR(hr);
protected override void WndProc(ref Message m)
if (this.videoWindow != null)
this.videoWindow.NotifyOwnerMessage(m.HWnd, m.Msg, m.WParam, m.LParam);
private void Form1_Resize(object sender, System.EventArgs e)
if (this.WindowState == FormWindowState.Minimized)
ChangePreviewState(false);
if (this.WindowState == FormWindowState.Normal)
ChangePreviewState(true);
private void StartCamera()
DsDevice[] devices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
MessageBox.Show("Нет подключенной вебкамеры");
button1.Text = "Запустить";
DsDevice dev = devices[0] as DsDevice;
MessageBox.Show("Device: " + dev.Name);
private void StopCamera()
button1.Text = "Запустить";
Application.Run(new Form1());
private void Form1_Load(object sender, EventArgs e)
private void pictureBox1_Click(object sender, EventArgs e)
private void button1_Click(object sender, EventArgs e)
string button_text = button1.Text;
if (button_text.Equals("Запустить"))
button1.Text = "Выключить";
button1.Text = "Запустить";