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 6215977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:09:09+00:00 2026-05-24T07:09:09+00:00

This is the code I cam up with to generate square thumbnail in C#

  • 0

This is the code I cam up with to generate square thumbnail in C#

I havent’t code this type of thing before.
Since this code will be used heavily, I am checking with you before I put this in production.

Do you see anything bad or can be improved further?

Basically I have a set of thumbnails sitting in some folder.
Those thumbnails are not square and I need to make all of them square.
This will be called in a loop for each file in the folder.

This is how I call it:

SaveSquareThumb(75, "C:\storage\images\temp\thumbs", "C:\storage\images\thumbs");

public static void SaveSquareThumb(int squareSize, string sourcePath, string savePath)
        {
            using (Bitmap srcImage = new Bitmap(sourcePath))
            {
                int width = srcImage.Width;
                int height = srcImage.Height;

                int x = 0;
                int y = 0;

                //Determine dimensions of resized version of the image 
                if (width > height)
                {
                    width = (int)Decimal.Round(Convert.ToDecimal((decimal)squareSize * ((decimal)width / (decimal)height)), 0);

                    height = squareSize;
                    // moves cursor so that crop is more centered 
                    x = Convert.ToInt32(Math.Ceiling(((decimal)(width - height) / 2M)));
                }
                else if (height > width)
                {
                    height = (int)Decimal.Round(Convert.ToDecimal((decimal)squareSize * ((decimal)height / (decimal)width)), 0);
                    width = squareSize;
                    // moves cursor so that crop is more centered 
                    y = Convert.ToInt32(Math.Ceiling(((decimal)(height - width) / 2M)));
                }
                else
                {
                    width = squareSize;
                    height = squareSize;
                }

                // required in case thumbnail creation fails?
                Image.GetThumbnailImageAbort dummyCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);

                EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
                ImageCodecInfo codecInfo = GetEncoderInfo("image/jpeg");
                EncoderParameters encoderParams = new EncoderParameters(1);
                encoderParams.Param[0] = qualityParam;

                //get thumbnail from source image
                using (Image thumb = srcImage.GetThumbnailImage(width, height, null, System.IntPtr.Zero))
                {
                    //Create a Crop Frame to apply to the Resized Image 
                    using (Bitmap myBitmapCropped = new Bitmap(squareSize, squareSize))
                    {
                        using (Graphics myGraphic = Graphics.FromImage(myBitmapCropped))
                        {
                            //Apply the Crop to the Resized Image 
                            myGraphic.DrawImage(thumb, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);

                            //Save the Croped and Resized image as a new square thumnail 
                            myBitmapCropped.Save(savePath, codecInfo, encoderParams);
                        }
                    }
                }
            }
        }
  • 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-05-24T07:09:09+00:00Added an answer on May 24, 2026 at 7:09 am

    First two items are most important, third and fourth are all about code readability:

    1. Add input parameters validation
    2. Consider how method should behave if any of path parameters is not found
    3. You have multiple casts of ((decimal)squareSize), ((decimal)width) consider single cast and store it in the new method level variables
    4. You canset at the beginning of method default values for width and height to be equals to squareSize and then remove following code block:
       else
       {
          width = squareSize;
          height = squareSize;
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code doesn't work what's wrong? $(.re).focus(function() { if($(this).attr(type) == text) { $(this).attr(type, password);
This code results in an error at second line ($('boxes div.box')) <script type=text/javascript> $(document).ready(function
this is my flash as3 code : var cam:Camera; cam= Camera.getCamera(); var video:Video=new Video(200,200);
Say you have this code pthread_mutex_lock(&cam->video_lock); while(cam->status == WAIT_DISPLAY) // <-- Why is this
This code in JS gives me a popup saying i think null is a
This code is from Prototype.js . I've looked at probably 20 different tutorials, and
this code always returns 0 in PHP 5.2.5 for microseconds: <?php $dt = new
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
This code always works, even in different browsers: function fooCheck() { alert(internalFoo()); // We
This code does not seem to compile, I just need to write something to

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.