Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert png/jpg/gif to ico

I have multiple images some of them are png some of them jpg and gif and i want to display them in a listview as thumbails TImageList supports only icons how can i convert them to be able to insert them in TImageList.

I am using Delphi XE

like image 961
opc0de Avatar asked Dec 21 '11 11:12

opc0de


People also ask

Can you convert GIF to ICO?

CloudConvert converts your image files online. Amongst many others, we support PNG, JPG, GIF, WEBP and HEIC. You can use the options to control image resolution, quality and file size.

Can you convert PNG to ICO?

How to convert a PNG to a ICO file? Choose the PNG file that you want to convert. Select ICO as the the format you want to convert your PNG file to. Click "Convert" to convert your PNG file.

Is PNG and ICO the same?

No. An ICO is actually a specialized file format that contains a collection of images at potentially many different sizes and color depths. A png is a specific image.


1 Answers

To specifically answer the question, also to take simple resizing into account (for thumbnails), some example 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;
like image 188
Sertac Akyuz Avatar answered Sep 20 '22 12:09

Sertac Akyuz