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.

转载于:https://www.cnblogs.com/javafun/archive/2008/05/21/1203837.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值