Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Image resizing to different size while preserving aspect ratio

I'm trying to resize an image while preserving the aspect ratio from the original image so the new image doesn't look squashed.

eg:

Convert a 150*100 image into a 150*150 image.
The extra 50 pixels of the height need to be padded with a white background color.

This is the current code I am using.

It works well for resizing but changing the aspect ratio of the original image squashes the new image.

private void resizeImage(string path, string originalFilename,                           int width, int height)     {         Image image = Image.FromFile(path + originalFilename);          System.Drawing.Image thumbnail = new Bitmap(width, height);         System.Drawing.Graphics graphic =                       System.Drawing.Graphics.FromImage(thumbnail);          graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;         graphic.SmoothingMode = SmoothingMode.HighQuality;         graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;         graphic.CompositingQuality = CompositingQuality.HighQuality;          graphic.DrawImage(image, 0, 0, width, height);          System.Drawing.Imaging.ImageCodecInfo[] info =                          ImageCodecInfo.GetImageEncoders();         EncoderParameters encoderParameters;         encoderParameters = new EncoderParameters(1);         encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality,                          100L);                     thumbnail.Save(path + width + "." + originalFilename, info[1],                           encoderParameters);     } 

EDIT: I'd like to have the image padded instead of cropped

like image 336
sf. Avatar asked Dec 21 '09 14:12

sf.


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

This should do it.

private void resizeImage(string path, string originalFilename,                       /* note changed names */                      int canvasWidth, int canvasHeight,                       /* new */                      int originalWidth, int originalHeight) {     Image image = Image.FromFile(path + originalFilename);      System.Drawing.Image thumbnail =          new Bitmap(canvasWidth, canvasHeight); // changed parm names     System.Drawing.Graphics graphic =                   System.Drawing.Graphics.FromImage(thumbnail);      graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;     graphic.SmoothingMode = SmoothingMode.HighQuality;     graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;     graphic.CompositingQuality = CompositingQuality.HighQuality;      /* ------------------ new code --------------- */      // Figure out the ratio     double ratioX = (double) canvasWidth / (double) originalWidth;     double ratioY = (double) canvasHeight / (double) originalHeight;     // use whichever multiplier is smaller     double ratio = ratioX < ratioY ? ratioX : ratioY;      // now we can get the new height and width     int newHeight = Convert.ToInt32(originalHeight * ratio);     int newWidth = Convert.ToInt32(originalWidth * ratio);      // Now calculate the X,Y position of the upper-left corner      // (one of these will always be zero)     int posX = Convert.ToInt32((canvasWidth - (originalWidth * ratio)) / 2);     int posY = Convert.ToInt32((canvasHeight - (originalHeight * ratio)) / 2);      graphic.Clear(Color.White); // white padding     graphic.DrawImage(image, posX, posY, newWidth, newHeight);      /* ------------- end new code ---------------- */      System.Drawing.Imaging.ImageCodecInfo[] info =                      ImageCodecInfo.GetImageEncoders();     EncoderParameters encoderParameters;     encoderParameters = new EncoderParameters(1);     encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality,                      100L);                 thumbnail.Save(path + newWidth + "." + originalFilename, info[1],                       encoderParameters); } 

Edited to add:

Those who want to improve this code should put it in the comments, or a new answer. Don't edit this code directly.

like image 78
egrunin Avatar answered Sep 20 '22 14:09

egrunin


I found out how to resize AND pad the image by learning from this this CodeProject Article.

static Image FixedSize(Image imgPhoto, int Width, int Height)     {         int sourceWidth = imgPhoto.Width;         int sourceHeight = imgPhoto.Height;         int sourceX = 0;         int sourceY = 0;         int destX = 0;         int destY = 0;          float nPercent = 0;         float nPercentW = 0;         float nPercentH = 0;          nPercentW = ((float)Width / (float)sourceWidth);         nPercentH = ((float)Height / (float)sourceHeight);         if (nPercentH < nPercentW)         {             nPercent = nPercentH;             destX = System.Convert.ToInt16((Width -                           (sourceWidth * nPercent)) / 2);         }         else         {             nPercent = nPercentW;             destY = System.Convert.ToInt16((Height -                           (sourceHeight * nPercent)) / 2);         }          int destWidth = (int)(sourceWidth * nPercent);         int destHeight = (int)(sourceHeight * nPercent);          Bitmap bmPhoto = new Bitmap(Width, Height,                           PixelFormat.Format24bppRgb);         bmPhoto.SetResolution(imgPhoto.HorizontalResolution,                          imgPhoto.VerticalResolution);          Graphics grPhoto = Graphics.FromImage(bmPhoto);         grPhoto.Clear(Color.Red);         grPhoto.InterpolationMode =                 InterpolationMode.HighQualityBicubic;          grPhoto.DrawImage(imgPhoto,             new Rectangle(destX, destY, destWidth, destHeight),             new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),             GraphicsUnit.Pixel);          grPhoto.Dispose();         return bmPhoto;     } 
like image 34
sf. Avatar answered Sep 17 '22 14:09

sf.