Recent

Author Topic: [SOLVED] Image resize  (Read 169 times)

DanishMale

  • Jr. Member
  • **
  • Posts: 83
[SOLVED] Image resize
« on: February 15, 2025, 09:10:19 pm »
Hi all,

I am trying to load an image and resize the image to save on another location in the target size.

Target info:
Height: 132
Width: 220
Aspect ratio: 1.67
Transparent





after resize

Keep aspect ratio from original image

if width is less than target, but height is equal to target merge with empty image and center image

if height is less than target, but width is equal to target merge with empty image and center image


Code: Pascal  [Select][+][-]
  1.       OrgImage.Picture.LoadFromFile(OpenDialog1.FileName);
  2.       OrgHeight := OrgImage.Picture.Height;
  3.       OrgWidth := OrgImage.Picture.Width;
  4.       AspectRatio := OrgWidth / OrgHeight;
  5.       AspectRatio := StrToFloat(Format('%.2f', [AspectRatio]));
  6.       if AspectRatio = 1.67 then
  7.       begin   // Equals Result dimensions
  8.         NewHeight := 132;
  9.         NewWidth := 220;
  10.       end
  11.       else if AspectRatio > 1 then
  12.       begin   // Width greater than Height
  13.         NewWidth := 220;
  14.         NewHeight := Trunc(220 / AspectRatio);
  15.                 if NewHeight > 132 then
  16.                 begin
  17.                   NewHeight := 132;
  18.                   NewWidth := Trunc(132 * AspectRatio);
  19.                 end;
  20.       end
  21.       else if AspectRatio < 1 then
  22.       begin   // Height greater than Width
  23.         NewHeight := 132;
  24.         NewWidth := Trunc(132 * AspectRatio);
  25.                 if NewWidth > 220 then
  26.                 begin
  27.                   NewHeight := 220;
  28.                   NewWidth := Trunc(220 / AspectRatio);
  29.                 end;
  30.       end;
  31.          
  32.           // GotIT working with this code  !!!
  33.       NewImage.Height := 132;
  34.       NewImage.Width := 220;
  35.       NewTop := Trunc((132 - NewHeight) / 2);
  36.       NewLeft := Trunc((220 - NewWidth) / 2);
  37.       NewImage.Picture.Bitmap.SetSize(220,132);
  38.       NewImage.Picture.Bitmap.PixelFormat := pf32bit;
  39.       NewImage.Picture.Bitmap.TransparentColor := TColor($D30094);
  40.       NewImage.Picture.Bitmap.Canvas.Clear;
  41.       NewImage.Picture.Bitmap.Canvas.StretchDraw(Rect(NewLeft,NewTop,
  42.                                                                                  NewLeft+NewWidth,NewTop+NewHeight),
  43.                                                                                  OrgImage.Picture.Graphic);
  44.       NewImage.Picture.SaveToFile(LogoPath+'\Manufacturer\'+Edit5.Text+'.png');
  45.  

I have tried many other ways, however, it seems I am unable to get it working
« Last Edit: February 16, 2025, 03:56:31 am by DanishMale »
Lazarus 3.8 x64 | Windows 10 x64 | Windows Server 2019 x64 | OpenViX 6.4.011 (Linux) | MySQL Community Server 8.0 x64 | MariaDB 11.2 x64 | SQLite 3.40.0 x64 | PostgresSQL 16.3 x64

 

TinyPortal © 2005-2018