I'm trying to create a source filter that makes a live video stream based on a sequence of pictures. To do this, I make an interface of IUnknown:
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("F18FC642-5BA2-460D-8D12-23B7ECFA8A3D")]
public interface IVirtualCameraFilter_Crop
{
void SetCurrentImage(Bitmap img);
...
};
And in my programm I get it:
pUnk = Marshal.GetIUnknownForObject(sourceFilter);
Marshal.AddRef(pUnk);
filterInterface = Marshal.GetObjectForIUnknown(pUnk) as IVirtualCameraFilter_Crop;
When I pass simple types everything works fine. But when I try to pass a C# Bitmap object I get an error unable to cast Com object to <my object type>
. Or application shutting down with error APPCRUSH.
filterInterface.SetCurrentImage(frame);
I understand that it`s not the correct way but I do not know other possible ways of passing parameters. I tried to pass IntPtr to BitmapData and then I get the same application crush. So How can I pass the bitmap to the DirectShow filter?
Result: For a complete picture of the code cite Create an interface:
[ComImport, InterfaceType (ComInterfaceType.InterfaceIsIUnknown), Guid ("F18FC642-5BA2-460D-8D12-23B7ECFA8A3D")]
public interface IVirtualCameraFilter_Crop
{
unsafe void SetPointerToByteArr (byte * array, int length);
};
implementation:
unsafe public void SetPointerToByteArr (byte * array, int length)
{
this.array = new byte [length];
Marshal.Copy (new IntPtr (array), this.array, 0, length);
}
In application:
byte [] text = ... get data;
unsafe
{
fixed (byte * ptr = & text [0])
{
filterInterface.SetPointerToByteArr (ptr, text.Length);
}
}