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
  • 800
  • 1
  • 5
  • 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
  • 22,953
  • 9
  • 55
  • 76
Rotem
  • 20,778
  • 6
  • 60
  • 108
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
  • 8,662
  • 3
  • 24
  • 39
0

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

phyrox
  • 2,383
  • 14
  • 23