patterncsharpMajor
Resizing image but keeping aspect ratio
Viewed 0 times
resizingkeepingimageaspectbutratio
Problem
I wrote some code that resizes an image to fit in a given box. I think that some parts of the code are not needed (for example if it's a box image). Is there a more efficient way to do this? Is there something wrong with my code (from the results it looks ok)? Is there a "shorter" version to do this?
Bitmap original, resizedImage;
try
{
using (FileStream fs = new System.IO.FileStream(imageLabel.Text, System.IO.FileMode.Open))
{
original = new Bitmap(fs);
}
int rectHeight = BOXHEIGHT;
int rectWidth = BOXWIDTH;
//if the image is squared set it's height and width to the smallest of the desired dimensions (our box). In the current example rectHeight rectWidth || newHeight > rectHeight)
{
//depending on which of the two exceeds the box dimensions set it as the box dimension and calculate the other one based on the aspect ratio
if (newWidth > newHeight)
{
newWidth = rectWidth;
newHeight = (int)(newWidth / aspect);
}
else
{
newHeight = rectHeight;
newWidth = (int)(newHeight * aspect);
}
}
resizedImage = new Bitmap(original, newWidth, newHeight);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}Solution
Your algorithm to calculate the final image dimensions could be improved by simplifying the logical process that you use to determine the final size.
Fundamentally, there are two dimensions you are interested in, the height, and width of the final image. There are two possible outcomes for the scaling, one where the scaling produces the target height, and the other where it produces the target width. The scaling that is used is the one which produces the smallest image. The size of the final image is determined by the scaling factor, and there are two factors:
Now, we have two scaling ratios, which one produces the smaller image? The one that has the smallest scaling factor.
Now you can simply do the adjustment:
Fundamentally, there are two dimensions you are interested in, the height, and width of the final image. There are two possible outcomes for the scaling, one where the scaling produces the target height, and the other where it produces the target width. The scaling that is used is the one which produces the smallest image. The size of the final image is determined by the scaling factor, and there are two factors:
float scaleHeight = (float)BOXHEIGHT / (float)original.Height;
float scaleWidth = (float)BOXWIDTH / (float)original.Width;Now, we have two scaling ratios, which one produces the smaller image? The one that has the smallest scaling factor.
float scale = Math.Min(scaleHeight, scaleWidth);Now you can simply do the adjustment:
resizedImage = new Bitmap(original,
(int)(original.Width * scale), (int)(original.Height * scale));Code Snippets
float scaleHeight = (float)BOXHEIGHT / (float)original.Height;
float scaleWidth = (float)BOXWIDTH / (float)original.Width;float scale = Math.Min(scaleHeight, scaleWidth);resizedImage = new Bitmap(original,
(int)(original.Width * scale), (int)(original.Height * scale));Context
StackExchange Code Review Q#70908, answer score: 20
Revisions (0)
No revisions yet.