我想将大图像的大小调整为 150 像素的宽度,并且高度将是固定的。然后将图像保存到我网站的文件夹中。
示例:(宽度,高度)如果我将图像调整为 300 像素 * 300 像素,我将获得 150 像素 * 150 像素的调整后图像。如果我将图像的大小调整为 450px*300px,我将得到一个 150px*100px 的调整大小的图像。
关键是宽度和高度之间的比率将始终保持为 150 像素的宽度。
有什么帮助吗?
我在这里找到了这个(不是我的)
private Bitmap ScaleImage(Image oldImage)
{
double resizeFactor = 1;
if (oldImage.Width > 150 || oldImage.Height > 150)
{
double widthFactor = Convert.ToDouble(oldImage.Width) / 150;
double heightFactor = Convert.ToDouble(oldImage.Height) / 150;
resizeFactor = Math.Max(widthFactor, heightFactor);
}
int width = Convert.ToInt32(oldImage.Width / resizeFactor);
int height = Convert.ToInt32(oldImage.Height / resizeFactor);
Bitmap newImage = new Bitmap(width, height);
Graphics g = Graphics.FromImage(newImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(oldImage, 0, 0, newImage.Width, newImage.Height);
return newImage;
}
您可以使用样式表来完成此操作。首先,定义以下样式:
.thumb {
max-width: 150px;
max-height: 150px;
width: expression(this.width > 150 ? "150px" : true);
height: expression(this.height > 150 ? "150px" : true);
}
然后,将此样式添加到您的图像标签中,如下所示:
<img class="thumb" ...>
这次不要包括高度和宽度元素。
该样式的前半部分适用于支持 max-width 和 max-height 属性的浏览器——Chrome、Firefox、Opera 和 Safari。后半部分是针对 IE 的 hack,但事实并非如此。
这是一个示例:jsFiddle