1

我需要根据屏幕大小调整(png)图像的大小。目前我使用下面的过程,但在转换过程中失去了透明度。

我确信必须有一种更有效的方法来实现相同的结果,但我对图形并不那么精通。

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;
4

0 回答 0