3

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.

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.

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

Any Help?

user189930
  • 37
  • 1
  • 4

2 Answers2

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;
}
Community
  • 1
  • 1
SimpleRookie
  • 305
  • 1
  • 3
  • 14
-1

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.

Here is an Example : jsFiddle

ygssoni
  • 7,219
  • 2
  • 23
  • 24