Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

TImage + StretchRatio

Posted on 2000-03-20
5
Medium Priority
?
426 Views
Last Modified: 2010-04-04
How can I add StretchRatio to TImage component?
ie if I reduce size of TImage component the picture is stretched but retains its proportion.

cheers
Chris
0
Comment
Question by:xpher
  • 3
5 Comments
 
LVL 2

Expert Comment

by:alexstewart@beta
ID: 2637604
I have used this sort of thing in the past. The image is contained in a freely resized panel. The image dimensions are kept in 'ratio'. You can use a hidden image or a tbitmap to do a similar thing. Use StretchDraw if what you want is to copy a bitmap into an image with your own 'ratio'.

Alex

....
var
   w,pgw,
   h,pgh : Integer;
begin
w:=Panel2.ClientWidth;
h:=Panel2.ClientHeight;
pgw:=FirstWidth;
pgh:=FirstHeight;
if w*PgH>h*PgW then w:=trunc(h*PgW/PgH);
if w*PgH<h*PgW then h:=trunc(w*PgH/PgW);
Image1.SetBounds(0,0,w,h);
end;

0
 
LVL 13

Accepted Solution

by:
Epsylon earned 300 total points
ID: 2637635
This Component has 1 new property 'KeepRatio' of type boolean. When it is set to true, changing Width or Height will keep the ratio as it was before.



------<ImageEx.pas>------

unit ImageEx;
     
interface

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

type
  TImageEx = class(TImage)
  private              
    { Private declarations }
    FKeepRatio: Boolean;
    procedure SetHeightEx(const Value: Integer);
    procedure SetWidthEx(const Value: Integer);
    function GetHeightEx: Integer;
    function GetWidthEx: Integer;
  protected
    { Protected declarations }
  public
    { Public declarations }
  published
    { Published declarations }
    property KeepRatio: Boolean read FKeepRatio write FKeepRatio;
    property Width: Integer read GetWidthEx write SetWidthEx;
    property Height: Integer read GetHeightEx write SetHeightEx;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Epsylon', [TImageEx]);
end;

{ TImageEx }

function TImageEx.GetHeightEx: Integer;
begin
  Result := inherited Height;
end;

function TImageEx.GetWidthEx: Integer;
begin
  Result := inherited Width;
end;

procedure TImageEx.SetHeightEx(const Value: Integer);
var ratio: Double;
begin
  if KeepRatio then
  begin
    ratio := inherited Width / inherited Height;
    inherited Height := Value;
    inherited Width := Round(inherited Height * ratio);
  end
  else
    inherited Height := Value;
end;

procedure TImageEx.SetWidthEx(const Value: Integer);
var ratio: Double;
begin
  if KeepRatio then
  begin
    ratio := inherited Height / inherited Width;
    inherited Width := Value;
    inherited Height := Round(inherited Width * ratio);
  end
  else
    inherited Width := Value;
end;

end.
0
 
LVL 1

Author Comment

by:xpher
ID: 2641429
Epsylon I can't get this image to do anything but stretch.

Cheers
Chris
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2641625
You are right. The ratio calculations where wrong. Corrected that.


unit ImageEx;
     
interface

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

type
  TImageEx = class(TImage)
  private              
    { Private declarations }
    FKeepRatio: Boolean;
    procedure SetHeightEx(const Value: Integer);
    procedure SetWidthEx(const Value: Integer);
    function GetHeightEx: Integer;
    function GetWidthEx: Integer;
  protected
    { Protected declarations }
  public
    { Public declarations }
  published
    { Published declarations }
    property KeepRatio: Boolean read FKeepRatio write FKeepRatio;
    property Width: Integer read GetWidthEx write SetWidthEx;
    property Height: Integer read GetHeightEx write SetHeightEx;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Epsylon', [TImageEx]);
end;

{ TImageEx }

function TImageEx.GetHeightEx: Integer;
begin
  Result := inherited Height;
end;

function TImageEx.GetWidthEx: Integer;
begin
  Result := inherited Width;
end;

procedure TImageEx.SetHeightEx(const Value: Integer);
var ratio: Double;
begin
  if KeepRatio then
  begin
    ratio := Picture.Width / Picture.Height;
    inherited Height := Value;
    inherited Width := Round(inherited Height * ratio);
  end
  else
    inherited Height := Value;
end;

procedure TImageEx.SetWidthEx(const Value: Integer);
var ratio: Double;
begin
  if KeepRatio then
  begin
    ratio := Picture.Height / Picture.Width;
    inherited Width := Value;
    inherited Height := Round(inherited Width * ratio);
  end
  else
    inherited Width := Value;
end;

end.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2641632
Be sure that you change the width ot height after you have loaded the image!!!

You app from the other question would look like this:

var
  MyImage: TImageEx;

procedure TForm1.Button1Click(Sender: TObject);
var i: Integer;
begin
  for i := 0 to FileListBox1.Items.Count - 1 do
  begin
    MyImage := TImageEx.Create(self);
    try
      with MyImage do
      begin
        Stretch := True;
        KeepRatio := True;
        Picture.LoadFromFile(FileListBox1.Items.Strings[i]);
        Height := 100;
//        Width := 100;
        Left := xdist;
        Parent := ScrollBox1;
        Top := ydist;
      end;
      xdist := MyImage.Left + MyImage.Width + 10;
    except
      MyImage.Free;
    end;
  end;
end;
0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question