Skip to main content
 首页 » 编程设计

C#.NET DirectShow 库 : Saving custom graph to a file

2024年08月12日35qq78292959

我正在尝试在 Directshow.net Lib 的帮助下在 C#.NET 中创建自定义 (Directshow) 滤镜图.该库基于 Microsoft 的 C++ directshow 接口(interface)。

创建图表有效,我可以向其中添加一个或多个过滤器。然而,当试图将图形保存到文件时,它写入了一些字节,但图形编辑器 (graphedt.exe) 无法打开它。

        DsDevice[] videoInputDevices = 
            DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice); 
 
        IGraphBuilder graphBuilder = (IGraphBuilder) new FilterGraph(); 
 
        ICaptureGraphBuilder2 captureGraphBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); 
        captureGraphBuilder.SetFiltergraph(graphBuilder); 
 
        object source; 
        videoInputDevices[0].Mon.BindToObject( 
            null, 
            null, 
            typeof(IBaseFilter).GUID, 
            out source); 
 
        IBaseFilter filter = (IBaseFilter)source; 
        graphBuilder.AddFilter(filter, "Video Capture"); 
 
        try 
        { 
            int renderStreamComResult = captureGraphBuilder.RenderStream( 
                PinCategory.Preview, 
                MediaType.Video, 
                filter, 
                null, 
                null); 
 
            MyStreamWriter r = new MyStreamWriter(); 
            IPersistStream p = (IPersistStream)graphBuilder; 
 
            p.Save(r, true); 
 
            // ugly, only temporary... 
            r.bWriter.Flush(); 
            r.bWriter.Close(); 
            // 
 
 
            //DsError.ThrowExceptionForHR(renderStreamComResult); 
        } 
        finally 
        { 
            if (filter != null) 
            { 
                Marshal.ReleaseComObject(filter); 
            } 
 
            if (graphBuilder != null) 
            { 
                Marshal.ReleaseComObject(graphBuilder); 
            } 
 
            if (captureGraphBuilder != null) 
            { 
                Marshal.ReleaseComObject(captureGraphBuilder); 
            } 
        } 

如果我将我生成的文件的内容与我在编辑器中手动创建的文件的内容进行比较,它们看起来确实不同。

Graph from c# net

Graph made using the editor

实现 ComTypes.IStream 的 MyStreamWriter 类:

IPersistStream.Save() 仅调用流编写器上的 Seek() 和 Write()。

public class MyStreamWriter : IStream 
{ 
    public BinaryWriter bWriter; 
 
    public MyStreamWriter() 
    { 
        this.bWriter = new BinaryWriter( 
            File.OpenWrite("graph.grf"),  
            Encoding.UTF8); 
    } 
 
    public void Clone(out iop.ComTypes.IStream ppstm) 
    { 
        throw new NotImplementedException(); 
    } 
 
    public void Commit(int grfCommitFlags) 
    { 
        bWriter.Flush(); 
        throw new NotImplementedException(); 
    } 
 
    public void CopyTo(iop.ComTypes.IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten) 
    { 
        throw new NotImplementedException(); 
    } 
 
    public void LockRegion(long libOffset, long cb, int dwLockType) 
    { 
        throw new NotImplementedException(); 
    } 
 
    public void Read(byte[] pv, int cb, IntPtr pcbRead) 
    { 
        throw new NotImplementedException(); 
    } 
 
    public void Revert() 
    { 
        throw new NotImplementedException(); 
    } 
 
    public void Seek(long dlibMove, int dwOrigin, IntPtr plibNewPosition) 
    { 
        bWriter.Seek((int)dlibMove, (SeekOrigin)dwOrigin); 
    } 
 
    public void SetSize(long libNewSize) 
    { 
        throw new NotImplementedException(); 
    } 
 
    public void Stat(out iop.ComTypes.STATSTG pstatstg, int grfStatFlag) 
    { 
        throw new NotImplementedException(); 
    } 
 
    public void UnlockRegion(long libOffset, long cb, int dwLockType) 
    { 
        throw new NotImplementedException(); 
    } 
 
    public void Write(byte[] pv, int cb, IntPtr pcbWritten) 
    { 
        bWriter.Write(pv); 
    } 
} 

directshow 库在 IPersistStream 中没有做任何奇怪的事情,它只是使 C++ 接口(interface)可用...所以问题一定出在其他地方。

非常感谢任何帮助。

请您参考如下方法:

我认为解决方案要简单得多 CSharp DirectShow 库允许您将图形保存在文件中。

这段代码对我有用:

使用 DirectShowLib.Utils;

FilterGraphTools.SaveGraphFile(this.m_TheGraphBuilder, FilePath);