Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get a native GDI bitmap from a .net bitmap

Tags:

c++

.net

gdi+

gdi

i would like to use a .net Bitmap in a native library using Pinvoke Technology, the native function has the following prototype ,

int cropImage(  bool aBlocking,Gdiplus::Bitmap *aInputImage, )

I worte the following pinvoke

public extern int cropImage( bool blocking , Intptr bitmap ) 

and I am calling it like this

System.Drawing.Bitmap b = new Syste.Drawing.Bitmap("Amour.jpg");
cropImage(true,b.getHitmap());

but it is not working , what do you suggest ?

like image 292
Yamen Ajjour Avatar asked Oct 22 '22 05:10

Yamen Ajjour


1 Answers

Gdiplus::Bitmap is a C++ class, it is declared in the <GdiPlusHeaders.h> Windows SDK header file. It is a wrapper class to make the rather unfriendly low-level GDI+ C interface easier to use. System.Drawing.Bitmap is not a direct substitute for this C++ class, it is also a wrapper class around the low-level GDI+ api but written in C#. It has no relationship at all with the C++ wrapper. Using the HBITMAP you get from Bitmap.GetHbitmap() will not work either, that's a handle, not a C++ object.

You cannot call this function directly from C#, pinvoke does not support creating C++ objects. You will need to write a ref class wrapper in the C++/CLI language. A language that supports both writing managed code and calling native C++ code without pinvoke. And you can #include <gdiplus.h> as necessary so you can create the Gdiplus::Bitmap object. Another approach is to create a DLL using C++ that has two exported functions, one that creates a Gdiplus::Bitmap and another that destroys it. That will let you use pinvoke, declaring the argument as IntPtr instead.

like image 181
Hans Passant Avatar answered Nov 01 '22 11:11

Hans Passant