Delphi TMemoryStream->;PNGImage->;提康变换

Delphi TMemoryStream->;PNGImage->;提康变换,delphi,delphi-5,Delphi,Delphi 5,这相当复杂,但是使用我目前没有安装的组件的解决方案是可以的,只要它们与Delphi 5一起工作(目前无法将这个旧项目升级到新的Delphi版本) 我有一个TMemoryStream。流的内容是png图像 我需要将这些数据转换为存储在TIcon对象中的位图 因此,链条应该是: MemoryStream->一个png类,将图像从流->转换加载到TIcon(而不是HICON)。这是我在另一个论坛上得到的解决方案。这个问题根本不是小事!我在谷歌上搜索了很多次都没有结果 Procedure LoadPNG

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

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

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

因此,链条应该是:


MemoryStream->一个png类,将图像从流->转换加载到TIcon(而不是HICON)。

这是我在另一个论坛上得到的解决方案。这个问题根本不是小事!我在谷歌上搜索了很多次都没有结果

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;

你有没有用谷歌搜索“Delphi PNG图像”或查看?这个网站不应该代替你自己先做一些基本的搜索或研究。你忘了问一个问题。请陈述你的问题。你已经解释了你想做什么;你的任务的哪一部分遇到了麻烦?检查像Vampyre Imaging和GraphicExaticon这样的库只是HICON的包装。您只需找到一个PNG库,您就很好了。仍然可以为旧的Delphi找到pngimage的副本。只需要四处搜寻一下。参考: