ASP.NET Tutorial – Making hight quality image thumbnails

來源:互聯網
上載者:User
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.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.