C# 如何将托管位图从C传递到包装的非托管OpenCV';垫子
我有一个包含3个部分的项目:C# 如何将托管位图从C传递到包装的非托管OpenCV';垫子,c#,c++,opencv,bitmap,mat,C#,C++,Opencv,Bitmap,Mat,我有一个包含3个部分的项目: 托管C#项目,带有回调,该回调给我一个位图(它应该具有PixelFormat=Format24bppRgb)。我尝试了几种方法将位图转换为可以传递到第2部分的内容,这是我最后尝试的: public int BufferCB(IntPtr pBuffer, int BufferLen) { byte[] aux = new byte[BufferLen]; Marshal.Copy(pBuffer, aux, 0, BufferLen); St
public int BufferCB(IntPtr pBuffer, int BufferLen)
{
byte[] aux = new byte[BufferLen];
Marshal.Copy(pBuffer, aux, 0, BufferLen);
String s_aux = System.Text.Encoding.Default.GetString(aux);
wrappedClassInstance.GetBitmap(s_aux);
}
int WrappedClass::GetBitmap(array<System::Byte>^ s_in) {
pin_ptr<unsigned char> pin = &s_in[0];
unsigned char* p = pin;
return privateImplementation->GetBitmap(p);
}
[DllImport("mydll.dll", ...)]
static extern void GetBitmap(IntPtr pBuffer, int h, int w);
void TestMethod(IntPtr pBuffer, int BufferLen)
{
// h and w defined elsewhere
// GetBitmap is essentially the same as in item 3.
GetBitmap(pBuffer, BufferLen, h, w);
}
这是可行的(它将位图保存到文件中),但因为DLL一直保持连接状态,直到我终止进程,所以解决方案对我来说不够好。我也不想将Mat类“镜像”到我的项目中,因为我知道Mat应该接受来自某个char*的数据
请帮忙,我怎么做?我是否进行了错误的类型转换
谢谢。为了确保这一点,我将第2部分改为memcpy非托管内存,改为本机内存。但我的代码的真正问题是,每当我想要得到一个新的Mat时,我都会调用Mat构造函数:
Mat myMat;
int MyClass::GetBitmap(unsigned char* s_in) {
myMat = Mat(...)
}
相反,我做到了:
Mat myMat;
int MyClass::GetBitmap(unsigned char* s_in) {
Mat aux;
aux = Mat(...)
aux.copyTo(myMat);
aux.release();
}
。。。现在我的代码工作得很好
编辑:
删除了一些使用新Mat(…)的部分,这是一个输入错误,它不会编译,因为我使用的是Mat,而不是Mat*我对第2部分做了一些修改,现在已经编辑好了。如果发现了,我想我必须先将数组复制到非托管堆中,然后再将指针传递给我的私有实现,或者弄清楚如何在C#中固定IntPtr。
Mat myMat;
int MyClass::GetBitmap(unsigned char* s_in) {
Mat aux;
aux = Mat(...)
aux.copyTo(myMat);
aux.release();
}