【问题标题】:TMemoryStream -> PNGImage -> TIcon convertion [closed]TMemoryStream -> PNGImage -> TIcon 转换 [关闭]
【发布时间】:2013-01-28 02:25:25
【问题描述】:

这相当复杂,但是使用我目前没有安装的组件的解决方案会很好,只要它们与 Delphi 5 一起使用(目前无法将此旧项目升级到新的 Delphi 版本)。

我有一个 TMemoryStream。流的内容是 png 图像。

我需要将此数据转换为存储在 TIcon 对象中的位图。

所以链条应该是:

MemoryStream -> 一个从流中加载图像的 png 类 -> 转换为 TIcon(不是 HICON)。

【问题讨论】:

  • 你有没有费心去谷歌“Delphi PNG image”或查看Torry?不应将此网站用作先进行一些基本搜索或自行研究的替代品。
  • 你忘了问问题。请edit 提出您的问题。你已经解释了你想做什么;你的哪一部分任务有问题?
  • 检查 Vampyre Imaging 和 GraphicEx 等库
  • TIcon 只是 HICON 的包装。您只需找到其中一个 PNG 库就可以了。
  • 仍然可以找到旧 Delphi 的 pngimage 副本。只需要四处打猎。

标签: delphi delphi-5


【解决方案1】:

这是我在另一个论坛中得到的解决方案。问题是/根本不是微不足道的!我在谷歌上搜索了很多都没有任何结果。

Procedure LoadPNGAsIcon(const fn: String; var ICO: TIcon);
 var
   Header: PBitmapV5Header;
   hNewBitmap, hMonoBitmap: HBITMAP;
   Bits: Pointer;
   x, y: Integer;
   DC: HDC;
   IconInfo: _ICONINFO;
   Pixel: ^Integer;
   ScanLine: PRGBTriple;
   AlphaScanline: pByteArray;
   PNG: TPngObject;
 begin

   PNG := TPngObject.Create;
   try
     PNG.LoadFromFile(fn);
     if not Assigned(ICO) then
       ICO := TIcon.Create;
     New(Header);
     FillMemory(Header, SizeOf(BitmapV5Header), 1);
     Header.bV5Size := SizeOf(BitmapV5Header);
     Header.bV5Width := PNG.Width;
     Header.bV5Height := -PNG.Height;
     Header.bV5Planes := 1;
     Header.bV5BitCount := 32;
     Header.bV5Compression := BI_BITFIELDS;
     Header.bV5RedMask := $00FF0000;
     Header.bV5GreenMask := $0000FF00;
     Header.bV5BlueMask := $000000FF;
     Header.bV5AlphaMask := $FF000000;
     DC := GetDC(0);
     hNewBitmap := CreateDIBSection(DC, PBitmapInfo(Header)^, DIB_RGB_COLORS,
       Bits, 0, 0);
     Dispose(Header);
     ReleaseDC(0, DC);
     hMonoBitmap := CreateBitmap(PNG.Width, PNG.Height, 1, 1, nil);
     Pixel := Bits;
     for y := 0 to PNG.Height - 1 do
     begin
       ScanLine := PNG.ScanLine[y];
       AlphaScanline := PNG.AlphaScanline[y];
       for x := 0 to PNG.Width - 1 do
       begin
         if Assigned(AlphaScanline) then
           Pixel^ := AlphaScanline[x]
         else
           Pixel^ := 255;
         Pixel^ := Pixel^ shl 8;
         Inc(Pixel^, ScanLine^.rgbtRed);
         Pixel^ := Pixel^ shl 8;
         Inc(Pixel^, ScanLine^.rgbtGreen);
         Pixel^ := Pixel^ shl 8;
         Inc(Pixel^, ScanLine^.rgbtBlue);
         Inc(Pixel);
         Inc(ScanLine);
       end;
     end;
     IconInfo.fIcon := true;
     IconInfo.hbmMask := hMonoBitmap;
     IconInfo.hbmColor := hNewBitmap;
     ICO.Handle := CreateIconIndirect(IconInfo);

     DeleteObject(hNewBitmap);
     DeleteObject(hMonoBitmap);
   finally
     PNG.Free;
   end;
 end;

 procedure TForm1.Button1Click(Sender: TObject);
 var
   ICO:TIcon;
 begin
   ICO := nil;
   LoadPNGAsIcon('C:\Bilder\about.png',ico);
   image1.Picture.Assign(ico);
   ico.Free;
 end;

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-05-05
  • 2012-05-14
  • 1970-01-01
  • 2023-04-06
  • 2015-12-03
  • 2013-10-11
  • 2010-09-30
  • 1970-01-01
相关资源
最近更新 更多