Board index » delphi » Resizing a larger rectangle to fit in a smaller one.

Resizing a larger rectangle to fit in a smaller one.

Hi,

Using D4 C/S on NT4SP3.

May be a simple question.  Say we have two rectangles (actually, two
bitmaps; width and height are in pixels): one is a fixed size and never
changes and the second varies in size and shape.  I would like to know
how to resize the second one to fit in the first one if it is bigger
than the first one while keeping the aspect ratio.  Basically, all I
need is a scaling factor which I will use to multiply by the size of the
second one.

Currently I am using a whole bunch of if..then statements to compare the
width and height of the two rectangles and determining the scaling
factor by dividing the original rect size (width or height) by the
second rect size.  If the second is smaller than the first, the scaling
factor is 1.

There MUST be a better way and I'm thinking StretchDIBits() but don't
quite know how to use it.  Remember, I am trying to reduce the rectangle
size while keeping the aspect ratio.

Sample code is helpful!

Thanks in advance,
Jon.

 

Re:Resizing a larger rectangle to fit in a smaller one.


On Wed, 09 Dec 1998 11:12:17 -0500, "Jon E. Scott"

Quote
<jsc...@landstar.com> wrote:
>Currently I am using a whole bunch of if..then statements to compare the
>width and height of the two rectangles and determining the scaling
>factor by dividing the original rect size (width or height) by the
>second rect size.  If the second is smaller than the first, the scaling
>factor is 1.

>There MUST be a better way and I'm thinking StretchDIBits() but don't
>quite know how to use it.  Remember, I am trying to reduce the rectangle
>size while keeping the aspect ratio.

Even using StretchDIBits, you have to calculate the scaling factor.
But it's really not that difficult:

 var
   XScale: Single;
   YScale: Single;
   Scale: Single;
 begin
 XScale := 1.0;
 YScale := 1.0;
 if TargetWidth < SourceWidth then
   XScale := TargetWidth / SourceWidth;
 if TargeeHeight < SourceHeight then
   YScale := TargetHeight / SourceHeight;
 Scale := XScale;
 if YScale < Scale then
   Scale := YScale;
 end;

Now use Scale as your scaling factor.

-Steve

Re:Resizing a larger rectangle to fit in a smaller one.


OK, thanks.  I guess I'll stick with my current algorithm.  I just thought
there would be a more efficient way of shrinking a rectangle to fit in
another rectangle while keeping the aspect ratio than the way I'm doing it,
but it works.

Thanks again,
Jon.

Quote
"Steve Schafer (TeamB)" wrote:
> On Wed, 09 Dec 1998 11:12:17 -0500, "Jon E. Scott"
> <jsc...@landstar.com> wrote:

> >Currently I am using a whole bunch of if..then statements to compare the
> >width and height of the two rectangles and determining the scaling
> >factor by dividing the original rect size (width or height) by the
> >second rect size.  If the second is smaller than the first, the scaling
> >factor is 1.

> >There MUST be a better way and I'm thinking StretchDIBits() but don't
> >quite know how to use it.  Remember, I am trying to reduce the rectangle
> >size while keeping the aspect ratio.

> Even using StretchDIBits, you have to calculate the scaling factor.
> But it's really not that difficult:

>  var
>    XScale: Single;
>    YScale: Single;
>    Scale: Single;
>  begin
>  XScale := 1.0;
>  YScale := 1.0;
>  if TargetWidth < SourceWidth then
>    XScale := TargetWidth / SourceWidth;
>  if TargeeHeight < SourceHeight then
>    YScale := TargetHeight / SourceHeight;
>  Scale := XScale;
>  if YScale < Scale then
>    Scale := YScale;
>  end;

> Now use Scale as your scaling factor.

> -Steve

Other Threads