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