eeteee
2018-05-06 03:14
采纳率: 50%
浏览 690

C# 不同控件调用同一方法出错。

private void btn_Record_Click(object sender, EventArgs e)
{
record();
}
//在一个button上调用了一个record()方法。此方法可以正常使用

             private void port_PinChanged(object sender, System.IO.Ports.SerialPinChangedEventArgs e)
    {
            switch (e.EventType)
            {
                case System.IO.Ports.SerialPinChange.DsrChanged:
                    //开始录像
                    if (com.DsrHolding == true)
                    {
                                                record();
                    }
                    break;
                default:
                    break;
            }
            //在一个SerialPort的PinChanged事件上也调用了一个record()方法。
            但是为啥会报一下错误---------------------------

无法将类型为“DirectShowLib.CaptureGraphBuilder2”的 COM 对象强制转换为接口类型“DirectShowLib.ICaptureGraphBuilder2”。此操作失败的原因是对 IID 为“{93E5A4E0-2D50-11D2-ABFA-00A0C9C6E38D}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 不支持此接口 (异常来自 HRESULT:0x80004002 (E_NOINTERFACE))。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

11条回答 默认 最新

  • threenewbee 2018-05-06 03:47
    已采纳

    CaptureD(); 是不是只能初始化1次,你定义成static,多次调用。看名字好像是图像捕捉?那么设备只能打开并且被一个对象调用。

    打赏 评论
  • eeteee 2018-05-06 03:19
                                record()方法的出错代码
    
                                hr = this.m_mediaControl.Stop();
                DsError.ThrowExceptionForHR(hr);
    
                //Add the Video compressor filter to the graph
                hr = m_graphBuilder.AddFilter(theCompressor, "compressor filter");
                DsError.ThrowExceptionForHR(hr);
    
                //Create the file writer part of the graph. SetOutputFileName does this for us, and returns the mux and sink
                hr = m_captureGraphBuilder.SetOutputFileName(MediaSubType.Avi, filePath, out mux, out sink);
                DsError.ThrowExceptionForHR(hr);
    
                //Connect the device and compressor to the mux to render the capture part of the graph
                hr = m_captureGraphBuilder.RenderStream(null, null, pCaptureOut, null, theCompressor);
                DsError.ThrowExceptionForHR(hr);
    
                //Connect the device and compressor to the mux to render the capture part of the graph
                hr = m_captureGraphBuilder.RenderStream(null, MediaType.Video, theCompressor, null, mux);
                DsError.ThrowExceptionForHR(hr);
    
                hr = this.m_mediaControl.Run();
                DsError.ThrowExceptionForHR(hr);
    
    打赏 评论
  • threenewbee 2018-05-06 03:21

    要看你完整的代码,record();这里record()是怎么定义的,窗体的成员函数?

    打赏 评论
  • threenewbee 2018-05-06 03:23

    m_captureGraphBuilder在哪里初始化的?它是一个com组件

    打赏 评论
  • eeteee 2018-05-06 03:37

    把方法写在了一个CaptureD cam = new CaptureD(); 的类上,
    CaptureD 类 里InitInterfaces() 初始化m_captureGraphBuilder
    private void InitInterfaces()
    {
    int hr = 0;
    //获取ICaptureGraphBuilder2接口对象
    this.m_captureGraphBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
    //设置ICaptureGraphBuilder2的IGraphBuilder接口对象为当前对象
    hr = this.m_captureGraphBuilder.SetFiltergraph(this.m_graphBuilder);
    DsError.ThrowExceptionForHR(hr);

    打赏 评论
  • eeteee 2018-05-06 03:38

    private void record()
    {
    string strParth = videoPath + returnFileName(".avi");
    bool info = cam.StartRecord(strParth);

    打赏 评论
  • eeteee 2018-05-06 03:39
    public bool StartRecord(string filePath)
        {
                 int hr = 0;
                hr = this.m_mediaControl.Stop();
                DsError.ThrowExceptionForHR(hr);
    
                //Add the Video compressor filter to the graph
                hr = m_graphBuilder.AddFilter(theCompressor, "compressor filter");
                DsError.ThrowExceptionForHR(hr);
    
                //Create the file writer part of the graph. SetOutputFileName does this for us, and returns the mux and sink
                hr = m_captureGraphBuilder.SetOutputFileName(MediaSubType.Avi, filePath, out mux, out sink);
                DsError.ThrowExceptionForHR(hr);
    
                //Connect the device and compressor to the mux to render the capture part of the graph
                hr = m_captureGraphBuilder.RenderStream(null, null, pCaptureOut, null, theCompressor);
                DsError.ThrowExceptionForHR(hr);
    
                //Connect the device and compressor to the mux to render the capture part of the graph
                hr = m_captureGraphBuilder.RenderStream(null, MediaType.Video, theCompressor, null, mux);
                DsError.ThrowExceptionForHR(hr);
    
                hr = this.m_mediaControl.Run();
                DsError.ThrowExceptionForHR(hr);
    
    打赏 评论
  • eeteee 2018-05-06 03:53

    嗯嗯,是用来捕捉图像的。O(∩_∩)O谢谢!,我试试。

    打赏 评论
  • eeteee 2018-05-06 03:59

    class CaptureD : ISampleGrabberCB, IDisposable
    {
    这个类实现了一些接口不能使用静态吧?

    打赏 评论
  • eeteee 2018-05-06 03:59

    而且我只实列化了一个CaptureD

    打赏 评论
  • threenewbee 2018-05-06 04:27

    不是CaptureD静态,而是static CaptureD 变量 = new CaptureD();,这个变量要static,只初始化一个

    打赏 评论

相关推荐 更多相似问题