c#怎么把Bitmap类型数据传送给c++的dll?

marklr 2014-06-25 10:42:03
程序没有报错,但就是在c++的Form界面不显示图片,测试int等简单类型,可以正常传值,我曾经试过把c++接口的参数更改为 showFormC(TMemoryStream *photo)等类型,但还是不行,请这方面有经验的指点一下,怎么传递图片类型的数据,给个简单例子

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

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

void __stdcall showFormC(byte *photo,int len)
{
ThelloWorld *a=new ThelloWorld(Application);//这是一个winForm测试界面,显示图片用
a->ImageEnView2->IO->LoadFromBuffer(photo,0,len);
a->Show();

}

...全文
283 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
marklr 2014-06-26
  • 打赏
  • 举报
回复
引用 1 楼 Hassle 的回复:
貌似仅传了位图数据过去,文件头、信息头、调色板呢? 建议了解一下Bitmap文件格式。
这部分怎么写?请给个简单例子吧,谢谢?
ching126 2014-06-25
  • 打赏
  • 举报
回复
[DllImport("dllTestForm.dll", EntryPoint = "showFormC")] static extern void testShowFormC(byte[] photo,int len); void __stdcall showFormC(byte *photo,int len) { ThelloWorld *a=new ThelloWorld(Application);//这是一个winForm测试界面,显示图片用 a->ImageEnView2->IO->LoadFromBuffer(photo,0,len); a->Show(); } 你这里肯定不对了,你把传过来的byte[]数组直接当指针用了,那肯定不行,你的这个void __stdcall showFormC(byte *photo,int len)第一个参数是指针,你的将byte[]类型的数组拷贝到指针即可,可以参考Marshal.Copy()这个方法的具体用法
Hassle 2014-06-25
  • 打赏
  • 举报
回复
貌似仅传了位图数据过去,文件头、信息头、调色板呢? 建议了解一下Bitmap文件格式。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧