Link to home
Start Free TrialLog in
Avatar of earlrainer
earlrainer

asked on

resize image


i want to make smaller images from the bigger images i Have.
i have to display them in the program.
i can use Timage and set its stretch property to true.
but i lose the proportion of height:width of the image.
is it possible to make smaller images but still retaining the proportion
Avatar of kretzschmar
kretzschmar
Flag of Germany image

additional to stretch set the proportional-property to true

meikl ;-)
download an example from:   http://www.geocities.com/esoftbg
                                  link:   D7 code Q_20893588.zip

emil
hi meikl,
the code is below:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    Image1: TImage;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    BitBtn05: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure BitBtn05Click(Sender: TObject);
  private  { Private declarations }
  public   { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure GetTrueSize(Image: TImage; var H: Integer; var W: Integer);
begin
  try
    H := Image.Picture.Bitmap.Height;
    W := Image.Picture.Bitmap.Height;
  except
    H := 0;
    W := 0;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Image1.Picture.LoadFromFile('153_139_ATHENA.BMP');
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  H:      Integer;
  W:      Integer;
begin
  GetTrueSize(Image1, H, W);
  Image1.Stretch := False;
  Image1.Height := H;
  Image1.Width := W;
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
var
  H:      Integer;
  W:      Integer;
begin
  GetTrueSize(Image1, H, W);
  Image1.Stretch := True;
  Image1.Height := H*2;
  Image1.Width := W*2;
end;

procedure TForm1.BitBtn3Click(Sender: TObject);
var
  H:      Integer;
  W:      Integer;
begin
  GetTrueSize(Image1, H, W);
  Image1.Stretch := True;
  Image1.Height := H*3;
  Image1.Width := W*3;
end;

procedure TForm1.BitBtn05Click(Sender: TObject);
var
  H:      Integer;
  W:      Integer;
begin
  GetTrueSize(Image1, H, W);
  Image1.Stretch := True;
  Image1.Height := Round(H/2);
  Image1.Width := Round(W/2);
end;

end.
Avatar of earlrainer
earlrainer

ASKER

Hi,

Meikl, i am using delphi5 , i dont find a proportional property.

i think emils solution will work.

my problem is i get images in different sizes.

i have to display then in an area(lengthxwidth) of 120 X 120.

if the image is smaller then 120x120 i dont have to do anything,simply display them.

however if they are bigger then that (eg 200X50 or 300X200) , then i have to proportionately rezise them to fit into the 120x120 area without any distortion

emil can you please modify your code to achive this.
Hi earlrainer, try next:

procedure GetTrueSize(Image: TImage; var H: Integer; var W: Integer);
begin
  try
    H := Image.Picture.Bitmap.Height;
    W := Image.Picture.Bitmap.Width;
  except
    H := 0;
    W := 0;
  end;
end;

procedure TForm1.BitBtn120Click(Sender: TObject);
const
  DSize:  Double = 120;
var
  H:      Integer;
  W:      Integer;
  D:      Double;
begin
  GetTrueSize(Image1, H, W);
  if ((H>DSize) or (W>DSize)) then
  begin
    Image1.Stretch := True;
    if (H=W) then
      D := DSize/H
    else
    if (H>W) then
      D := DSize/(H)
    else
      D := DSize/(W);
    Image1.Height := Round(D*H);
    Image1.Width := Round(D*W);
  end;
end;

emil
ooops:
 
    if (H>=W) then
      D := DSize/H
    else
      D := DSize/W;
procedure ResizeImage(Image: TImage; DSize:  Double);
var
  H:      Integer;
  W:      Integer;
  D:      Double;
begin
  D := 0;
  H := 0;
  W := 0;
  try
    H := Image.Picture.Bitmap.Height;
    W := Image.Picture.Bitmap.Width;
    if ((H>DSize) or (W>DSize)) then
    begin
      Image.Stretch := True;
      if (H>W) then
        D := DSize/H
      else
        D := DSize/W;
    end;
  finally
    Image.Height := Round(D*H);
    Image.Width := Round(D*W);
  end;
end;

procedure TForm1.BitBtn120Click(Sender: TObject);
const
  DSize:  Double = 120;
begin
  ResizeImage(Image1, DSize);
end;
ASKER CERTIFIED SOLUTION
Avatar of esoftbg
esoftbg
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks
Hi Emil,

As an extension to your above code snippet, could you make it so that after your resize, the image is centred?

I tried:

procedure ResizeImage(Image: TImage; DSize:  Double);
var
  H:      Integer;
  W:      Integer;
  D:      Double;
begin
  D := 1;
  H := Image.Picture.Bitmap.Height;
  W := Image.Picture.Bitmap.Width;
  try
    Image.Center := False;
    if ((H>DSize) or (W>DSize)) then
    begin
      Image.Stretch := True;
      if (H>W) then
        D := DSize/H
      else
        D := DSize/W;
    end;
  finally
    Image.Stretch := False;
    Image.Center := True;
    Image.Height := Round(D*H);
    Image.Width := Round(D*W);
  end;
end;

but to no avail... a quick 250 pts for a working answer.



DragonSlayer
DragonSlayer,
the question is about to keep proportion of the image and set its stretch property to true:

>i want to make smaller images from the bigger images i Have.
>i can use Timage and set its stretch property to true.
>but i lose the proportion of height:width of the image.
>is it possible to make smaller images but still retaining the proportion

you have an iteresting solution, but it is out of requirement: Image.Stretch := True;
and using it we see the central zone of the image. The proportion is kept.
After all this question is for 50 points  ;-)

emil
erm... emil... you got me wrong :-)

What i said was, I am offering 250pts for you, to still retain the above code but to make it center as well. Meaning, I still need proportionate resize, but I want the output of the resize to be centred in the TImage.. e.g. if after a resize of a 140x150 JPEG on a 300x300 TImage, the image will appear to the left, with 20 pixels left in the TImage.

Now is there a way for me to center it so that I have 10 pixels on both sides of the TImage as compared to having 20 pixels on the right?

The Image.Center := True code was what I tried, but it didn't work... I still have 20 pixels to the right.


DragonSlayer.
Hi meikl, that's what I'm currently doing too... "cheating" a bit by repositioning the TImage instead. But I was wondering if Emil (or anyone else) can post code to Center it inside the TImage instead of centering the TImage in a Parent Control
>can post code to Center it inside the
>TImage instead of centering the TImage in a Parent Control

in this case you have to create a bitmap with size og the timage-control,
and paint there ceneterd your minimized picture

meikl ;-)
erm... no intermediate TBitmaps... can anything be done on a single TImage? And the sizes of my bitmaps can be of any width x height