Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Scaling rectangle

Posted on 2003-12-08
10
Medium Priority
?
707 Views
Last Modified: 2013-11-22
Hi,

I drew a rectangle in a TImage object.

The size of TImage is for example width = W and Height = H.

The rectangle has 4 coordinate positions, X1, Y1, X2, Y2.

Then, I proportionally shrink the TImage to be this size: width = W1 and Height = H1.

So, anyone know a formula to calculate the new X1, Y1, X2, and Y2, so the rectangle will still be proportional ?

Thanks
0
Comment
Question by:muis2002
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9896695
just from head, not tested, and maybe possible wrong

scalingfactor := NewHeight/OldHeight;

newX1 := x1*scalingfactor;
newY1 := y1*scalingfactor;
newX2 := y2*scalingfactor;
newY2 := y2*scalingfactor;

meikl ;-)
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 9896717
x := X*W1 div W;
x1 := X1*W1 div W;
y := y*H1 div H;
y1 := y1*H1 div H;

F68 ;-)
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 9896723
this time you're the faster meikl :))
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 9896769
i'm not sure, if my calculation is correct, f68 :-)
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 9896817
infact looking better in your calculation y must depend on Width scaling factor, that could be different from the height one...
SO

WSCalingFactor := NewWIdth/OldWIdth;
HSCalingFactor := NewHeight/OldHeight;

x1 := x1*WSCalingFactor;
x2 := x2*WScalingFactor;
y1 := y1*HSCalingFactor;
y2 := y2*HScalingFactor;

F68 ;-)
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 9896828
ops...NewWIdth div OldWIdth...etc. etc. (result must be integer, not real)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9896862
>ops...NewWIdth div OldWIdth...etc. etc. (result must be integer, not real)

just use round or trunc

>WSCalingFactor := NewWIdth/OldWIdth;
>HSCalingFactor := NewHeight/OldHeight;

i decided to use one scalingfactor, because
if the image is scaled proportional, then
WSCalingFactor and HSCalingFactor
should have the same value

meikl ;-)
0
 

Author Comment

by:muis2002
ID: 9902245
Well, after I tried, it does not works :( not because the calculation, but because of the way TImage stretching :(

I cant get the W1 and H1 (width and height after stretched proportionally).

Original TImage size W * H

then I have newTImage with "smaller" size (let say Wx and Hx).
I assigned TImage to the newTImage, so that newTImage contains TImage bitmap.

But then, I cant get the stretched image size in newTImage.

I checked newTImage.Width , it will always Wx
I checked newTImage.Picture.Width, it will always W (the same with TImage.Width).
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9902338
the picture width never changes,
only the visualizing of the TImage
is adjusted (autostretch-property is set to true)

if you want the picture scaled physically,
then use canvas-methods like

- Stretchdraw
- copyrect

or api like
- BitBlt

to create a new scaled picture

meikl ;-)
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 200 total points
ID: 9902389
instead of autostretch i meant autosize

a sample

unit image_scale_u;

interface

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

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Splitter1: TSplitter;
    Panel3: TPanel;
    Image1: TImage;
    Image2: TImage;
    Button1: TButton;
    TrackBar1: TTrackBar;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
  private
    procedure scale;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


//scaling routine, craeting a scaled image
procedure TForm1.scale;
var bmp : TBitmap;
begin
  image2.Picture := nil; //clear content;
  bmp := TBitmap.Create;
  try
    bmp.Width := round(image1.Picture.Bitmap.Width*(trackbar1.Position/100));
    bmp.Height := round(image1.Picture.Bitmap.Height*(trackbar1.Position/100));
    //draw, not the fastest routine
    bmp.Canvas.CopyRect(Rect(0,0,bmp.Width, bmp.height),
                        image1.Picture.Bitmap.Canvas,
                        Rect(0,0,image1.Picture.Bitmap.width,image1.Picture.Bitmap.height));
    image2.Picture.Assign(bmp);  //show
  finally
    bmp.Free;
  end;
end;


//load a file
procedure TForm1.Button1Click(Sender: TObject);
begin
  if opendialog1.Execute then
  begin
    image1.Picture.Bitmap.LoadFromFile(opendialog1.filename);
    scale;
  end;
end;

//scale on trackbarchange (range: 0%-500%)
procedure TForm1.TrackBar1Change(Sender: TObject);
begin
  scale;
end;

end.

meikl ;-)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

609 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