2014-12-03 67 views
1

我想在C#.NET中創建一個自定義(Directshow)過濾器圖形,並使用Directshow.net Lib的幫助。該庫基於微軟的C++ directshow接口。C#.NET DirectShow Lib:將自定義圖形保存到文件

創建圖表可以工作,我可以添加一個或多個過濾器。但是,試圖將圖保存到文件時,它會寫入一些字節,但Graph編輯器(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

Graph from c# net

Graph made using the editor

MyStreamWriter類:)

IPersistStream.Save(只調用SEEK()和Write()上的StreamWriter。

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++接口提供...所以這個問題必須在別處。

任何幫助真的很感激。

+0

文件不必匹配,字節到字節 – 2014-12-03 17:42:05

回答

3

我認爲解決方案更簡單 CSharp DirectShow lib允許您將圖形保存在文件中。

此代碼適用於我:

使用DirectShowLib.Utils;

FilterGraphTools.SaveGraphFile(this.m_TheGraphBuilder,FilePath);

+0

好吧,我沒有DirectShowLib.Utils,但?我需要添加一個不同的DLL嗎?目前,我有DirectShowLib-2005。 – Gonzalioz 2014-12-04 12:29:51

+0

我也使用DirectShowLib-2004,記得使用DirectShowLib.Utils包含 ;「 – arturn 2014-12-04 14:39:37

+1

好的。原來,你可以在你可以從directshow lib(http://sourceforge.net/projects/directshownet/files/)下載的其中一個示例中找到FilterGraphTools。這工作。 – Gonzalioz 2014-12-04 15:56:33

相關問題