如何将位图传递给c ++ dll?

时间:2014-06-25 03:15:25

标签: c# c++ .net pinvoke native

程序没有提示错误,但是没有任何图片显示在c ++表格中,经过测试的简单数据类型,如int,可以正常发货,请给我一个简单的例子,告诉我该怎么做

C#代码

[DllImport("dllTestForm.dll", EntryPoint = "showFormC")]
static extern void testShowFormC(byte[] photo,int len);

private void button6_Click(object sender, EventArgs e)
{
        Bitmap bmp = (Bitmap)Image.FromFile(@"d:\1\1.jpg");

        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
        System.Drawing.Imaging.BitmapData bmpData =
        bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,bmp.PixelFormat);

        // Get the address of the first line.
        IntPtr ptr = bmpData.Scan0;

        // Declare an array to hold the bytes of the bitmap. 
        int bytes  = Math.Abs(bmpData.Stride) * bmp.Height;
        byte[] rgbValues = new byte[bytes];

         // Copy the RGB values into the array.
         System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

         // Unlock the bits.
         bmp.UnlockBits(bmpData);

         testShowFormC(rgbValues, bytes);
}

C ++代码

void __stdcall showFormC(byte *photo,int len)
{
    ThelloWorld *a=new ThelloWorld(Application); //winForm,display photo
    a->ImageEnView2->IO->LoadFromBuffer(photo,0,len);  
    a->Show();
}

2 个答案:

答案 0 :(得分:0)

从C#代码获取的缓冲区未完成。

您只获取位图像素数据,但不获取标题。标题包含描述像素数据的信息(8位,16位,24位,调色板等)。

对于a->ImageEnView2->IO->LoadFromBuffer(photo,0,len);,我猜您使用的是TImageEnIO组件。从帮助http://www.imageen.com/help/ImageEn/TImageEnIO.LoadFromBuffer.html,为什么不将整个jpeg文件数据传递给LoadFromBuffer?

答案 1 :(得分:0)

您可以使用Bitmap.Save()方法将其转换为内存流,然后将其发送到C++ dll

image.Save(imageMemoryStream, System.Drawing.Imaging.ImageFormat.Png);

然后,在C++ dll中使用cv::imdecode()方法等解码来获取图像。

详情为here