Efficient way to pass Bitmap into registered COM component


I have registered COM Class and interface written in .NET 2.0 . Now i am looking for a way to pass Bitmap object from WindowsForms .Net 4.5 application into COM component.

When i try to do this code:

 Type type = Type.GetTypeFromCLSID(clsid);
 source = (IBaseFilter) Activator.CreateInstance(type);
        public void SetBitmap(Bitmap input)
 IImageSourceFilter advSource = (IImageSourceFilter)source;

I get next exception

  Message=Unable to cast COM object of type 'System.__ComObject' to class type 'System.Drawing.Bitmap'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
   at RTSPTestAPP.Camera.BMPtoCam.IImageSourceFilter.SetBitmapObj(Object inp)
   at RTSPTestAPP.Camera.BMPtoCam.SetBitmap(Bitmap input) in C:\Users\...\BMPtoCam.cs:line 43

Passing it as byte array is not efficient and locates a lot CPU resources (and i am sending alot bitmaps from decoded videostream).

asked on Stack Overflow May 24, 2019 by XLR8

1 Answer


Found a way: i just develop COM component with same .net as program which will use it. Then on Type type = Type.GetTypeFromCLSID(clsid); i'll get not raw type (System.__ComObject), instead i`ll get correct interface, with correct methods. And i just pass bitmap as argument. Its important too add reference to this assembly too.

answered on Stack Overflow May 27, 2019 by XLR8

User contributions licensed under CC BY-SA 3.0