If you need to change the size of an Image without changing the ratio of X to Y then this function should do what you want. The input parameters maxWidth and maxHeight define the largest values that you want to get back out of the image after it has been scaled.
|
public static Bitmap ScaleImage(Bitmap bmp, int maxWidth, int maxHeight) { var ratioX = (double)maxWidth / bmp.Width; var ratioY = (double)maxHeight / bmp.Height; var ratio = Math.Min(ratioX, ratioY); var newWidth = (int)(bmp.Width * ratio); var newHeight = (int)(bmp.Height * ratio); var newImage = new Bitmap(newWidth, newHeight); using (var graphics = Graphics.FromImage(newImage)) graphics.DrawImage(bmp, 0, 0, newWidth, newHeight); return newImage; } |
Results of Scaling an Image Here is a […]
Read more