Search code examples
delphiiconsfavicondelphi-xeglyph

Loading specific icon size into TIcon from stream


My application downloads and displays favicons for specific websites. I followed Bing's solution for detecting image format from stream, but have hit another snag. Assuming an actual icon image, the code goes like this:

var
  icon : TIcon;
begin      
  icon := TIcon.Create;
  try
    icon.LoadFromStream( faviconStream );
    spFavicon.Glyph.Assign( icon );
  finally
    icon.Free;
  end;
end;

(spFavicon is TRzGlyphStatus from Raize Components. Its Glyph property is a TBitmap)

Now, this works, but sometimes the downloaded icon contains multiple images in different sizes, e.g. 32x32 in addition to the expected 16x16. For some reason the control's Glyph property picks the larger size.

How can I load only the 16x16 size into TIcon, or from TIcon into TBitmap?

Test favicon: http://www.kpfa.org/favicon.ico

On edit: If at all possible, I'd rather avoid saving the icon to a file first.


Solution

  • The primary source for the file format of an .ico file is on MSDN. You should be able to work it out from this.

    The ReadIcon procedure in Graphics.pas may be of use but I imagine that you only need to find 16x16 since you are looking for favicons.

    If you wanted to get really cute, you could download the source to, say, Firefox, and see exactly how they handle favicons.