C# Marshal / Pinvoke CBitmap

cpinvoke

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class.

My import looks like this:

[DllImport(@"test.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr GetBitmap(System.IntPtr hwnd, int nBMPWIDTH, int nBMPHEIGHT);

The H file section looks like:

Cbitmap* GetBitmap(HWND hwnd,int nBMPWIDTH,int nBMPHEIGHT);

How do I go about converting the Cbitmap into something I can display in C#?

Many thanks.

Best Answer

The closest API in the .NET Framework is Image.FromHbitmap, but this would require you to extract the HBITMAP from the CBitmap, which you can't do in C# (at least not without knowing the internals of CBitmap). If it's possible for you to modify your C++ GetBitmap function to return the HBITMAP rather than the CBitmap wrapper, that would be the easiest solution. Is that an option for you?

Related Topic