简体   繁体   English

将位图从 C# 传递到 C++

[英]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++我如何将从 C# 加载的位图对象传递给 C++ 本机代码,实际上我正在包装一些要从 C# 使用的 OpenCV API 但是我在从 C# 传递位图并在 C++ 上重建它时遇到了问题

any code or idea would be appreciated任何代码或想法将不胜感激

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.您可以使用LockBits方法锁定内存中的Bitmap像素,并使用 P/Invoke 调用将指向第一个像素的指针、尺寸和跨度传递给 C++。

Overview of LockBits : https://web.archive.org/web/20141229164101/http://bobpowell.net/lockingbits.aspx 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 .在本机代码完成图像处理后,您可以使用UnlockBits释放锁。

If you have a .NET Bitmap object you can use the function GetHBitmap() to get a pointer to the image acessible from unmanaged code.如果您有一个 .NET 位图对象,您可以使用函数GetHBitmap()来获取指向可从非托管代码访问的图像的指针。

Instead of implementing your own wrapper, you can use EmguCV .您可以使用EmguCV而不是实现自己的包装器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM