views:

198

answers:

3

In a rather simple ASP.NET application where the user can upload a image, mostly from his digital camera. I have to resize it to a workable size for the web and a thumbnail. What is here the best practice for? Is there a library that I in a simple way can implement without installing something on the web server.

+1  A: 

Look at the BitMap class- you can do this quite easily by specifying the size in the constructor.

So imagine you wanted to half it:

Bitmap firstBitMap; // created somewhere else

// Create a new bitmap from the first, scaling down as we go
Bitmap halfSize = new Bitmap(firstBitMap, new Size(firstBitMap.Width/2, firstBitMap.Height/2));

If you want a higher quality solution, you need to consider the InterpolationMode enumeration.

For the simple scenario you describe you certainly don't need to bother with 3rd party libraries.

RichardOD
+2  A: 

This thread on SO will probably help you decide:

What is the best image manipulation library

Robert Koritnik
+1  A: 

I am no image expert, but I implemented image resizing on a website and used something like this:

public static void ResizeImageHighQuality(string imgFilename, string imgResizedFilename, int resizeH, int resizeW)
{
    Image img = Image.FromFile(imgFilename);
    int h = 0, w = 0;

    if (img.Width > img.Height)
    {
        w = Convert.ToInt32(resizeW);
        h = Convert.ToInt32(w * Convert.ToDouble(img.Height) / Convert.ToDouble(img.Width));

    }
    else if (img.Height > img.Width)
    {
        h = Convert.ToInt32(resizeH);
        w = Convert.ToInt32(h * Convert.ToDouble(img.Width) / Convert.ToDouble(img.Height));
    }
    else
    {
        h = resizeH;
        w = resizeW;
    }

    Image thumbnail = new Bitmap(w, h);
    Graphics graphic = Graphics.FromImage(thumbnail);

    graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphic.SmoothingMode = SmoothingMode.HighQuality;
    graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
    graphic.CompositingQuality = CompositingQuality.HighQuality;

    graphic.DrawImage(img, 0, 0, w, h);

    ImageCodecInfo[] Info = ImageCodecInfo.GetImageEncoders();
    EncoderParameters Params = new EncoderParameters(1);
    Params.Param[0] = new EncoderParameter(Encoder.Quality, 100L);

    File.Delete(imgResizedFilename);
    FileStream fs = new FileStream(imgResizedFilename, FileMode.CreateNew);
    thumbnail.Save(fs, Info[1], Params);
    fs.Close();
}
Steve