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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I proportionally resize a bitmap?

Delphi Imaging

How do I proportionally resize a bitmap?

by  djjd47130  Posted    (Edited  )
Code:
[b]procedure[/b] ResizeBitmap(Bitmap: TBitmap; Width, Height: Integer; Background: TColor);
[b]var[/b]
  R: TRect;
  B: TBitmap;
  X, Y: Integer;
[b]begin[/b]
  [b]if[/b] assigned(Bitmap) [b]then[/b] [b]begin[/b]   
    B:= TBitmap.Create;
    [b]try[/b]
      [b]if[/b] Bitmap.Width > Bitmap.Height [b]then[/b] [b]begin[/b]
        R.Right:= Width;
        R.Bottom:= ((Width * Bitmap.Height) [b]div[/b] Bitmap.Width);    
        X:= [navy]0[/navy];
        Y:= (Height [b]div[/b] [navy]2[/navy]) - (R.Bottom [b]div[/b] [navy]2[/navy]);
      [b]end[/b] [b]else[/b] [b]begin[/b]
        R.Right:= ((Height * Bitmap.Width) [b]div[/b] Bitmap.Height);
        R.Bottom:= Height;
        X:= (Width [b]div[/b] [navy]2[/navy]) - (R.Right [b]div[/b] [navy]2[/navy]);
        Y:= [navy]0[/navy];
      [b]end[/b];           
      R.Left:= [navy]0[/navy];
      R.Top:= [navy]0[/navy];
      B.PixelFormat:= Bitmap.PixelFormat;
      B.Width:= Width;
      B.Height:= Height;
      B.Canvas.Brush.Color:= Background;
      B.Canvas.FillRect(B.Canvas.ClipRect);
      B.Canvas.StretchDraw(R, Bitmap);
      Bitmap.Width:= Width;
      Bitmap.Height:= Height;
      Bitmap.Canvas.Brush.Color:= Background;
      Bitmap.Canvas.FillRect(Bitmap.Canvas.ClipRect);
      Bitmap.Canvas.Draw(X, Y, B);
    [b]finally[/b]
      B.Free;
    [b]end[/b];
  [b]end[/b];
[b]end[/b];
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top