I want to make HeidiSQL high-dpi aware, which includes upscaling my one TImageList with lots of alpha-transparent PNG icons in it.
I have baken a procedure which does it, but it breaks the normal transparency and also the alpha-transparency, so the icons look very broken afterwards, especially at their edges:
Here's the code for that:
procedure ScaleImageList(const ImgList: TImageList; ScaleFactor: Real);
var
i: Integer;
Extracted, Scaled: Graphics.TBitmap;
ImgListCopy: TImageList;
begin
if ScaleFactor = 1 then
Exit;
// Create copy of original image list
ImgListCopy := TImageList.Create(nil);
ImgListCopy.ColorDepth := cd32Bit;
ImgListCopy.DrawingStyle := dsTransparent;
ImgListCopy.Clear;
// Add from source image list
for i := 0 to ImgList.Count-1 do begin
ImgListCopy.AddImage(ImgList, i);
end;
// Set size to match scale factor
ImgList.SetSize(Round(ImgList.Width * ScaleFactor), Round(ImgList.Height * ScaleFactor));
for i:= 0 to ImgListCopy.Count-1 do begin
Extracted := Graphics.TBitmap.Create;
ImgListCopy.GetBitmap(i, Extracted);
Scaled := Graphics.TBitmap.Create;
Scaled.Width := ImgList.Width;
Scaled.Height := ImgList.Height;
Scaled.Canvas.FillRect(Scaled.Canvas.ClipRect);
GraphUtil.ScaleImage(Extracted, Scaled, ScaleFactor);
ImgList.Add(Scaled, Scaled);
end;
ImgListCopy.Free;
end;
I also tried some code from Žarko Gajić but that did just remove transparency from the images, even without actual scaling.
Paint.net does nice scaling on its icons, but it's written in C#, so this is of no help:
Ok, here's how I upscaled images in that list smoothly.
From the main form's
OnCreate
event, I am callingScaleImageList
:ScaleImageList
itself creates a blank TImageList at runtime, loads PNGs from the original list, resizes each of them, and put these into the new image list. In the end the original image list gets overwritten with the new one:Most important are the both helpers
LoadPNGFromImageList
for loading an PNG image from an imagelist into aTPNGImage
, including its alpha channel. AndResizePngImage
, which is basically a code snippet from Gustavo Daud, the author of PNGDelphi:And the second helper: