调整图像的宽度和固定高度。

时间:2022-11-19 13:53:05

I want to resize big images to have a width of 150 pixels and the height will be fixed. and then save the image into a folder in my website.

我想调整大图像的大小,宽度为150像素,高度固定。然后将图像保存到我网站的一个文件夹中。

examples: (width,height) if I resize an image 300px*300px I will get a resized image of 150px*150px. if I resize an image 450px*300px I will get a resized image of 150px*100px.

例如:(宽度,高度)如果我调整一个图片的大小为300px*300px,我将得到一个150px*150px的调整后的图片。如果我调整一个图片的大小为450px*300px,我将得到一个150px*100px的放大图片。

the point is that the ratio between width and height will be saved always keeping a width of 150 pixels.

重点是宽度和高度的比值将始终保持150像素的宽度。

Any Help?

任何帮助吗?

2 个解决方案

#1


2  

I found this (not mine) here

我在这里找到了这个(不是我的)

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;
}

#2


0  

You can accomplish this with a stylesheet. First, define the following style:

您可以使用样式表实现这一点。首先,定义以下样式:

.thumb {
  max-width: 150px;
  max-height: 150px;
  width: expression(this.width > 150 ? "150px" : true);
  height: expression(this.height > 150 ? "150px" : true);
}

Then, add this style to your image tags, like so:

然后,将此样式添加到图像标记中,如下所示:

<img class="thumb" ...>

Do not include the height and width elements this time.

这次不要包含高度和宽度元素。

The first half of that style is for browsers that support the max-width and max-height properties -- Chrome, Firefox, Opera, and Safari. The second half is a hack for IE, which doesn't.

这种风格的前半部分用于支持最大宽度和最大高度属性的浏览器——Chrome、Firefox、Opera和Safari。下半部分是IE的破解版,但实际并非如此。

Here is an Example : jsFiddle

这里有一个例子:jsFiddle

#1


2  

I found this (not mine) here

我在这里找到了这个(不是我的)

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;
}

#2


0  

You can accomplish this with a stylesheet. First, define the following style:

您可以使用样式表实现这一点。首先,定义以下样式:

.thumb {
  max-width: 150px;
  max-height: 150px;
  width: expression(this.width > 150 ? "150px" : true);
  height: expression(this.height > 150 ? "150px" : true);
}

Then, add this style to your image tags, like so:

然后,将此样式添加到图像标记中,如下所示:

<img class="thumb" ...>

Do not include the height and width elements this time.

这次不要包含高度和宽度元素。

The first half of that style is for browsers that support the max-width and max-height properties -- Chrome, Firefox, Opera, and Safari. The second half is a hack for IE, which doesn't.

这种风格的前半部分用于支持最大宽度和最大高度属性的浏览器——Chrome、Firefox、Opera和Safari。下半部分是IE的破解版,但实际并非如此。

Here is an Example : jsFiddle

这里有一个例子:jsFiddle