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

  • SEARCH
  • Home
  • 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 7892035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:46:07+00:00 2026-06-03T06:46:07+00:00

I want to scale a System.Drawing.Bitmap to at least less than some fixed width

  • 0

I want to scale a System.Drawing.Bitmap to at least less than some fixed width and height. This is to generate thumbnails for an image gallery on a website, so I want to keep the aspect ratio the same.

I have some across quite a few solutions but none seem to really do what I need; they revolve around scaling based on keeping the width or the height the same but not changing both.

An example:

If I have a 4272 by 2848 image and I want to scale it to a size of 1024 by 768, then the resulting image should be 1024 by 683 and padded (with a black border) to 1024 by 768.

How can I do this with images larger than the required size and smaller than the require sized and also pad images which don’t come out to the exact size I need once scaled?

  • 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-03T06:46:08+00:00Added an answer on June 3, 2026 at 6:46 am

    Target parameters:

    float width = 1024;
    float height = 768;
    var brush = new SolidBrush(Color.Black);
    

    Your original file:

    var image = new Bitmap(file);
    

    Target sizing (scale factor):

    float scale = Math.Min(width / image.Width, height / image.Height);
    

    The resize including brushing canvas first:

    var bmp = new Bitmap((int)width, (int)height);
    var graph = Graphics.FromImage(bmp);
    
    // uncomment for higher quality output
    //graph.InterpolationMode = InterpolationMode.High;
    //graph.CompositingQuality = CompositingQuality.HighQuality;
    //graph.SmoothingMode = SmoothingMode.AntiAlias;
    
    var scaleWidth = (int)(image.Width * scale);
    var scaleHeight = (int)(image.Height * scale);
    
    graph.FillRectangle(brush, new RectangleF(0, 0, width, height));
    graph.DrawImage(image, ((int)width - scaleWidth)/2, ((int)height - scaleHeight)/2, scaleWidth, scaleHeight);
    

    And don’t forget to do a bmp.Save(filename) to save the resulting file.

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

Sidebar

Related Questions

I want to scale out my EC2 instances on AWS. For this I have
I'm trying to implement a function that takes a System.Drawing.Bitmap object and renders it
I want to scale images to 400x400 (I am creating thumbnails). I am using
I want to scale images these day and find the following example: Example #1
We want to scale, rotate and drag functionality in flash as3. I do have
I want to scale my round rectangle and keep it's radius from been changed.
I want to scale the picture to fit the main div class http://jsfiddle.net/EyW99/ Sorry,
Say I have a BitmapData of 600x600 and I want to scale it down
I'm looking to create a fairly large-scale ASP.NET MVC project and I want to
I'm trying to use CSS3 animations to scale up a div. However, I want

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.