0

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

Niklaus Wirth
  • 820
  • 1
  • 7
  • 21
  • This has been asked and answered before: http://stackoverflow.com/questions/661016/how-can-i-pass-a-net-bitmap-to-a-native-dll?rq=1 – Security Hound Nov 28 '12 at 12:49

3 Answers3

1

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.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Rotem
  • 21,452
  • 6
  • 62
  • 109
0

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

mortb
  • 9,361
  • 3
  • 26
  • 44
0

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

phyrox
  • 2,423
  • 15
  • 23