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;
–
–
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.