概述
void SetBackgroundMaskImage(LPCTSTR lpszImageName)
{
COLORREF crAlpha = RGB(255, 0, 0);
CImage img; img.Load(lpszImageName);
if (img.IsNull())
return;
struct PixelData
{
BYTE B;
BYTE G;
BYTE R;
BYTE A;
};
HRGN hRgn = NULL;
#define ALLOC_UNIT 200
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);
for (int y = 0; y < img.GetHeight(); y++)
{
for (int x = 0; x < img.GetWidth(); x++)
{
int x0 = x;
while (x < img.GetWidth())
{
PixelData* pPixel = (PixelData*)img.GetPixelAddress(x, y);
if (pPixel->R != GetRValue(crAlpha) || pPixel->G != GetGValue(crAlpha) || pPixel->B != GetBValue(crAlpha))
//if (pPixel->A == 0)
break;
x++;
}
if (x > x0)
{
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 *pRc = (RECT *)&pData->Buffer;
SetRect(&pRc[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++;
if (pData->rdh.nCount == 500)
{
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);
}
}
}
}
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;
GlobalUnlock(hData);
GlobalFree(hData);
::SetWindowRgn(m_hWnd, hRgn, TRUE);
}
最后
以上就是开朗大雁为你收集整理的VC++ CImage抠图的全部内容,希望文章能够帮你解决VC++ CImage抠图所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复