pass mat from dll c++ to imagesource c# -


i'm working visual c# 2010, , send image dll c++ visual c# 2010. image has 166*166*24 send image dll c++ c#. i'm using code:

main.cpp

unsigned char*   test()         { read image filename         mat originalimg = imread(filename, cv_load_image_color);           return (originalimg.data);}    originalimg.data return pointer image. 

main.h

 extern "c" { __declspec(dllexport) unsigned char* test();} 

and in program visual c#, use code:

 [dllimport("testng.dll", callingconvention = callingconvention.cdecl)]                private static extern intptr test();          intptr intptr1;                      bitmapimage bitmapimage;         calling c++ dll                     intptr1 = test(1);                      if (intptr1.tostring().compareto("0") != 0)                              {      create bitmap pointer                              bitmap bitmap = new bitmap(166, 166, 3 * 166, system.drawing.imaging.pixelformat.format24bpprgb, intptr1); show bitmap need convert bitmap image                     bitmapimage=convertbitmaptobitmapimage(bitmap);                      system.windows.media.imagebrush ib = new system.windows.media.imagebrush();                     ib.imagesource = bitmapimage; put image received c++ dll background 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());                         }                                            {                            //deleteobject(hbitmap);                          }                       return (bitmapimage)retval;                    } 

my guess pointer cv::mat data no longer valid after function test() returns, because variable originalimg gets out of scope, destructors have cleaned originalimg data. try making originalimg global if possible prevent automatic destroy of variable.

in general, recommend making generic c++/cli wrapper opencv. made a post that. example, i'm working on same problem , i'm creating system.drawing.bitmap object in c++/cli without dllimports...


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -