-3

This is rather complicated, but a solution that uses components that I don't have currently installed would be fine, as long as they work with Delphi 5 (cannot currently upgrade this old project to a new Delphi version).

I've a TMemoryStream. The content of the stream is a png image.

I need this data converted to a bitmap that is stored in a TIcon object.

So the chain should be:

MemoryStream -> a png class that loads the image from the stream -> convertion to a TIcon (not HICON).

4

1 回答 1

3

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

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;
于 2013-02-02T10:47:06.653 回答