将 png/jpg/gif 转换为 ico

发布于 2024-12-22 10:19:27 字数 127 浏览 0 评论 0原文

我有多个图像,其中一些是 png,其中一些是 jpg 和 gif,我想将它们显示在列表视图中,因为缩略图 TImageList 仅支持图标,我如何将它们转换为能够将它们插入到 TImageList 中。

我使用的是德尔福XE

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蘸点软妹酱 2024-12-29 10:19:27

为了具体回答这个问题,还要考虑简单的调整大小(对于缩略图),一些示例代码:

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;

或者,不创建图标:

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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文