Click here to Skip to main content
15,891,769 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have image with black dots in a white background, which i use to create a patten brush and change the color of the black dots using SetTextColor. I created a CBitmap using PNG image(i used CPngImage to get HBITMAP and attached it to CBitmap ). Now i created PatternBrush using(CreatePatternBrush) with that bitmap, the problem is that the color of the brush doesn't change if i use PNG image. But it changes when i use image in bmp format.

What I have tried:

here is some code
CBitmap bmp;
if (bmp.LoadBitmap(IDB_BITMAP1))
{
if (brush.CreatePatternBrush(&bmp))
pOldBrush = pDC->SelectObject(&brush);
}

pDC->SetTextColor(color); This Works

CBitmap bmp;
CPngImage pngImage;
HBITMAP hBitmap = NULL;

if ( pngImage.Load(MAKEINTRESOURCE(nIDResource)) )
{
hBitmap = (HBITMAP)pngImage.Detach();
}


if ( hBitmap )
bmp.Attach(hBitmap);

if (brush.CreatePatternBrush(&bmp))
pOldBrush = pDC->SelectObject(&brush);

pDC->SetTextColor(color); This Doesn't work
Posted
Updated 5-Nov-17 22:55pm
v4
Comments
Richard MacCutchan 6-Nov-17 4:38am    
I cannot find the Detach method in the documentation: CPngImage Class[^]. And I am not sure that you can just cast a PNG to a HBITMAP in that way.
Ravi Teja 6-Nov-17 5:46am    
CPngImage is derived from CBitmap Which in turn derived from CGDIObject class which is where the Detach method comes from. if you can check CMFCToolBarImages::LoadStr method in VC\atlmfc\src\mfc folder this how they get the HBITMAP from PNG file

1 solution

It looks like your bitmap IDB_BITMAP1 is monochrome while your PNG image is not.
A brush created using a monochrome bitmap (1 color plane, 1 bit per pixel) is drawn using the current text and background colors. Pixels represented by a bit set to 0 are drawn with the current text color. Pixels represented by a bit set to 1 are drawn with the current background color.
So the colours set by SetTextColor and SetBkColor are used with brushes created from monochrome bitmaps. But with brushes created from non-monochrome bitmaps, the colours from the bitmap are used. If you don't need brushes with multiple colours and want to use a PNG image, convert that to monochrome.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900