我正在尝试创建一个源过滤器,该过滤器根据一系列图片制作实时视频流。为此,我创建了一个 IUnknown 接口:
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("F18FC642-5BA2-460D-8D12-23B7ECFA8A3D")]
public interface IVirtualCameraFilter_Crop
{
void SetCurrentImage(Bitmap img);
...
};
在我的程序中,我明白了:
pUnk = Marshal.GetIUnknownForObject(sourceFilter);
Marshal.AddRef(pUnk);
filterInterface = Marshal.GetObjectForIUnknown(pUnk) as IVirtualCameraFilter_Crop;
当我传递简单类型时,一切正常。但是当我尝试传递一个 C# Bitmap 对象时,我得到一个错误unable to cast Com object to <my object type>
。或应用程序因错误 APPCRUSH 而关闭。
filterInterface.SetCurrentImage(frame);
我知道这不是正确的方法,但我不知道其他可能的参数传递方式。我试图将 IntPtr 传递给 BitmapData,然后我得到了相同的应用程序迷恋。那么如何将位图传递给 DirectShow 过滤器呢?
结果:完整的代码图片请参考创建界面:
[ComImport, InterfaceType (ComInterfaceType.InterfaceIsIUnknown), Guid ("F18FC642-5BA2-460D-8D12-23B7ECFA8A3D")]
public interface IVirtualCameraFilter_Crop
{
unsafe void SetPointerToByteArr (byte * array, int length);
};
执行:
unsafe public void SetPointerToByteArr (byte * array, int length)
{
this.array = new byte [length];
Marshal.Copy (new IntPtr (array), this.array, 0, length);
}
在应用中:
byte [] text = ... get data;
unsafe
{
fixed (byte * ptr = & text [0])
{
filterInterface.SetPointerToByteArr (ptr, text.Length);
}
}