如何在PictureBox中显示预览?

时间:2012-06-07 05:31:36

标签: c# directshow.net

我使用DirectshowLib和c#录制来自网络摄像头的视频,但在视频录制过程中也会显示视频预览,所以我已经完成了以下步骤

1.i获得了可用的视频设备

  1. 初始化。使用Graph = (IFilterGraph2)new FilterGraph();

  2. 的IFilterGrapth2对象
  3. 初始化。使用captureGraph = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();

  4. 的ICaptureGraphBuilder2对象

    4.filter grapth设置为ICaptureGraphBuilder2对象captureGraph.SetFiltergraph(Graph);

    1. 然后添加了soursefilterformoniker()和outputfilename()
    2. 完整的代码如下。它没有任何错误,但它单独打开预览窗口;我想在图片框上显示预览..

      如何预览图片框中的窗口?

      这是我的完整代码

      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 DirectShowLib;
      using DirectShowLib.DMO;
      using System.Runtime.InteropServices;
      using System.Drawing.Drawing2D;
      using System.IO;
      
      namespace Cam_Recording_V1_directshow.net_
      {
          public partial class Form1 : Form
          {
              string captureDeviceName = string.Empty;
              IFilterGraph2 Graph = null;
              IMediaControl m_mediaCtrl = null;
              public List<DsDevice> AvailableVideoInputDevices { get; private set; }
              IAMVideoProcAmp vpa;
              [DllImport("olepro32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
              private static extern int OleCreatePropertyFrame(IntPtr hwndOwner, int x, int y,
                  string lpszCaption, int cObjects,
                  [In, MarshalAs(UnmanagedType.Interface)] ref object ppUnk,
                  int cPages, IntPtr pPageClsID, int lcid, int dwReserved, IntPtr pvReserved);
              public Form1()
              {
                  InitializeComponent();
              }
      
              private void Form1_Load(object sender, EventArgs e)
              {
      
                  IBaseFilter capFilter = null;
                  IBaseFilter asfWriter = null;
                  IFileSinkFilter pTmpSink = null;
                  ICaptureGraphBuilder2 captureGraph = null;
                  object o;
      
                  //
                  //Get list of video devices
                  //
                  AvailableVideoInputDevices = new List<DsDevice>();
                  DsDevice[] videoInputDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
                  AvailableVideoInputDevices.AddRange(videoInputDevices);
                  if (AvailableVideoInputDevices.Count > 0)
                  {
                      //
                      //init capture graph
                      //
                      Graph = (IFilterGraph2)new FilterGraph();
                      captureGraph = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
      
                      //
                      //sets filter object from graph
                      //
                      captureGraph.SetFiltergraph(Graph);
                      //
                      //which device will use graph setting
                      //
                      Graph.AddSourceFilterForMoniker(AvailableVideoInputDevices.First().Mon, null, AvailableVideoInputDevices.First().Name, out capFilter);
                      captureDeviceName = AvailableVideoInputDevices.First().Name;
                      #region WMV
                      //
                      //sets output file name,and file type
                      //
                      captureGraph.SetOutputFileName(MediaSubType.Asf, /*DateTime.Now.Ticks.ToString()  +".wmv"*/ "test.wmv", out asfWriter, out pTmpSink);
                      //
                      //configure which video setting is used by graph
                      //                
                      IConfigAsfWriter lConfig = asfWriter as IConfigAsfWriter;
                      Guid cat = new Guid("8C45B4C7-4AEB-4f78-A5EC-88420B9DADEF");
                      lConfig.ConfigureFilterUsingProfileGuid(cat);
                      #endregion                      
                      captureGraph.RenderStream(PinCategory.Preview, MediaType.Video, capFilter, null, null);
      
                      captureGraph.RenderStream(PinCategory.Capture, MediaType.Video, capFilter, null, asfWriter);
                      m_mediaCtrl = Graph as IMediaControl;
                      m_mediaCtrl.Run();
                  }
                  else
                  {
                      MessageBox.Show("Video Capture Device Not Found!!");
                      button1.Visible = false;
                  }
              }
      

1 个答案:

答案 0 :(得分:0)

我使用VMR9完成了这项工作。我在Graph中添加了一个VMR9过滤器。还有更多。