Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Resampling an image problem

Status
Not open for further replies.

AP81

Programmer
Apr 11, 2003
740
AU
This is a really a logic or maths problem, but I can't get my head around it. Maybe someone can help me.

I have written a small app which resamples an image if the width is greater than 640 pixels or the height is greater than 480 pixels.

My problem is I want to maintain the aspect ratio (the ratio between the width and the height), and find the maximum it can fit within 640 x 480 (for display on TV),

e.g. width = 200, height = 900
-> Given that the max width is 640 and the max height = 480, what is the maximum width/height that can fit in the boundaries of 640x480?

Can you see my problem here? If I get a standard size image (e.g. 800x600) and resize it, it will be fine. If I get an image which is 200 x 900 and resample it to 640x480, the images is stretched.

Any ideas appreciated. This is really simple, I have just hit a mental block!





------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
Here's some code that takes an oldHeight and oldWidth value from TEdits and puts the updated values in newHeight and newWidth TEdits.
Code:
procedure TForm1.Button1Click(Sender: TObject);
const
  MaxHeight = 480;
  MaxWidth = 640;
  Aspect = MaxWidth / MaxHeight;
var
  w: Single;
  h: Single;
  ratio: Single;
begin
  w := StrToIntDef ( oldwidth.text, 0 ) + 0.0;
  h := StrToIntDef ( oldheight.Text, 0 ) + 0.0;
  Assert ( w * h > 0 );
  ratio := w / h;
  if ratio > Aspect then begin
    newWidth.text := IntToStr(MaxWidth);
    newHeight.text := IntToStr ( Round ( h * MaxWidth / w ) ) ;
  end
  else begin
    newHeight.text := IntToStr(MaxHeight);
    newWidth.text := IntToStr ( Round ( w * MaxHeight / h ) );
  end;
Hopefully it's self explanatory and you can adapt it for your requirements.


Andrew
Hampshire, UK
 
Thanks, that works very well.

I worked it out yesterday, but your code is very neat. Thanks again.




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top