Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7912923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:43:24+00:00 2026-06-03T13:43:24+00:00

Take the below image I will use for the following examples: The dimensions unchaged

  • 0

Take the below image I will use for the following examples:

The dimensions unchaged are currently 96 x 71

enter image description here

Lets say I wanted to resize the canvas to 115 x 80 – the resulting image should then be:

enter image description here

Finally if I resized it to a smaller size than the original canvas was, eg 45 x 45 the output would appear like so:

enter image description here

This is what I have tried so far:

procedure ResizeBitmapCanvas(Bitmap: TBitmap; H, W: Integer);
var
  Bmp: TBitmap;
  Source, Dest: TRect;
begin
  Bmp := TBitmap.Create;
  try
    Source := Rect(0, 0, Bitmap.Width, Bitmap.Height);
    Dest := Source;
    Dest.Offset(Bitmap.Width div 2, Bitmap.Height div 2);
    Bitmap.SetSize(W, H);
    Bmp.Assign(Bitmap);
    Bmp.Canvas.FillRect(Source);
    Bmp.Canvas.CopyRect(Dest, Bitmap.Canvas, Source);
    Bitmap.Assign(Bmp);
  finally
    Bmp.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ResizeBitmapCanvas(Image1.Picture.Bitmap, 110, 110);
end;

If you try the above on a bitmap loaded into a TImage the actual bitmap does not center, the canvas does change size however.

The properties I have set for the Image are:

Image1.AutoSize := True;
Image1.Center   := True;
Image1.Stretch  := False;

I think it could be the line Dest.Offset(Bitmap.Width div 2, Bitmap.Height div 2); which needs looking at, to calculate the correct center position?

The code has been adapted/modified slightly from a recent question David Heffernan answered.

How do I resize the canvas that surrounds a bitmap, but without stretching the bitmap?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:43:25+00:00Added an answer on June 3, 2026 at 1:43 pm

    I think this is what you are looking for:

    procedure ResizeBitmapCanvas(Bitmap: TBitmap; H, W: Integer; BackColor: TColor);
    var
      Bmp: TBitmap;
      Source, Dest: TRect;
      Xshift, Yshift: Integer;
    begin
      Xshift := (Bitmap.Width-W) div 2;
      Yshift := (Bitmap.Height-H) div 2;
    
      Source.Left := Max(0, Xshift);
      Source.Top := Max(0, Yshift);
      Source.Width := Min(W, Bitmap.Width);
      Source.Height := Min(H, Bitmap.Height);
    
      Dest.Left := Max(0, -Xshift);
      Dest.Top := Max(0, -Yshift);
      Dest.Width := Source.Width;
      Dest.Height := Source.Height;
    
      Bmp := TBitmap.Create;
      try
        Bmp.SetSize(W, H);
        Bmp.Canvas.Brush.Style := bsSolid;
        Bmp.Canvas.Brush.Color := BackColor;
        Bmp.Canvas.FillRect(Rect(0, 0, W, H));
        Bmp.Canvas.CopyRect(Dest, Bitmap.Canvas, Source);
        Bitmap.Assign(Bmp);
      finally
        Bmp.Free;
      end;
    end;
    

    I can’t remember if XE supports setting Width and Height for a TRect. If not then change the code to

    Source.Right := Source.Left + Min(W, Bitmap.Width);
    

    and so on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take a look at the image below. I defined the red boxes inside the
I have a basic PHP question, take the code below for example, let's say
Below is my function that will take a timestamp and tell you the time
My function below, will take the values from my custom meta fields (after a
Please take a look at image below All I want is somehow organize Div
The piece of code given below will read and show image of the file
do take alook at this link below.. http://www.7tech.co.in/php/how-to-generate-a-graph-image-in-php/ I am wondering, what does this
I want to fetch a method's comments,take below method for example: /** * Returns
I'd like to take my form below, and somehow adjust it to submit the
Is there a way to take text like below (if it was already in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.