C#.NET DirectShow库:将自定义图形保存到文件

C#.NET DirectShow库:将自定义图形保存到文件,c#,.net,graph,directshow,C#,.net,Graph,Directshow,我正试图在的帮助下在C#.NET中创建一个自定义(Directshow)filtergraph。LIB是基于微软的C++ DirectShow接口的。 创建图形是可行的,我可以向它添加一个或多个过滤器。但是,当试图将图形保存到文件时,它会写入一些字节,但图形编辑器(graphedt.exe)无法打开它 DsDevice[] videoInputDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoIn

我正试图在的帮助下在C#.NET中创建一个自定义(Directshow)filtergraph。LIB是基于微软的C++ DirectShow接口的。 创建图形是可行的,我可以向它添加一个或多个过滤器。但是,当试图将图形保存到文件时,它会写入一些字节,但图形编辑器(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);
            }
        }
如果我将生成的文件内容与在编辑器中手动创建的文件内容进行比较,它们看起来确实不同

实现ComTypes.IStream的MyStreamWriter类:

IPersistStream.Save()仅调用streamwriter上的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 LIB在IPersistStream没有任何奇怪的行为,它只是使C++接口可用…所以问题一定在别的地方


非常感谢您的帮助。

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

此代码适用于我:

使用DirectShowLib.Utils


FilterGraphTools.SaveGraphFile(this.m_TheGraphBuilder,FilePath)

文件不必匹配,字节到字节OK,但我没有DirectShowLib.Utils?我需要添加不同的dll吗?目前,我有DirectShowLib-2005。我也在使用DirectShowLib-2004,请记住包括“使用DirectShowLib.Utils;”。事实证明,您可以从directshow lib()下载的一个示例中找到FilterGraphTools。我不知道FilterGraphTools是示例的一部分;)。当我开始使用DirectShowLib时,它已经构建好了