当前位置:编程学习 > C#/ASP.NET >>

求大神解读视频捕获程序 跪谢

namespace 视屏捕获
{
    public partial class Form1 : Form
    {
        IGraphBuilder graphBuilder = null;
        ICaptureGraphBuilder2 captureGraphBuilder = null;
        IBaseFilter sourceFilter = null;                                             //////IBaseFilter theDevice = null;
        IVideoWindow videoWindow = null;
        IMediaControl mediaControl = null;

        public Form1()
        {
            InitializeComponent();
            foreach (DsDevice ds in DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice))
            {
                comboBox1.Items.Add(ds.Name);
            }
            if (comboBox1.Items.Count > 0)
            {
                comboBox1.SelectedIndex = 0;
            }
        }
        private SerialPort comm = new SerialPort();
        private StringBuilder builder = new StringBuilder();//避免在事件处理方法中反复的创建,定义到外面。  
        //private long received_count = 0;//接收计数  
       // private long send_count = 0;//发送计数  

          public void CaptureVideo()
        {
            if (sourceFilter == null)
            {
                MessageBox.Show("没有检测到视频设备11111111111!");
                return;
            }
            int hr = 0;
            try
            {
                GetInterfaces();

                hr = this.captureGraphBuilder.SetFiltergraph(this.graphBuilder);
                DsError.ThrowExceptionForHR(hr);

                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);

                SetupVideoWindow();

                hr = this.mediaControl.Run();
                DsError.ThrowExceptionForHR(hr);

            }
            catch
            {
                MessageBox.Show("An unrecoverable error has occurred.");
            }

        }
          public void GetInterfaces()
          {
              this.graphBuilder = (IGraphBuilder)new FilterGraph();
              this.captureGraphBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
              this.mediaControl = (IMediaControl)this.graphBuilder;
              this.videoWindow = (IVideoWindow)this.graphBuilder;
          }

          private IBaseFilter CreateFilter(Guid category, string friendlyname)
          {
              object source = null;
              Guid iid = typeof(IBaseFilter).GUID;
              foreach (DsDevice device in DsDevice.GetDevicesOfCat(category))
              {
                  if (device.Name.CompareTo(friendlyname) == 0)
                  {
                      device.Mon.BindToObject(null, null, ref iid, out source);
                      break;
                  }
              }

              return (IBaseFilter)source;
          }


          public void SetupVideoWindow()
          {
              int hr = 0;
              hr = this.videoWindow.put_Owner(panel1.Handle);
              DsError.ThrowExceptionForHR(hr);

              hr = this.videoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren);
              DsError.ThrowExceptionForHR(hr);

              ResizeVideoWindow();

              // Make the video window visible, now that it is properly positioned
              hr = this.videoWindow.put_Visible(OABool.True);
              DsError.ThrowExceptionForHR(hr);
          }
          
        public void ResizeVideoWindow()
          {
              // Resize the video preview window to match owner window size
              if (this.videoWindow != null)
              {
                  this.videoWindow.SetWindowPosition(0, 0, this.panel1.Width, this.panel1.Height);


              }
          }

        private void 视屏_Click(object sender, EventArgs e)
        {
            if (sourceFilter != null)
            {
                Marshal.ReleaseComObject(sourceFilter);
                sourceFilter = null;
            }
            //Create the filter for the selected video input device
            string devicepath = comboBox1.SelectedItem.ToString();
            sourceFilter = CreateFilter(FilterCategory.VideoInputDevice, devicepath);

            CaptureVideo();
        }
     
    }
}
--------------------编程问答-------------------- 怎么选的设备
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,