Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I need add in runtime a png image to a
TImageList
. I've looked at the functions implemented by the
TCustomImageList
but they only allow adding
bitmaps,
icons or
images from another imagelist
E.g.:
function Add(Image, Mask: TBitmap): Integer;
function AddIcon(Image: TIcon): Integer;
function AddImage(Value: TCustomImageList; Index: Integer): Integer;
procedure AddImages(Value: TCustomImageList);
function AddMasked(Image: TBitmap; MaskColor: TColor): Integer;
How I can add a PNG image to a ImageList component without converting this image to BMP?
The IDE already can add a PNG to an ImageList at design time:
Now we need to do it at runtime.
Delphi XE has all the support to handle png images and 32-bit
bitmaps with alpha channel. Here is how to add png to an ImageList:
uses CommCtrl;
var pngbmp: TPngImage;
bmp: TBitmap;
ImageList: TImageList;
begin
ImageList:=TImageList.Create(Self);
ImageList.Masked:=false;
ImageList.ColorDepth:=cd32bit;
pngbmp:=TPNGImage.Create;
pngbmp.LoadFromFile('test.png');
bmp:=TBitmap.Create;
pngbmp.AssignTo(bmp);
// ====================================================
// Important or else it gets alpha blended into the list! After Assign
// AlphaFormat is afDefined which is OK if you want to draw 32 bit bmp
// with alpha blending on a canvas but not OK if you put it into
// ImageList -- it will be way too dark!
// ====================================================
bmp.AlphaFormat:=afIgnored;
ImageList_Add(ImageList.Handle, bmp.Handle, 0);
You must include
ImgList, PngImage
If you now try:
Pngbmp.Draw(Bmp1.Canvas,Rect);
ImageList.Draw(Bmp1.Canvas,0,0,0,true);
you'll see that the images are the same. Actually, there are a few
\pm 1 rgb differences due to rounding errors during alpha blending
but you cannot see them with naked eye. Neglecting to set
bmp.AlphaFormat:=afIgnored; would result in the second image being
much darker!
Best regards,
According to MSDN an imagelist can only contain bitmaps and icons. To add a png image to an imagelist you have to convert it to an icon first. The code to do that can be found in the PngComponents package. If you have only PNG images in your imagelist you can for simplicity just use TPngImageList that comes with that package.
Create an instance of TPngImage, PngImage: PngImage
Load the image into this instance, PngImage.LoadFromFile(..)
Create an instance of TBitmap, Bitmap: TBitmap
Assign the PNG to the bitmap, Bitmap.Assign(PngImage)
Add the bitmap to the image list
Job done!
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.