Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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);
  PngObject: TPngObject;
  TempPngImage: TPngImage;
  Bitmap: TBitmap;
  Ratio: real;
begin
    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;
                @AndreiGalatyn: Simple stretching should be ok. The source image is a high quality png image.
– Pieter van Wyk
                Oct 21, 2013 at 10:40
                Very similar task was already discussed: stackoverflow.com/questions/2437714/resize-png-image?lq=1 and stackoverflow.com/questions/18596172/… There are several solutions (and descriptions of problem with one of those solutions). Is it what you need ?
– Andrei Galatyn
                Oct 21, 2013 at 10:51
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.