DirectShow vs. DirectShow.NET
下面两段代码,哪个更容易理解?
C++:
1 IGraphBuilder *pGraph = NULL;
2 IMediaControl *pControl = NULL;
3 IMediaEvent *pEvent = NULL;
4
5 // Initialize the COM library.
6 HRESULT hr = CoInitialize(NULL);
7 if (FAILED(hr))
8 {
9 printf("ERROR - Could not initialize COM library");
10 return;
11 }
12
13 // Create the filter graph manager and query for interfaces.
14 hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
15 IID_IGraphBuilder, (void **)&pGraph);
16 if (FAILED(hr))
17 {
18 printf("ERROR - Could not create the Filter Graph Manager.");
19 return;
20 }
21
22 hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
23 hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
24
25 // Build the graph. IMPORTANT: Change this string to a file on your system.
26 hr = pGraph->RenderFile(L"C:\\Example.avi", NULL);
27 if (SUCCEEDED(hr))
28 {
29 // Run the graph.
30 hr = pControl->Run();
31 if (SUCCEEDED(hr))
32 {
33 // Wait for completion.
34 long evCode;
35 pEvent->WaitForCompletion(INFINITE, &evCode);
36
37 // Note: Do not use INFINITE in a real application, because it
38 // can block indefinitely.
39 }
40 }
41 pControl->Release();
42 pEvent->Release();
43 pGraph->Release();
44 CoUninitialize();
45
C#:
1 IGraphBuilder graph = new FilterGraph() as IGraphBuilder;
2 IMediaControl control = graph as IMediaControl;
3 IMediaEvent mediaEvent = graph as IMediaEvent;
4 graph.RenderFile(@"D:\Projects\DynamicVideoSkimming\cd310_escapa.wmv", null);
5 control.Run();
6 EventCode eventCode;
7 mediaEvent.WaitForCompletion(int.MaxValue, out eventCode);
8 Marshal.ReleaseComObject(graph);
最近重拾DirectShow,由于COM基础不好,看C++看得东倒西歪,郁闷万分。想到就算把C++ code写完以后还要P/Invoke才能在C#中调用,想死的心都有。突然想起来试试DirectShow.NET,写出来代码果然好看。更关键的是没有COM的阻挠,瞬间就理解了DirectShow的架构。想想看学习OpenCV和EmguCV的也是这样,看了EmguCV后对OpenCV反而有了更深刻的理解。看来.NET技术不仅用起来简单实用,帮助用户集中精力于算法实现,还有一大功用是帮助理解其余库的精髓所在。:-)