Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
556 views
in Technique[技术] by (71.8m points)

c# - How can I pass a .NET Bitmap to a native DLL?

Here is what I have:

  • An external DLL, I have written in C. This DLL links to opencv. I don't want to call OpenCV directly from C# - there are already huge amounts of C code accessing OpenCV which will be used in the DLL. There is an exported function: passbitmap(void *mem, size_t s);

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

  • A System.Drawing.Bitmap object from which I want to pass the pixel/bitmap data somehow to my DLL.

I guess its some kind of P/Invoke, but I have never done it, and don't know how can I do it properly in this case. How should I proceed?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to call Bitmap.GetHbitmap() to get the native handle to the bitmap.

Then you basically want to do some native interop and call GetDIBits(). It may be better to have your target dll actually call GetDIBits() and just pass the win32 handle, rather than do the interop.

You'll need to call CreateCompatibleDC() on the desktop DC which you get from GetDC(NULL), then CreateCompatibleBitmap() on the DC you just made. Then call GetDIBits().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...