I need to resize (png) images depending on the size of the screen. Currently I use the procedure below, but transparency is lost in conversion.
I'm sure there must be a more efficient way to achieve the same result, but I'm not that proficient with graphics.
procedure TFormMain.LoadAndSizeImagesForScreen(ImageFilename: string;
PngImage: TPngImage);
var
PngObject: TPngObject;
TempPngImage: TPngImage;
Bitmap: TBitmap;
Ratio: real;
begin
try
TempPngImage := TPngImage.Create;
TempPngImage.Transparent := True;
TempPngImage.TransparentColor := clWhite;
TempPngImage.LoadFromFile(ImageFilename);
//Determine the ration with which to increase/decrease image size
//BigScreenHeight is the base-line: 1050 currently.
Ratio := Screen.Height / BigScreenHeight;
//Create a bitmap to resize
Bitmap := TBitmap.Create;
//Resize the image to fit the target screen
Bitmap.Width := Round(TempPngImage.Width * Ratio);
Bitmap.Height := Round(TempPngImage.Height * Ratio);
Bitmap.Canvas.StretchDraw(Rect(0, 0, Bitmap.Width, Bitmap.Height), TempPngImage);
//Create a temporary object
PngObject := TPngObject.Create;
PngObject.Assign(Bitmap);
PngImage.Assign(PngObject);
finally
PngObject.Free;
TempPngImage.Free;
Bitmap.Free;
end;
end;