1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
HRGN RegionbyBitmap(HBITMAP pBitmap,COLORREF clrTransparent=-1 )
{
BYTE jTranspR = GetRValue(clrTransparent), jTranspG=GetGValue(clrTransparent), jTranspB=GetBValue(clrTransparent);
// bitmap width and height
WORD wBmpWidth,wBmpHeight;
// the final region and a temporary region
HRGN hRgn, hTmpRgn;
// 24bit pixels from the bitmap
BYTE *pPixels = Get24BitPixels(pBitmap, &wBmpWidth, &wBmpHeight);
if (!pPixels) return NULL;
// create our working region
hRgn = CreateRectRgn(0,0,wBmpWidth,wBmpHeight);
if (!hRgn)
{
delete[] pPixels;
return NULL;
}
// ---------------------------------------------------------
// scan the bitmap
// ---------------------------------------------------------
DWORD p=0;
for (WORD y=0; y<wBmpHeight; y++)
{
for (WORD x=0; x<wBmpWidth; x++)
{
BYTE jRed = pPixels[p+2];
BYTE jGreen = pPixels[p+1];
BYTE jBlue = pPixels[p+0];
if ((jRed == jTranspR && jGreen == jTranspG && jBlue == jTranspB))
{
// remove transparent color from region
hTmpRgn = CreateRectRgn(x,y,x+1,y+1);
CombineRgn(hRgn, hRgn, hTmpRgn, RGN_XOR);
DeleteObject(hTmpRgn);
}
// next pixel
p+=3;
}
}
// release pixels
delete[] pPixels;
return hRgn;
}
|