I thought I'd know how to paint into a bitmap, but I am experiencing now that I don't...
This is what I want to achieve: Working at some advanced color selection component (
http://forum.lazarus.freepascal.org/index.php/topic,29912.msg190100.html#msg190100) I want to display the selected color in the bitmap of a toolbutton, similar to Office where there is a main icon such as a brush and a color bar at the bottom with the selected color. Therefore, the imagelist contains a "raw" icon for this toolbutton and after color selection a rectangle is painted into this bitmap. Sounds simple. But it does not work, the drawn rectangle does not show up.
Here is the code which is supposed to modify the imagelist image:
procedure TForm1.UpdateGlyph(AColor: TColor);
var
imgList: TImageList;
bmp: TBitmap;
begin
imgList := TImageList(Toolbar1.Images);
Toolbar1.BeginUpdate;
bmp := TBitmap.Create;
try
// Extract bitmap from imagelist
imgList.GetBitmap(0, bmp);
// Paint colored bar at bottom of bitmap
bmp.Canvas.Brush.Color := AColor;
bmp.Canvas.Brush.Style := bsSolid;
bmp.Canvas.Rectangle(0, bmp.Height-6, bmp.Width, bmp.Height);
// bmp.Canvas.Rectangle(0, 0, bmp.Width, bmp.Height); --> this works
bmp.TransparentColor := clWhite;
bmp.TransparentMode := tmFixed;
bmp.Transparent := true;
// Replace bitmap in imagelist by modified version
imgList.ReplaceMasked(0, bmp, clWhite);
// Just for debugging: Show new bitmap in separate image component
Image1.Picture.Assign(bmp);
finally
bmp.Free;
Toolbar1.EndUpdate;
end;
end;
If somebody wants to play with this problem I attached a simple demo project. It contains a toolbar with the tool button displaying an icon from the image list. Clicking on the "Red" button calls above code which should add a 6 pixel-high red stripe to the bottom of the image, clicking on the "Black" button should add a black stripe. There is also a TImage which should display the modified bitmap before insertion back into the image list.
There are two things which are not working:
- The image is a PNG (which is contained in the attachment as well). The image contains a blue bar, "hard-painted" by GIMP. If I load the image into the image editor of the ImageList the blue part does not show up. Why? The blue part however is seen in the non-toolbar image of the demo project when the program starts. Strange...
- After one of the "Red" or "Black" buttons is pressed the new color is not painted into the image. Something has happened, though, because the blue part of the non-imagelist image disappears. What am I doing wrong?
I thought it could have something to do with the 32bpp images of the png format. Therefore the project contains another image list, ImageList_BMP, which contains the same image but now reduced to 24bpp. Just assign this image list to the Images of the Toolbar to activate this test. -- But it does not work either...
I would appreciate any help.