分享
 
 
 

位图快速转化成区域

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

BMP2RGN

BMP2RGN

针对各种颜色的HBITMAP转化为region。

HRGN BitmapToRegion (HBITMAP hBmp, COLORREF cTransparentColor = 0, COLORREF cTolerance = 0x101010)

{

HRGN hRgn = NULL;

if (hBmp)

{

// Create a memory DC inside which we will scan the bitmap content

HDC hMemDC = CreateCompatibleDC(NULL);

if (hMemDC)

{

// Get bitmap size

BITMAP bm;

GetObject(hBmp, sizeof(bm), &bm);

// Create a 32 bits depth bitmap and select it into the memory DC

BITMAPINFOHEADER RGB32BITSBITMAPINFO = {

sizeof(BITMAPINFOHEADER), // biSize

bm.bmWidth, // biWidth;

bm.bmHeight, // biHeight;

1, // biPlanes;

32, // biBitCount

BI_RGB, // biCompression;

0, // biSizeImage;

0, // biXPelsPerMeter;

0, // biYPelsPerMeter;

0, // biClrUsed;

0 // biClrImportant;

};

VOID * pbits32;

HBITMAP hbm32 = CreateDIBSection(hMemDC, (BITMAPINFO *)&RGB32BITSBITMAPINFO, DIB_RGB_COLORS, &pbits32, NULL, 0);

if (hbm32)

{

HBITMAP holdBmp = (HBITMAP)SelectObject(hMemDC, hbm32);

// Create a DC just to copy the bitmap into the memory DC

HDC hDC = CreateCompatibleDC(hMemDC);

if (hDC)

{

// Get how many bytes per row we have for the bitmap bits (rounded up to 32 bits)

BITMAP bm32;

GetObject(hbm32, sizeof(bm32), &bm32);

while (bm32.bmWidthBytes % 4)

bm32.bmWidthBytes++;

// Copy the bitmap into the memory DC

HBITMAP holdBmp = (HBITMAP)SelectObject(hDC, hBmp);

BitBlt(hMemDC, 0, 0, bm.bmWidth, bm.bmHeight, hDC, 0, 0, SRCCOPY);

// For better performances, we will use the ExtCreateRegion() function to create the

// region. This function take a RGNDATA structure on entry. We will add rectangles by

// amount of ALLOC_UNIT number in this structure.

#define ALLOC_UNIT 100

DWORD maxRects = ALLOC_UNIT;

HANDLE hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects));

RGNDATA *pData = (RGNDATA *)GlobalLock(hData);

pData->rdh.dwSize = sizeof(RGNDATAHEADER);

pData->rdh.iType = RDH_RECTANGLES;

pData->rdh.nCount = pData->rdh.nRgnSize = 0;

SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);

// Keep on hand highest and lowest values for the "transparent" pixels

BYTE lr = GetRValue(cTransparentColor);

BYTE lg = GetGValue(cTransparentColor);

BYTE lb = GetBValue(cTransparentColor);

BYTE hr = min(0xff, lr + GetRValue(cTolerance));

BYTE hg = min(0xff, lg + GetGValue(cTolerance));

BYTE hb = min(0xff, lb + GetBValue(cTolerance));

// Scan each bitmap row from bottom to top (the bitmap is inverted vertically)

BYTE *p32 = (BYTE *)bm32.bmBits + (bm32.bmHeight - 1) * bm32.bmWidthBytes;

for (int y = 0; y < bm.bmHeight; y++)

{

// Scan each bitmap pixel from left to right

for (int x = 0; x < bm.bmWidth; x++)

{

// Search for a continuous range of "non transparent pixels"

int x0 = x;

LONG *p = (LONG *)p32 + x;

while (x < bm.bmWidth)

{

BYTE b = GetRValue(*p);

if (b >= lr && b <= hr)

{

b = GetGValue(*p);

if (b >= lg && b <= hg)

{

b = GetBValue(*p);

if (b >= lb && b <= hb)

// This pixel is "transparent"

break;

}

}

p++;

x++;

}

if (x > x0)

{

// Add the pixels (x0, y) to (x, y+1) as a new rectangle in the region

if (pData->rdh.nCount >= maxRects)

{

GlobalUnlock(hData);

maxRects += ALLOC_UNIT;

hData = GlobalReAlloc(hData, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), GMEM_MOVEABLE);

pData = (RGNDATA *)GlobalLock(hData);

}

RECT *pr = (RECT *)&pData->Buffer;

SetRect(&pr[pData->rdh.nCount], x0, y, x, y+1);

if (x0 < pData->rdh.rcBound.left)

pData->rdh.rcBound.left = x0;

if (y < pData->rdh.rcBound.top)

pData->rdh.rcBound.top = y;

if (x > pData->rdh.rcBound.right)

pData->rdh.rcBound.right = x;

if (y+1 > pData->rdh.rcBound.bottom)

pData->rdh.rcBound.bottom = y+1;

pData->rdh.nCount++;

// On Windows98, ExtCreateRegion() may fail if the number of rectangles is too

// large (ie: > 4000). Therefore, we have to create the region by multiple steps.

if (pData->rdh.nCount == 2000)

{

HRGN h = ExtCreateRegion(NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);

if (hRgn)

{

CombineRgn(hRgn, hRgn, h, RGN_OR);

DeleteObject(h);

}

else

hRgn = h;

pData->rdh.nCount = 0;

SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);

}

}

}

// Go to next row (remember, the bitmap is inverted vertically)

p32 -= bm32.bmWidthBytes;

}

// Create or extend the region with the remaining rectangles

HRGN h = ExtCreateRegion(NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);

if (hRgn)

{

CombineRgn(hRgn, hRgn, h, RGN_OR);

DeleteObject(h);

}

else

hRgn = h;

// Clean up

GlobalFree(hData);

SelectObject(hDC, holdBmp);

DeleteDC(hDC);

}

DeleteObject(SelectObject(hMemDC, holdBmp));

}

DeleteDC(hMemDC);

}

}

return hRgn;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有