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))。
CaptureD(); 是不是只能初始化1次,你定义成static,多次调用。看名字好像是图像捕捉?那么设备只能打开并且被一个对象调用。
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);
要看你完整的代码,record();这里record()是怎么定义的,窗体的成员函数?
m_captureGraphBuilder在哪里初始化的?它是一个com组件
把方法写在了一个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);
private void record()
{
string strParth = videoPath + returnFileName(".avi");
bool info = cam.StartRecord(strParth);
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);
嗯嗯,是用来捕捉图像的。O(∩_∩)O谢谢!,我试试。
class CaptureD : ISampleGrabberCB, IDisposable
{
这个类实现了一些接口不能使用静态吧?
而且我只实列化了一个CaptureD
不是CaptureD静态,而是static CaptureD 变量 = new CaptureD();,这个变量要static,只初始化一个