How to pass .NET Bitmap to native DLL?

Here is what I have:

  • An external DLL, I wrote in C. This DLL is a link to opencv. I do not want to call OpenCV directly from C # - there is already a huge amount of C code, access to OpenCV, which will be used in the DLL. There is an exported function: passbitmap (void * mem, size_t s);

  • C # project from which I want to call a DLL.

  • The System.Drawing.Bitmap object from which I want to transfer pixel / bitmap data somehow to my DLL.

I assume this is some kind of P / Invoke, but I have never done it and I don’t know how I can do it right in this case. How do I proceed?

+1
source share
2 answers

Bitmap.GetHbitmap(), .

- interop GetDIBits(). , , DLL GetDIBits() win32, interop.

CreateCompatibleDC() DC , GetDC (NULL), CreateCompatibleBitmap() DC, . GetDIBits().

+4

C , , , - $64K. / , Bitmap ByteArray. ...

, ...

[untested, , )

* , , -
*
* .
* C dll
* ,

,

IntPtr pnt = Marshal.AllocHGlobal(size);

,

Marshal.Copy(theByteArray, 0, pnt, theByteArray.Length);

, - , .

Marshal.FreeHGlobal(ptr)

, , @jeffamaphone, c-, .

+1

All Articles