从dll c和x2B传递mat+;到图像源c# 我使用Visual C 2010,我喜欢从DLL C++发送图像到我的Visual C 2010。该图像具有166×166×24,将图像从DLL C++发送到C。我正在使用以下代码:

从dll c和x2B传递mat+;到图像源c# 我使用Visual C 2010,我喜欢从DLL C++发送图像到我的Visual C 2010。该图像具有166×166×24,将图像从DLL C++发送到C。我正在使用以下代码:,c#,c++,opencv,C#,C++,Opencv,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" { __de

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();}
在我的程序Visual C#中,我使用以下代码:

 [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;



                }

我猜在函数
test()
返回后,指向
cv::Mat
数据的指针不再有效,因为变量
OriginalImg
超出范围,析构函数已清除
OriginalImg
数据。如果可以防止自动销毁变量,请尝试将
OriginalImg
设置为全局


通常,我建议为OpenCV制作一个通用的C++/CLI包装器。对此我有点担心。例如,我现在正在处理与您相同的问题,我正在C++/CLI中创建一个
系统.Drawing.Bitmap
对象,没有任何
DllImport
s…

,问题是…?对不起,这是我第一次在这里问问题,问题是这行:Bitmap Bitmap=new Bitmap(166,166,3*166,System.Drawing.Imaging.PixelFormat.Format24bppRgb,intPtr1);返回错误:参数无效,因此我不知道到底是什么问题。因此您遇到编译问题?请粘贴编译器错误消息。调试文件时,它停止在此行:调试文件时,它停止在此行:位图位图=新位图(166,166,3*166,System.Drawing.Imaging.PixelFormat.Format24bppRgb,intPtr1);指向显示无效参数消息的fromat24bppRgb,但如果有更多问题,请在此处发布。