Convert png / jpg / gif to ico

I have several images, some of them are png, some of them are jpg and gif, and I want to display them as a list in the form of files. TImageList only supports icons, how can I convert them so that they can be inserted into a TImageList.

I am using Delphi XE

+5
source share
1 answer

To specifically answer the question, you also need to consider the simplicity of resizing (for thumbnails), some sample code:

var
  Img: TImage;
  BmImg: TBitmap;
  Bmp: TBitmap;
  BmpMask: TBitmap;
  IconInfo: TIconInfo;
  Ico: TIcon;
begin
  Img := TImage.Create(nil);
  Img.Picture.LoadFromFile(...

  BmImg := TBitmap.Create;
  BmImg.Assign(Img.Picture.Graphic);
  Img.Free;

  Bmp := TBitmap.Create;
  Bmp.SetSize(ImageList1.Width, ImageList1.Height);
  SetStretchBltMode(Bmp.Canvas.Handle, HALFTONE);
  StretchBlt(Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height,
              BmImg.Canvas.Handle, 0, 0, BmImg.Width, BmImg.Height, SRCCOPY);
  BmImg.Free;

  BmpMask := TBitmap.Create;
  BmpMask.Canvas.Brush.Color := clBlack;
  BmpMask.SetSize(Bmp.Width, Bmp.Height);

  FillChar(IconInfo, SizeOf(IconInfo), 0);
  IconInfo.fIcon := True;
  IconInfo.hbmMask := BmpMask.Handle;
  IconInfo.hbmColor := Bmp.Handle;

  Ico := TIcon.Create;
  Ico.Handle := CreateIconIndirect(IconInfo);

  ImageList1.AddIcon(Ico);

  Bmp.Free;
  BmpMask.Free;
  Ico.Free;  // calls DestroyIcon
end;

or without creating an icon:

var
  Img: TImage;
  BmImg: TBitmap;
  Bmp: TBitmap;
begin
  Img := TImage.Create(nil);
  Img.Picture.LoadFromFile(..

  BmImg := TBitmap.Create;
  BmImg.Assign(Img.Picture.Graphic);
  Img.Free;

  Bmp := TBitmap.Create;
  Bmp.SetSize(ImageList1.Width, ImageList1.Height);
  SetStretchBltMode(Bmp.Canvas.Handle, HALFTONE);
  StretchBlt(Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height,
              BmImg.Canvas.Handle, 0, 0, BmImg.Width, BmImg.Height, SRCCOPY);
  BmImg.Free;

  ImageList1.AddMasked(Bmp, clNone);

  Bmp.Free;
end;
+9
source

All Articles