[英]how to pass bitmap to c++ dll?
The program has no tip errors ,But there is no any picture display in c++ form,Tested the simple data types, such as int, can be normal delivery,please give me a simple example ,tell me what to do 该程序没有提示错误,但是没有任何以c ++格式显示的图片,测试了简单的数据类型,例如int,可以正常交付,请给我一个简单的例子,告诉我该怎么做
C# Code 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++ Code 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();
}
Buffer got from C# code is not completed. 从C#代码获取的缓冲区未完成。
You only get bitmap pixel data but not header. 您只会获得位图像素数据,而不会获得标头。 Header contains info to describe pixel data (8 bit, 16 bit, 24 bit, color palette and so on).
标头包含描述像素数据的信息(8位,16位,24位,调色板等)。
For a->ImageEnView2->IO->LoadFromBuffer(photo,0,len);
对于
a->ImageEnView2->IO->LoadFromBuffer(photo,0,len);
, I guess you are using TImageEnIO component. ,我猜您正在使用TImageEnIO组件。 From help http://www.imageen.com/help/ImageEn/TImageEnIO.LoadFromBuffer.html , why not pass whole jpeg file data to LoadFromBuffer ?
从帮助http://www.imageen.com/help/ImageEn/TImageEnIO.LoadFromBuffer.html中 ,为什么不将整个jpeg文件数据传递给LoadFromBuffer?
You can use Bitmap.Save()
method to convert it to a memory stream and then send it to C++ dll
. 您可以使用
Bitmap.Save()
方法将其转换为内存流,然后将其发送到C++ dll
。
image.Save(imageMemoryStream, System.Drawing.Imaging.ImageFormat.Png);
Then, in C++ dll
use decoding such as cv::imdecode()
method to get the image. 然后,在
C++ dll
使用诸如cv::imdecode()
方法之类的解码来获取图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.