Recent

Author Topic: (Solved) Resizing to a fixed with/height proportionally  (Read 2135 times)

AsleyCruz

  • Jr. Member
  • **
  • Posts: 98
    • Graphic and web designer
(Solved) Resizing to a fixed with/height proportionally
« on: April 02, 2020, 07:56:04 am »
Hi coders,
The below code resize an image to a fixed size.

How can I resize an image/timage to a fixed width or fixed height proportionally?
I don't want to stretch the image :/

Thanks for your time.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   aBitmap: TBGRAbitmap;
  4. begin
  5.   aBitmap := TBGRAbitmap.create(Image1.Picture.Bitmap);
  6.   BGRAReplace(aBitmap, aBitmap.resample(400, 300, rmFineResample));
  7.   Image1.Picture.Bitmap.Assign(aBitmap);
  8. end;
« Last Edit: April 03, 2020, 04:10:37 am by AsleyCruz »
Graphic & web designer

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Resizing to a fixed with/height proportionally
« Reply #1 on: April 02, 2020, 08:10:58 am »
a good explanation of proportion calculations
https://sciencing.com/calculate-ratios-proportions-math-2310756.html

circular

  • Hero Member
  • *****
  • Posts: 4196
    • Personal webpage
Re: Resizing to a fixed with/height proportionally
« Reply #2 on: April 02, 2020, 10:07:23 am »
@Ashley

The simple idea is that you would like to apply the same ratio to both width and height. A ratio is a number by which you multiply.

For example, if you want the image twice as big, you would multiply by 2 both width and height.

Now if you want a certain width for example, you can compute the ratio (new width / current width) and apply it to the height as well.
Conscience is the debugger of the mind

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Resizing to a fixed with/height proportionally
« Reply #3 on: April 02, 2020, 11:24:16 am »
Hi!

Try something like this:

Code: Pascal  [Select][+][-]
  1. uses ......,math;
  2. ....
  3. var ratioX, ratioY, ratio : single;
  4.       NewW, NewH : integer;
  5. ...
  6.  
  7. ratioX := aBitmap.Width/400 ;
  8. ratioY := aBitmap.Height/300;
  9. ratio := math.min (ratioX,ratioY);
  10. NewW := round (aBitmap.width*ratio);
  11. NewH := round(aBitmap.height*ratio);
  12. BGRAReplace (aBitmap,aBitmap.resample(NewW,NewH,rmFineResample) );
  13. ...
  14.  
  15.  

Winni



AsleyCruz

  • Jr. Member
  • **
  • Posts: 98
    • Graphic and web designer
Re: Resizing to a fixed with/height proportionally
« Reply #4 on: April 03, 2020, 04:10:25 am »
Many thanks for your time coders.

I solved it with these codes:

Resize a image to a fixed height proportionally:

Code: Pascal  [Select][+][-]
  1. var
  2.   vNewWidth, vNewHeight:integer;
  3.   vAspectRatio:double;
  4. begin
  5.   vNewHeight := 400;
  6.   vAspectRatio:=Image1.Picture.Width/Image1.Picture.Height;
  7.   vNewWidth:=round(vAspectRatio*vNewHeight);
  8.   Image1.Width:=vNewWidth;
  9.   Image1.Height:=vNewHeight;

Resize a image to a fixed width proportionally:

Code: Pascal  [Select][+][-]
  1. var
  2.   vNewWidth, vNewHeight:integer;
  3.   vAspectRatio:double;
  4. begin
  5.   vNewWidth := 550;
  6.   vAspectRatio:=Image1.Picture.Height/Image1.Picture.Width;
  7.   vNewHeight:=round(vAspectRatio*vNewWidth);
  8.   Image1.Width:=vNewWidth;
  9.   Image1.Height:=vNewHeight;
Graphic & web designer

 

TinyPortal © 2005-2018