[英]Retrieve C# .NET image from C++ unmanaged application
I'm using openFrameworks to render a video stream. 我正在使用openFrameworks渲染视频流。 OpenFrameworks uses the
ofImage
class, which stores the images as a vector of ofPixel
(which stores an unsigned char
vector). OpenFrameworks使用
ofImage
类,该类将图像存储为ofPixel
的矢量(存储unsigned char
矢量)。
I am using a C# .NET dll to retrieve each frame. 我正在使用C#.NET dll检索每个帧。 This library uses
Bitmap
to return the retrieved frames. 该库使用
Bitmap
返回检索到的帧。 So far I know C# .NET works under the Common Language Runtime . 到目前为止,我知道C#.NET在Common Language Runtime下工作 。
My question is, what .NET tools or functionalities can I use in order to comunicate native C++ code and managed C#? 我的问题是,我可以使用哪些.NET工具或功能来通信本机C ++代码和托管C#?
In order to retrieve the image I used this tutorial . 为了检索图像,我使用了本教程 。 It thoroughly explains how to consume a C# managed library from unmanaged code.
它彻底解释了如何从非托管代码使用C#托管库。
The process I followed is documented here . 我遵循的过程在此处记录 。
Here's an overview 这是概述
About the image information, it can be referred to as a byte array. 关于图像信息,它可以被称为字节数组。 It has to be pinned in the managed code using a GCHandle.
必须使用GCHandle将其固定在托管代码中。 Here is some code to illustrate this concept (Disclaimer, this answer was provided by another author in the original msdn forum post ):
以下是一些代码来说明此概念(免责声明,此答案由原msdn论坛帖子中的另一位作者提供):
#include <unordered_map>
using namespace System;
using namespace System::Runtime::InteropServices;
std::unordered_map<void *, void *> dataHandles;
void *getData() {
array<Byte>^ arr = gcnew array<Byte>(2);
arr[0] = 'C';
arr[1] = '#';
GCHandle handle = GCHandle::Alloc(arr, GCHandleType::Pinned);
void *data = (void *)handle.AddrOfPinnedObject();
dataHandles.insert(std::make_pair(data, (void *)GCHandle::ToIntPtr(handle)));
return data;
}
void releaseData(void *data) {
auto i = dataHandles.find(data);
if (i != dataHandles.end())
GCHandle::FromIntPtr((IntPtr)i->second).Free();
}
#pragma unmanaged
void native() {
char *data = (char *)getData();
for (int i = 0; i < 2; i++)
printf("%c ", data[i]);
releaseData(data);
}
#pragma managed
int main(array<System::String ^> ^args) {
native();
return 0;
}
More about specific Bitmap
to unsigned char *
conversion here 有关将特定
Bitmap
转换为unsigned char *
更多信息,请unsigned char *
此处
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.