Skip to content

Instantly share code, notes, and snippets.

@DJm00n
Created November 12, 2022 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DJm00n/ff49af518ddd55dd070f1242fec3039a to your computer and use it in GitHub Desktop.
Save DJm00n/ff49af518ddd55dd070f1242fec3039a to your computer and use it in GitHub Desktop.
BitmapColorTranslate(HDC hdcBits, BITMAP* pbm, DWORD rgb)
/*
* using the first pixel as the "transparent" color, make all pixels
* in the hdc that are equal to the "transparent" color the passed
* color.
*/
PRIV VOID BitmapColorTranslate(HDC hdcBits, BITMAP* pbm, DWORD rgb)
{
HDC hdcMask;
HBITMAP hbmMask, hbmT;
HBRUSH hbrT;
BOOL fMono;
/*
* is the bitmap mono, or the first pixel is already equal to the
* passed color? if so we have nothing to do.
*/
fMono = pbm->bmPlanes == 1 && pbm->bmBitsPixel == 1;
if (fMono || GetPixel(hdcBits, 0, 0) == rgb)
return;
// create a mask bitmap and associated DC
if ((hbmMask = CreateBitmap(pbm->bmWidth, pbm->bmHeight, 1, 1, NULL)))
{
hdcMask = CreateCompatibleDC(hdcBits);
// select the mask bitmap into the mono DC
hbmT = SelectObject(hdcMask, hbmMask);
// create the brush and select it into the bits DC
hbrT = SelectObject(hdcBits, CreateSolidBrush(rgb));
// do a color to mono bitblt to build the mask
// generate 1's where the source is equal to the background is
if (hdcMask) {
SetBkColor(hdcBits, GetPixel(hdcBits, 0, 0));
BitBlt(hdcMask, 0, 0, pbm->bmWidth, pbm->bmHeight, hdcBits, 0, 0, SRCCOPY);
// where the mask is 1 lay down the brush, where it is 0 leave the desitnation
SetBkColor(hdcBits, rgbWhite);
SetTextColor(hdcBits, rgbBlack);
BitBlt(hdcBits, 0, 0, pbm->bmWidth, pbm->bmHeight, hdcMask, 0, 0, DSPDxax);
DeleteObject(SelectObject(hdcBits, hbrT));
DeleteObject(SelectObject(hdcMask, hbmT));
DeleteDC(hdcMask);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment