读书人

关于位图的一点小疑点

发布时间: 2013-01-28 11:49:56 作者: rapoo

求助,关于位图的一点小问题
求各位大神帮帮忙 我今天写了个判断两张位图是否相同的程序,我先把位图异或,然后调用下面的函数判断不为黑色的部分,并返回矩形区域。我用GetBitmapBits将像素值拷备到数组中,可是在下面的函数中,当i循环到7178的时候,数组后面的值就全是一些随机数了,而不是位图的像素值,不知道为什么 求大神解答




/* ==========================================================
* 开发人员:
* 编写时间:2013/1/22
* 函数名称:RECT IsDifferent(HBITMAP Hbmp);
* 参数说明:HBITMAP Hbmp;//按布尔型异或后的位图句柄
* 返 回 值:RECT //返回一个矩形结构
* 功能说明:找出位图中不为黑色的部分,返回该部分的矩形区域
*/
RECT IsDifferent(HBITMAP Hbmp)//按布尔型异或后的位图句柄
{
RECT rect;
BITMAP bmp;
GetObject(Hbmp,sizeof(bmp), &bmp);
rect.top=bmp.bmHeight;
rect.left=bmp.bmWidth;
rect.right=0;
rect.bottom=0;
//int px[169*169];
int *px = new int [bm.bmHeight * bm.bmWidth];
int b=GetBitmapBits(Hbmp,bmp.bmHeight * bmp.bmWidth,px);
for (int i=0;i<bmp.bmHeight * bmp.bmWidth;i++)
{
if (0!=px[i])
{
i/bmp.bmWidth>rect.bottom ? rect.bottom=i/bmp.bmWidth:rect.bottom;
i/bmp.bmWidth<rect.top ? rect.top=i/bmp.bmWidth:rect.top;
i%bmp.bmWidth<rect.left ? rect.left=i%bmp.bmWidth:rect.left;
i%bmp.bmWidth>rect.right ? rect.right=i%bmp.bmWidth:rect.right;
}
}
return rect;
}





[解决办法]
仔细看 BITMAP 在msdn的描述,位图象素的存储跟每个像素用多少个字节(bmBitsPixel)保存,还有每一行象素使用多少个字节(bmWidthBytes)相关,假设每一个象素用3个字节描述,每一行有15个象素,那么每一行象素使用多少个字节来保存是由 bmWidthBytes 来决定的,而不是你想的 3 x 15 = 45 (bytes)

读书人网 >C++

热点推荐