求教:如何将二维数组用图像显示 出来

水瓶06 2010-05-31 05:33:01
我程序中得到了一个比较大的二维数组,256*256的,想用图像的形式把它显示在一个对话框里,
在网上搜索后用了下面的代码:但是显示的不对,
int iWidth, iHeight, iBytesWidth;
iWidth = 256;
iHeight = 256;

iBytesWidth = (iWidth % 4) ? (iWidth / 4 + 1) * 4 : iWidth;
HANDLE hMem = GlobalAlloc(GMEM_MOVEABLE, iBytesWidth * iHeight * 3);
BYTE *Image = (BYTE*)GlobalLock(hMem);

for(i=0;i<16;i++) //应该是这里赋值的问题,但是为什么错了呢,应该怎样呢?
for(j=0;j<16;j++)
{
*(Image+i*iBytesWidth+j*3)=int(255*arry0[i][j]);
*(Image+1+i*iBytesWidth+j*3)=int(255*arry0[i][j]);
*(Image+2+i*iBytesWidth+j*3)=int(255*arry0[i][j]);
}

BITMAPINFO bmi;
ZeroMemory(&bmi, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = iWidth;
bmi.bmiHeader.biHeight = iHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;
::SetDIBitsToDevice(dc.GetSafeHdc(), 0, 0, iWidth, iHeight, 0, 0, 0, iHeight, Image, &bmi, DIB_RGB_COLORS);
GlobalUnlock(hMem);
GlobalFree(hMem);
...全文
1213 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
我是你凯凯哥 2012-11-27
  • 打赏
  • 举报
回复
楼主,你的程序的工作空间能发给我看一下吗,我最近也在做这个~
kkk3108001227 2011-05-05
  • 打赏
  • 举报
回复
怎么改呀,求代码!
水瓶06 2010-05-31
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 gudufuyun 的回复:]
我已经提到了你的iBytesWidth计算有问题啊
不光光是加那句话,还需要改iBytesWidth。

另外位图是以左下角为起始点的,你的数组循环方式要注意下。
这是你方向的问题
[/Quote]

哦,原来是这样,“位图是以左下角为起始点的,你的数组循环方式要注意下”,呵呵,非常感谢,我来改一下。。。
还有我的iByteWidth的确用的有问题,有的地方当成了像素宽度,有的地方当成了字节宽度,后来改了所以对了,谢谢了,呵呵
gudufuyun 2010-05-31
  • 打赏
  • 举报
回复
我已经提到了你的iBytesWidth计算有问题啊
不光光是加那句话,还需要改iBytesWidth。

另外位图是以左下角为起始点的,你的数组循环方式要注意下。
这是你方向的问题
水瓶06 2010-05-31
  • 打赏
  • 举报
回复
现在弄出来了,贴出现在的代码

int iWidth, iHeight, iBytesWidth;
iWidth = 16;
iHeight = 16;

iBytesWidth = ((iWidth*3) % 4) ? (iWidth*3 / 4 + 1) * 4 : (iWidth*3);
HANDLE hMem = GlobalAlloc(GMEM_MOVEABLE, iBytesWidth * iHeight);
BYTE *Image = (BYTE*)GlobalLock(hMem);
//memset(Image, 255, iBytesWidth * iHeight * 3);
for(i=0;i<16;i++)
for(j=0;j<16;j++)
{
*(Image+i*iBytesWidth+j*3)=int(25500*arry0[i][j]);
*(Image+1+i*iBytesWidth+j*3)=int(25500*arry0[i][j]);
*(Image+2+i*iBytesWidth+j*3)=int(25500*arry0[i][j]);

}

BITMAPINFO bmi;
ZeroMemory(&bmi, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = iWidth;
bmi.bmiHeader.biHeight = iHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;

bmi.bmiHeader.biSizeImage = iBytesWidth * iHeight ;
//::SetDIBitsToDevice(dc.GetSafeHdc(), 0, 0, iWidth, iHeight, 0, 0, 0, iHeight, Image, &bmi, DIB_RGB_COLORS);
::StretchDIBits(dc.GetSafeHdc(),0,0,128,128,0,0,16,16,Image,&bmi,DIB_RGB_COLORS,SRCCOPY);
GlobalUnlock(hMem);
GlobalFree(hMem);

现在是一个16*16的二维数组,但是有一个问题,就是显示的图像好像是方向是不对的,应该是按照主对角线对称的,现在却是关于副对角线对称。。。可能是什么问题呢
水瓶06 2010-05-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bobob 的回复:]
arry0是什么类型?
[/Quote]

arry0是我的含有数据的二维数组,因为里面的数据是归一化的,所以乘以255再进行图像显示……
水瓶06 2010-05-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gudufuyun 的回复:]
iBytesWidth = (iWidth % 4) ? (iWidth / 4 + 1) * 4 : iWidth;
//改为
iBytesWidth=(iWidth*24+31)/32*4; 一个像素占3个字节,24位。然后转化为4字节整数倍

不是256X256吗?
怎么循环i j成了16?

另外还需指定
bmi.bmiHeader.biSizeImage = (Widt……
[/Quote]

不好意思,那个地方是粘贴错了
i,j的循环都是256
加上那句话我试了下,还是不行的……
gudufuyun 2010-05-31
  • 打赏
  • 举报
回复
iBytesWidth = (iWidth % 4) ? (iWidth / 4 + 1) * 4 : iWidth;
//改为
iBytesWidth=(iWidth*24+31)/32*4; 一个像素占3个字节,24位。然后转化为4字节整数倍

不是256X256吗?
怎么循环i j成了16?

另外还需指定
bmi.bmiHeader.biSizeImage = (Width*24+31)/32*4*Height;
bobob 2010-05-31
  • 打赏
  • 举报
回复
arry0是什么类型?

19,469

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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