4

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);

alt text
(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?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Roddy
  • 66,617
  • 42
  • 165
  • 277

6 Answers6

4

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.

Tiago Moraes
  • 318
  • 3
  • 7
  • Nope, the alpha information isn't lost. You can use the TImagelist with TButtons and get proper transparent glyphs on the buttons. Besides, bitmaps can be 32 bpp... – Roddy Feb 16 '09 at 20:32
2

Check the Enable Runtime Themes in the tab at
Project -> Options -> Application tab

This solved my problem for me in RAD Studio 2010.

Caryon
  • 21
  • 1
1

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).

TheArtTrooper
  • 1,105
  • 8
  • 19
  • That doesn't work for me - I get the same result as shown above. Are you sure your PNG hasd proper soft-edged transparency? – Roddy Feb 12 '09 at 22:39
  • Alpha transparency becomes simple BMP-color-as-transparent when used inside TIMageList, as per design of the MS Common Controls library. This is not even Delphi code, it's Windows itself doing it. – Warren P Jun 20 '11 at 20:28
1

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...

Vegar
  • 12,828
  • 16
  • 85
  • 151
0

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);
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
  • Thanks, but I'm after proper alpha blending as the left-hand images shows, not single-bit masking, . – Roddy Feb 15 '09 at 19:42
0

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.

Roddy
  • 66,617
  • 42
  • 165
  • 277