ASP.NET Tutorial - Making hight quality image thumbnails

Ref: http://asptutorials.net/ASP/making-high-quality-image-thumbnails/

It is very useful on image gallery websites to take uploaded gifs or jpegs and automatically create thumbnails from them. It is not simple to do this in ASP.NET 2, but this tutorial will show you how to create high quality jpg thumbnails that are not at all grainy.

Use the System.Drawing.Imaging library

Before you can handle images in any way in your ASP.NET page, you need to include the following two libraries. Here I am using c# and putting this in my code-behind page:

using System.Drawing;
using System.Drawing.Imaging;

Reading the images in

The first stage is to read the original image file in and convert it into a System.Drawing.Image object:

System.Drawing.Image myimage;
string image_filename=@"c:\location\of\my\image";
try
{
myimage = System.Drawing.Image.FromFile(image_filename);
}
catch
{
Response.Write("Couldn't open file" + image_filename + "<br>");    return;
}

System.Drawing.Image.FromFile method can open .gif, .jpg and .jpeg files. It may even be able to open .png files, but I've never tried it.

Creating the thumbnail bitmap

Before we can make a thumbnail we need to decide on the width and height we want it to be. Normally I decide on the height and then scale the width appropriately to maintain the aspect ratio. I've called this two dimensions thumb_width and thumb_height.

In order to work with the System.Drawing.Image object, we now convert it into a Bitmap. This is because we will eventually draw the image onto a Graphics object that we have created, and the Graphics.DrawImage function requires a Bitmap. So, we make source_bitmap from myimage.

We also make a new Bitmap for the thumbnail, and we just make an empty bitmap in this case, called thumb_bitmap. We next make a Graphics object from this thumb_bitmap and draw the source_bitmap onto this Graphics object. The clever bit is that the Graphics object can define an InterpolationMode that controls the type of re-sampling used when shrinking images, and it automatically resizes the image for you when you call the DrawImage method.

I also fill the Graphics object with white first, in case the image that gets draw onto it has a transparent background, as I want to control the background to be white. You can of course do whatever you want at this stage, or just after the DrawImage call, such as drawing a border onto the thumbnail, or putting a watermark through it.

Bitmap source_bitmap = new Bitmap(myimage);
Bitmap thumb_bitmap = new Bitmap(thumb_width, thumb_height);
Graphics g = Graphics.FromImage(thumb_bitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, thumb_width, thumb_height);
g.DrawImage(source_bitmap, 0, 0, thumb_width, thumb_height);

Saving the thumbnail to disk

Finally, we save the thumb_bitmap to disk, using the best image-encoding quality:

ImageCodecInfo[] Info = ImageCodecInfo.GetImageEncoders();
EncoderParameters Params = new EncoderParameters(1);
Params.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
thumb_bitmap.Save(thumb_filename, Info[1], Params);

thumb_filename is the filename of the target image, including the .jpg extension.

Conclusion

It's quick and easy to convert pretty much any image that someone uploads to your ASP.NET website into a little thumbnail in .jpg format.

posted @ 2008-05-21 10:49  Vincent Yang  阅读(484)  评论(0编辑  收藏  举报