5

我想在使用 Pinvoke 技术的本机库中使用 .net 位图,本机函数具有以下原型,

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

我写了以下 pinvoke

public extern int cropImage( bool blocking , Intptr bitmap ) 

我这样称呼它

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

但它不起作用,你有什么建议?

4

2 回答 2

3

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.

于 2013-07-15T12:17:58.740 回答
0

我建议解决这个问题的最好方法是使用一个接受 HBitmap 的本机库,然后使用 CBitmap::fromHandle 函数,然后调用您尝试与之通信的 api!

于 2013-07-15T09:54:20.160 回答