Recent

Author Topic: Painting into images of an imagelist  (Read 3138 times)

wp

  • Hero Member
  • *****
  • Posts: 11916
Painting into images of an imagelist
« on: October 15, 2015, 12:21:27 pm »
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:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.UpdateGlyph(AColor: TColor);
  2. var
  3.   imgList: TImageList;
  4.   bmp: TBitmap;
  5. begin
  6.   imgList := TImageList(Toolbar1.Images);
  7.  
  8.   Toolbar1.BeginUpdate;
  9.   bmp := TBitmap.Create;
  10.   try
  11.     // Extract bitmap from imagelist
  12.     imgList.GetBitmap(0, bmp);
  13.  
  14.     // Paint colored bar at bottom of bitmap
  15.     bmp.Canvas.Brush.Color := AColor;
  16.     bmp.Canvas.Brush.Style := bsSolid;
  17.     bmp.Canvas.Rectangle(0, bmp.Height-6, bmp.Width, bmp.Height);
  18. //    bmp.Canvas.Rectangle(0, 0, bmp.Width, bmp.Height);   --> this works
  19.  
  20.     bmp.TransparentColor := clWhite;
  21.     bmp.TransparentMode := tmFixed;
  22.     bmp.Transparent := true;
  23.  
  24.     // Replace bitmap in imagelist by modified version
  25.     imgList.ReplaceMasked(0, bmp, clWhite);
  26.  
  27.     // Just for debugging: Show new bitmap in separate image component
  28.     Image1.Picture.Assign(bmp);
  29.  
  30.   finally
  31.     bmp.Free;
  32.     Toolbar1.EndUpdate;
  33.   end;
  34. end;
  35.  

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.

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: Painting into images of an imagelist
« Reply #1 on: October 15, 2015, 08:37:05 pm »
Quote
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 think there is nothing wrong with the code in fact the same code runs perfectly in delphi; also if you look at the file saved by
Code: Pascal  [Select][+][-]
  1. Image1.Picture.SaveToFile
you'll see that the image correctly changed color.

so maybe it is bug or something.

 

TinyPortal © 2005-2018