Niklaus Wirth
Niklaus Wirth

Reputation: 870

Pass bitmap from C# to C++

How can I pass a Bitmap object which is loaded from C# to C++ native code, actually I'm working on wrapping some OpenCV API to be used from C# But I faced a problem in passing the bitmap from c# and reconstruct it on c++

any code or idea would be appreciated

Upvotes: 0

Views: 1772

Answers (3)

Rotem
Rotem

Reputation: 21947

You can use the LockBits method to lock the Bitmap pixels in memory, and pass the pointer to the first pixel, the dimensions and the stride to C++ using a P/Invoke call.

Overview of LockBits: https://web.archive.org/web/20141229164101/http://bobpowell.net/lockingbits.aspx

After the native code has finished processing the image, you would then free the lock using UnlockBits.

Upvotes: 1

phyrox
phyrox

Reputation: 2449

Instead of implementing your own wrapper, you can use EmguCV.

Upvotes: 0

mortb
mortb

Reputation: 9869

If you have a .NET Bitmap object you can use the function GetHBitmap() to get a pointer to the image acessible from unmanaged code.

Upvotes: 0

Related Questions