/// <summary>
/// Luoz:start all the interfaces, graphs and preview window
/// </summary>
bool StartupVideo(IMoniker mon)
{
int hr;
try
{
if (!CreateCaptureDevice(mon))
return false; if (!GetInterfaces())
return false; if (!SetupGraph())
return false; if (!SetupVideoWindow())
return false;#if DEBUG
//DsROT.AddGraphToRot(graphBuilder, out rotCookie); // graphBuilder capGraph
#endif hr = mediaCtrl.Run();
if (hr < 0)
Marshal.ThrowExceptionForHR(hr);
return true;
}
catch (Exception ee)
{
MessageBox.Show(this, "Could not start video stream\r\n" + ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return false;
}
} /// <summary>
/// Luoz: 创建用户选择的设备设备
/// </summary>
bool CreateCaptureDevice(IMoniker mon)
{
object capObj = null;
try
{
Guid gbf = typeof(IBaseFilter).GUID;
mon.BindToObject(null, null, ref gbf, out capObj);
capFilter = (IBaseFilter)capObj; capObj = null;
return true;
}
catch (Exception ee)
{
MessageBox.Show(this, "Could not create capture device\r\n" + ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return false;
}
finally
{
if (capObj != null)
Marshal.ReleaseComObject(capObj); capObj = null;
}
} /// <summary>
/// Luoz:创建使用COM组件的接口并获得
/// </summary>
/// <returns></returns>
bool GetInterfaces()
{
Type comType = null;
object comObj = null;
try
{
comType = Type.GetTypeFromCLSID(Clsid.FilterGraph);
if (comType == null)
throw new NotImplementedException(@"DirectShow FilterGraph not installed/registered!");
comObj = Activator.CreateInstance(comType);
graphBuilder = (IGraphBuilder)comObj; comObj = null; //Guid clsid = Clsid.CaptureGraphBuilder2;
//Guid riid = typeof(ICaptureGraphBuilder2).GUID;
//comObj = DsBugWO.CreateDsInstance(ref clsid, ref riid);
//capGraph = (ICaptureGraphBuilder2)comObj; comObj = null; comType = Type.GetTypeFromCLSID(Clsid.CaptureGraphBuilder2);
comObj = Activator.CreateInstance(comType);
capGraph = (ICaptureGraphBuilder2)comObj; comObj = null; mediaCtrl = (IMediaControl)graphBuilder;
videoWin = (IVideoWindow)graphBuilder;
mediaEvt = (IMediaEventEx)graphBuilder;
return true;
}
catch (Exception ee)
{
MessageBox.Show(this, "Could not get interfaces\r\n" + ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return false;
}
finally
{
if (comObj != null)
Marshal.ReleaseComObject(comObj); comObj = null;
}
} /// <summary>
/// "Luoz:打造捕捉图"
/// </summary>
/// <returns></returns>
bool SetupGraph()
{
int hr;
IBaseFilter mux = null;
IFileSinkFilter sink = null; try
{
hr = capGraph.SetFiltergraph(graphBuilder);
if (hr < 0)
Marshal.ThrowExceptionForHR(hr); hr = graphBuilder.AddFilter(capFilter, "Ds.NET Video Capture Device");
if (hr < 0)
Marshal.ThrowExceptionForHR(hr); DsUtils.ShowCapPinDialog(capGraph, capFilter, this.Handle); Guid sub = MediaSubType.Avi;
hr = capGraph.SetOutputFileName(ref sub, fileName, out mux, out sink);
if (hr < 0)
Marshal.ThrowExceptionForHR(hr); Guid cat = PinCategory.Capture;
Guid med = MediaType.Video;
hr = capGraph.RenderStream(ref cat, ref med, capFilter, null, mux); // 流文件
if (hr < 0)
Marshal.ThrowExceptionForHR(hr); cat = PinCategory.Preview;
med = MediaType.Video;
hr = capGraph.RenderStream(ref cat, ref med, capFilter, null, null); // 预览窗口
if (hr < 0)
Marshal.ThrowExceptionForHR(hr); return true;
}
catch (Exception ee)
{
MessageBox.Show(this, "Could not setup graph\r\n" + ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return false;
}
finally
{
if (mux != null)
Marshal.ReleaseComObject(mux); mux = null;
if (sink != null)
Marshal.ReleaseComObject(sink); sink = null;
}
} /// <summary>
/// Luoz: 预览
/// </summary>
bool SetupVideoWindow()
{
int hr;
try
{
// 设置视频窗口是主窗口的子窗口
hr = videoWin.put_Owner(panel1.Handle);
if (hr < 0)
Marshal.ThrowExceptionForHR(hr); // Set video window style
hr = videoWin.put_WindowStyle(WS_CHILD | WS_CLIPCHILDREN);
if (hr < 0)
Marshal.ThrowExceptionForHR(hr); //使用助手功能定位在客户端的视频窗口的所有者窗口矩形
//ResizeVideoWindow(); // 使视频窗口可见,现在它已正确定位
hr = videoWin.put_Visible(DsHlp.OATRUE);
if (hr < 0)
Marshal.ThrowExceptionForHR(hr); hr = mediaEvt.SetNotifyWindow(this.Handle, WM_GRAPHNOTIFY, IntPtr.Zero);
if (hr < 0)
Marshal.ThrowExceptionForHR(hr);
return true;
}
catch (Exception ee)
{
MessageBox.Show(this, "Could not setup video window\r\n" + ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return false;
}
} }
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货