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.
JD Solutions
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