如何预览/播放IMediaControl.Run在面板或图片框中-C#Direct Show

时间:2019-05-02 11:00:45

标签: c# winforms directshow directshow.net

使用下面的代码,我可以创建一个Direct Show图形来捕获网络摄像机输入设备。现在我的问题是可以在Winform中预览吗?在图片框或面板中显示此视频容易吗?

  

IMediaControl.Run()

返回真实值。我想显示所选摄像机在我的应用中运行。

 private void buttonPlayVideoDevice_Click(object sender, EventArgs e)
{
    if (comboBoxDevices.SelectedIndex < 0)
        return;
    StopPlaying();
    Device device = (Device)comboBoxDevices.Items[comboBoxDevices.SelectedIndex];
    if (m_player.OpenCamera(device, m_vcam_filter))
    {
        buttonStopVideoDevice.Enabled = true;
        m_player.Run();
    }
  }

public bool OpenCamera(Device i_device, IBaseFilter i_vcam_filter)
{
    // Stop and release interfaces
    Cleanup();
    bool succeeded = true;
    IPin pin_out = null;
    IPin pin_in = null;
    int hr = 0;

    // Create an instance of FilterGraph interface
    m_graph_builder = (IGraphBuilder)new FilterGraph();

    // Add camera source filter to our graph.
    IBaseFilter filter_source = i_device.CreateDevice();
    if (0 != (hr = m_graph_builder.AddFilter(filter_source, "Source Filter")))
    {
        succeeded = false;
        goto exit;
    }

    // Add VCam Render filter to graph, the VCam Render will pass video frames to VCam
    if (0 != (hr = m_graph_builder.AddFilter(i_vcam_filter, "VCam Renderer Filter")))
    {
        succeeded = false;
        goto exit;
    }

    pin_out = DsFindPin.ByDirection(filter_source, PinDirection.Output, 0);
    pin_in = DsFindPin.ByDirection(i_vcam_filter, PinDirection.Input, 0);
    if (pin_out == null || pin_in == null)
    {
        succeeded = false;
        goto exit;
    }

    if (0 != (hr = m_graph_builder.Connect(pin_out, pin_in)))
    {
        succeeded = false;
        goto exit;
    }

    m_control = (IMediaControl)m_graph_builder;      
    exit:
      if (filter_source != null)                                                                                      Marshal.ReleaseComObject(filter_source);
    if (pin_out != null) Marshal.ReleaseComObject(pin_out);
    if (pin_in != null) Marshal.ReleaseComObject(pin_in);
    return succeeded;            
}
   public void Run()
    {
        if (m_control != null)
            m_control.Run();
    }

0 个答案:

没有答案