Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Proportionally Resizing an Image 1

Status
Not open for further replies.

djjd47130

Programmer
Nov 1, 2010
480
US
I'm working on this procedure to resize a bitmap proportionally, and centering it. It mostly works, except for the centering part, where I can't get the right formula. The calculation to get S isn't coming out right. S is supposed to be the amount of Space on either side of the image, depending on its proportional difference. S then determines how far from the top/bottom or left/right to be positioned. I can't seem to get S right, as well as using S in R.Bottom or R.Right.

Code:
[navy][i]// for automatic syntax highlighting see faq102-6487 
[/i][/navy]
[b]procedure[/b] ResizeBitmap(Bitmap: TBitmap; Width, Height: Integer);
[b]var[/b]
  R: TRect;
  S: Integer;
  B: TBitmap;
[b]begin[/b]
  [b]if[/b] assigned(Bitmap) [b]then[/b] [b]begin[/b]   
    B:= TBitmap.Create;
    [b]try[/b]
      B.Width:= Width;
      B.Height:= Height;
      B.PixelFormat:= Bitmap.PixelFormat;
      B.Canvas.Brush.Color:= clWhite;
      B.Canvas.FillRect(B.Canvas.ClipRect);
      [b]if[/b] Bitmap.Width > Bitmap.Height [b]then[/b] [b]begin[/b]
        [b]if[/b] Bitmap.Height > Height [b]then[/b]
          S:= (Bitmap.Height - Height) [b]div[/b] [navy]2[/navy]
        [b]else[/b]
          S:= (Height - Bitmap.Height) [b]div[/b] [navy]2[/navy];
        R.Top:= S;
        R.Left:= [navy]0[/navy];
        R.Right:= Width;
        R.Bottom:= ((Width * Bitmap.Height) [b]div[/b] Bitmap.Width) + S;
      [b]end[/b] [b]else[/b] [b]begin[/b]
        [b]if[/b] Bitmap.Width > Width [b]then[/b]
          S:= (Bitmap.Width - Width) [b]div[/b] [navy]2[/navy]
        [b]else[/b]
          S:= (Width - Bitmap.Width) [b]div[/b] [navy]2[/navy];
        R.Top:= [navy]0[/navy];
        R.Left:= S;
        R.Bottom:= Height;
        R.Right:= ((Height * Bitmap.Width) [b]div[/b] Bitmap.Height) + S;
      [b]end[/b];
      B.Canvas.StretchDraw(R, Bitmap);
      Bitmap.Width:= Width;
      Bitmap.Height:= Height;
      Bitmap.Canvas.Draw([navy]0[/navy], [navy]0[/navy], B);
    [b]finally[/b]
      B.Free;
    [b]end[/b];
  [b]end[/b];
[b]end[/b];


JD Solutions
 
This should be very easy, don't know what I'm doing wrong. It should be a margin on either side of the image. It should work like this...

if Height > Width (Right/Left Margins)
S = (AvailWidth - PicWidth) / 2
L = S
T = 0
R = AvailWidth - S (or R = PicWidth + S)
B = AvailHeight (or B = PicHeight)

if Width > Height (Top/Bottom Margins)
S = (AvailHeight - PicHeight) / 2
L = 0;
T = S;
R = AvailWidth (or B = PicWidth)
B = AvailHeight - S (or B = PicHeight + S)

(Where L, T, R, and B are of the TRect)


JD Solutions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top