如果宽度固定,高度等比例压缩的话,如何计算高度的问题:
string originalImagePath;//原图的路径
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
1.指定宽,高按比例 缩放
int toheight = originalImage.Height * width / originalImage.Width;
2.指定高,宽按比例
int towidth = originalImage.Width * height / originalImage.Height;
3.如果指定宽、高
private static Size ResizeImage(int width, int height, int maxWidth, int maxHeight)
{
decimal MAX_WIDTH = (decimal)maxWidth;
decimal MAX_HEIGHT = (decimal)maxHeight;
decimal ASPECT_RATIO = MAX_WIDTH / MAX_HEIGHT;
int newWidth, newHeight;
decimal originalWidth = (decimal)width;
decimal originalHeight = (decimal)height;
if (originalWidth > MAX_WIDTH || originalHeight > MAX_HEIGHT)
{
decimal factor;
if (originalWidth / originalHeight > ASPECT_RATIO)
{
factor = originalWidth / MAX_WIDTH;
newWidth = Convert.ToInt32(originalWidth / factor);
newHeight = Convert.ToInt32(originalHeight / factor);
}
else
{
factor = originalHeight / MAX_HEIGHT;
newWidth = Convert.ToInt32(originalWidth / factor);
newHeight = Convert.ToInt32(originalHeight / factor);
}
}
else
{
newWidth = width;
newHeight = height;
}
return new Size(newWidth, newHeight);
}
最后缩略成功