xpher
asked on
TImage + StretchRatio
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
ie if I reduce size of TImage component the picture is stretched but retains its proportion.
cheers
Chris
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Epsylon I can't get this image to do anything but stretch.
Cheers
Chris
Cheers
Chris
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('Epsylo n', [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.
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('Epsylo
end;
{ TImageEx }
function TImageEx.GetHeightEx: Integer;
begin
Result := inherited Height;
end;
function TImageEx.GetWidthEx: Integer;
begin
Result := inherited Width;
end;
procedure TImageEx.SetHeightEx(const
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.
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(FileL istBox1.It ems.String s[i]);
Height := 100;
// Width := 100;
Left := xdist;
Parent := ScrollBox1;
Top := ydist;
end;
xdist := MyImage.Left + MyImage.Width + 10;
except
MyImage.Free;
end;
end;
end;
You app from the other question would look like this:
var
MyImage: TImageEx;
procedure TForm1.Button1Click(Sender
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(FileL
Height := 100;
// Width := 100;
Left := xdist;
Parent := ScrollBox1;
Top := ydist;
end;
xdist := MyImage.Left + MyImage.Width + 10;
except
MyImage.Free;
end;
end;
end;
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;