2

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;
Pieter van Wyk
  • 2,316
  • 9
  • 48
  • 65
  • Do you need kind of smooth/quality resampling or simple stretching is enough ? – Andrei Galatyn Oct 18 '13 at 17:40
  • @AndreiGalatyn: Simple stretching should be ok. The source image is a high quality png image. – Pieter van Wyk Oct 21 '13 at 10:40
  • 2
    Very similar task was already discussed: http://stackoverflow.com/questions/2437714/resize-png-image?lq=1 and http://stackoverflow.com/questions/18596172/resize-png-image-in-delphi-incorrect-alpha-channel There are several solutions (and descriptions of problem with one of those solutions). Is it what you need ? – Andrei Galatyn Oct 21 '13 at 10:51

0 Answers0