将mat从DLL c++传递到图像资源c#
pass mat from dll c++ to imagesource c#
我正在使用Visual c# 2010,我喜欢从DLL c++发送图像到我的Visual c# 2010。图像有166*166*24从DLL c++发送到c#。我使用以下代码:
main.cpp
unsigned char* test()
{
read image from filename
Mat OriginalImg = imread(fileName, CV_LOAD_IMAGE_COLOR);
return (OriginalImg.data);}
OriginalImg.data return the pointer to the image.
main.h
extern "C" { __declspec(dllexport) unsigned char* test();}
,在我的visualc#程序中,我使用以下代码:
[DllImport("testng.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr test();
IntPtr intPtr1;
BitmapImage bitmapImage;
calling the c++ dll
intPtr1 = test(1);
if (intPtr1.ToString().CompareTo("0") != 0)
{
create a bitmap with the pointer
Bitmap bitmap = new Bitmap(166, 166, 3 * 166, System.Drawing.Imaging.PixelFormat.Format24bppRgb, intPtr1);
to show the bitmap i need to convert it to a bitmap image
bitmapImage=convertBitmapToBitmapImage(bitmap);
System.Windows.Media.ImageBrush ib = new System.Windows.Media.ImageBrush();
ib.ImageSource = bitmapImage;
i put the image received from c++ dll like a background to my canvas: canvasImage
windows.canvasImage.Background = ib;
}
BitmapImage convertBitmapToBitmapImage(Bitmap bitmap)
{
IntPtr hBitmap = bitmap.GetHbitmap();
BitmapSource retval;
try
{
retval = Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
finally
{
//DeleteObject(hBitmap);
}
return (BitmapImage)retval;
}
我的猜测是指向cv::Mat
数据的指针在函数test()
返回后不再有效,因为变量OriginalImg
超出了范围,析构函数已经清理了OriginalImg
数据。如果可能的话,请尝试将OriginalImg
设置为全局,以防止自动销毁变量。
一般来说,我建议为OpenCV制作一个通用的c++/CLI包装器。A为此发了一个帖子。例如,我现在正在处理与您相同的问题,我正在C++/CLI中创建System.Drawing.Bitmap
对象,而没有任何DllImport
s…
相关文章:
- 具有瞬态资源的RAII类
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- QML:设置动画图像源,而不加载资源中的文件
- 如何替换QML(QT)文件上的图像中的图像中没有资源中的图像
- MFC-无法从资源输出BMP图像
- 让主程序知道资源在哪里(图像、数据等)
- 使用 CMake 将资源(例如着色器代码、图像)嵌入到可执行文件/库中
- 从 WinAPI C 项目中的专用 DLL 加载图像资源
- 如果我在Visual Studio项目中将图像作为资源包含在内,这些图像是否会编译到可执行文件中
- 无法加载Qt5/cmake项目中Qt资源文件中指定的图像
- QImage,可以更改资源图像的背景(png/jpg)
- Gdiplus::图标资源中的图像
- 从资源加载图像失败
- 如何从资源中的图像数组中获得一个图像的HBITMAP
- QT资源文件-图像不显示时运行
- 从visual studio IDE运行时,项目无法加载图像资源
- c++位图资源(RT_BITMAP)和PNG图像格式
- 将mat从DLL c++传递到图像资源c#
- 从资源加载图像不起作用(C++ Dll)
- 在代码中嵌入图像,而不使用资源节或外部图像