通过.NET Bitmap对象以COM(DirectShow过滤器) [英] Pass .NET Bitmap object to COM (DirectShow filter)

查看:198
本文介绍了通过.NET Bitmap对象以COM(DirectShow过滤器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个源过滤器,使得基于图像序列中的实时视频流。 要做到这一点,我做的IUnknown的接口:

  [ComImport,InterfaceType(ComInterfaceType.InterfaceIsIUnknown)的Guid(F18FC642-5BA2-460D-8D12-23B7ECFA8A3D)]
公共接口IVirtualCameraFilter_Crop
{
    无效SetCurrentImage(位图IMG);
    ...
};
 

在我PROGRAMM我明白了:

 朋克= Marshal.GetIUnknownForObject(sourceFilter);
Marshal.AddRef(朋克);
filterInterface = Marshal.GetObjectForIUnknown(朋克)为IVirtualCameraFilter_Crop;
 

当我经过简单类型一切工作正常。但是,当我试图通过一个C#位图对象,我得到一个错误无法施展的COM对象到<我的对象类型> 。或应用程序关闭错误APPCRUSH。

  filterInterface.SetCurrentImage(架);
 

据我所知,它`不正确的做法,但我不知道传递参数的其他可能的方式。我试图通过的IntPtr来的BitmapData,然后我得到了相同的应用程序美眉。 那么,如何可以通过位图的DirectShow过滤器?

结果: 对于的code一个完整的画面举 创建接口:

  [ComImport,InterfaceType(ComInterfaceType.InterfaceIsIUnknown)的Guid(F18FC642-5BA2-460D-8D12-23B7ECFA8A3D)]
公共接口IVirtualCameraFilter_Crop
{
    不安全无效SetPointerToByteArr(BYTE *数组,INT的长度);
};
 

执行:

 不安全公共无效SetPointerToByteArr(BYTE *数组,INT的长度)
{
this.array =新的字节[长度];
Marshal.Copy(新的IntPtr(阵列),this.array,0,长度);
}
 

在应用程序:

  byte []的文字= ...获取数据;
不安全
{
    固定(BYTE * PTR =&放大器;文[0])
{
filterInterface.SetPointerToByteArr(PTR,text.Length);
}
}
 

解决方案

System.Drawing.Bitmap是一个.NET类型,而不是COM类型,并且在COM没有类似的,所以你不能把它作为一个参数一个COM接口。

无论是使用COM接口的的IStream ,这是不容易在C#中使用,因为.NET的MemoryStream没有实现它,或者使用COM接口的 IPICTURE 或的字节的只是阵列

另外要注意,你的DirectShow过滤器通常被称为一个线程,是不是UI线程,所以你应该采取把适当的锁定机制的过滤器内部的照顾。

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); 
     }
}

解决方案

System.Drawing.Bitmap is a .net type, not COM type, and there is no equivalent for it in COM, so you cannot use it as a parameter of a COM interface.

Either use the COM interface IStream, which is not easy to use in C# since .net MemoryStream does not implements it, or use the COM interface IPicture, or just an array of bytes.

Also be aware that your DirectShow filter will usually be called in a thread that is not the UI thread, so you are supposed to take care of putting the proper lock mechanisms inside your filter.

这篇关于通过.NET Bitmap对象以COM(DirectShow过滤器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆