Border size around proportional images

Hi there!

I'd be really grateful if someone could help me out with this problem I have.

I'm using the TImage component within Delphi to display images, and have set it to display images proportionally. How is it possible to calculate or get the image compoent to return the size of the left/right/top/bottom borders around the picture?

Many thanks in advance
Matt Briggs

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russell LibbySoftware Engineer, Advisory Commented:
The following is from the calculations used by the Paint procedure in TImage. This will let you get both the Rect for the control as well as the image.

Hope this helps,

// Assuming a control called Image1
var  rcImage:    TRect;
     rcCtl:      TRect;

  // Get the control rectangle
  rcCtl:=Rect(0, 0, Image1.Width, Image1.Height);

  // Get the actual image rect
  with Image1 do
     if Stretch then
     else if Center then
        rcImage:=Bounds((Width - Picture.Width) div 2, (Height - Picture.Height) div 2, Picture.Width, Picture.Height)
        rcImage:=Rect(0, 0, Picture.Width, Picture.Height);

 // Do whatever with the rects


hyperrealAuthor Commented:

The picture displayed within Image1 is Streched, Centred and Proportional. As a result the above code does not seem to give me the left, right and top,bottom border size around the image.

        leftBorder: integer;
        rightBorder: integer;
        topBorder: integer;
        bottomBorder: integer;

       Image1.AutoSize := True;
       Image1.Centre := True;
       Image1.Proportional := True;
       Image1.Picture := 'c:\test.bmp';

       . . .
       leftBorder := rcImage.Left;
       bottomBorder := rcImage.Bottom;
       . . .

... Any thoughts?

Many thanks in advance.
Russell LibbySoftware Engineer, Advisory Commented:

What version of delphi are you working with? Looking at D5, there is no "Proportional" setting, just the strecth and center; in which case the stretch overrides the center value (and the image is stretched to the dimensions of the control, leaving no border).

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

hyperrealAuthor Commented:

I'm using Delphi 6, and need to know the border sizes around the image when the image is proportional.

i think you want to have the information what the stretch property of the timage has done.

for this you should load the image with autosize := true and  proportional and stretch  := false;
now you know how big the original is.
if you save these values and compare them to the proportional := true image you should
know how big your border is.

Apologies, DanRollins.

Unfortunetly, no one managed to supply the answer I was looking for, and I managed to figure it out by calculating the image scaling myself. I'll post the code I used on here shortly.

Many thanks to all that attempted to help.

View the image scaling in action:
hyperrealAuthor Commented:

As promised, here is the code I used:


Ratio := imgsolution.picture.Width / imgsolution.picture.Height;
AWidth := Round(image1.Height * Ratio);
AHeight := Round(image1.Width / Ratio);
lborder := (round((image1.Width - AWidth) / 2));
tborder := (round((image1.Height - AHeight) / 2));
if (lborder < 0) then
if (tborder < 0) then


Play Matt's 15-1 Puzzle Game:
PAQed with points refunded (500)

Community Support Moderator

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.