问题
D2009 introduces PNG support for Images and Imagelists.
However...
I have an imagelist containing png images with alpha. I want to place one of these on a form using a TImage. How do I do this and get the image nicely composited?
As an example of the problem I'm facing the code below fails to work correctly, and produces the effect shown:
ImageList.GetBitmap(index, Image1.Picture.Bitmap);
(source: clip2net.com)
To explain a bit more:
Drop a Timage on a form, and at design time, load a PNG file with alpha using the Picture property. Note how it is correctly composited with full transparency onto the form.
Now, at design time, add a second empty Timage, add a TImagelist, and add the same PNG to the imagelist. How can I assign the PNG in the TImageList to the second TImage, and have it look identical to the first one?
回答1:
From my research I found that TImageList stores the images as TBitmaps, so the alpha information is lost on storage, and you can't achieve what you're looking for with the current implementation of TImageList.
Update:
A little more experiments and with the code below i could make transparency work with the code below.
ImageList1.ColorDepth := cd32Bit;
Image2.Transparent := True;
Image2.Canvas.Pen.Style := psClear;
Image2.Canvas.Rectangle(0, 0, Image2.Width+1, Image2.Height+1);
ImageList1.Draw(Image2.Canvas, 0,0,0);
But it didn't look as pretty as a loaded png.
回答2:
Check the Enable Runtime Themes in the tab at
Project -> Options -> Application tab
This solved my problem for me in RAD Studio 2010.
回答3:
I just tried a simple test. TImageList contains a PNG image with transparency. I render the image on the second TImage using:
imlImageList.Draw(img2.Canvas, 0, 0, 0);
What made the difference for me was setting img2.Transparent := true (I used the designer, not code).
回答4:
I stumbled over this discussion-thread:
Tranparent PNGs in D2009 TImageList
@Pekka Nyyssonen: Setting ColorDepth to cd32Bit and DrawingStyle to dsTransparent worked for me.
I don't have access to delphi 2009 my self so I havn't tried it out, though...
回答5:
There are several ways to add transparent images to an image list.
With AddMasked or InsertMasked, you add an image and tags a color to be the transparent color:
procedure InsertMasked(Index: Integer; Image: TBitmap; MaskColor: TColor);
function AddMasked(Image: TBitmap; MaskColor: TColor): Integer;
With Insert or Add, you add an image and a mask. The mask if a 2 color (black/white) image where only the white pixels from the image are used, the others are transparent.
function Add(Image, Mask: TBitmap): Integer;
procedure Insert(Index: Integer; Image, Mask: TBitmap);
回答6:
To the best of my knowledge, this cannot be acheived. None of the suggestions given result in a properly alpha-blended image, which is the primary requirement.
Maybe by defining a class derived from TImageList, which can then access protected methods, something could be got to work. My solution for now is to use a third-party custom ImageList component specifically for this.
来源:https://stackoverflow.com/questions/532777/pngs-in-delphi-2009-imagelists-and-images