Story: The user uploads an image that will be added to a photo gallery. As part of the upload process, we need to A) store the image on the web server's hard drive and B) store a thumbnail of the image on the web server's hard drive.
"Best" here is defined as
Performance and high-quality thumbnails are secondary.
A thumbnail is a small image representation of a larger image, usually intended to make it easier and faster to look at or manage a group of larger images.
GetThumbnailImage would work, but if you want a little better quality you can specify your image options for the BitMap class and save your loaded image into there. Here is some sample code:
Image photo; // your uploaded image Bitmap bmp = new Bitmap(resizeToWidth, resizeToHeight); graphic = Graphics.FromImage(bmp); graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; graphic.SmoothingMode = SmoothingMode.HighQuality; graphic.PixelOffsetMode = PixelOffsetMode.HighQuality; graphic.CompositingQuality = CompositingQuality.HighQuality; graphic.DrawImage(photo, 0, 0, resizeToWidth, resizeToHeight); imageToSave = bmp;
This provides better quality than GetImageThumbnail would out of the box
I suppose your best solution would be using the GetThumbnailImage from the .NET Image class.
// Example in C#, should be quite alike in ASP.NET // Assuming filename as the uploaded file using ( Image bigImage = new Bitmap( filename ) ) { // Algorithm simplified for purpose of example. int height = bigImage.Height / 10; int width = bigImage.Width / 10; // Now create a thumbnail using ( Image smallImage = image.GetThumbnailImage( width, height, new Image.GetThumbnailImageAbort(Abort), IntPtr.Zero) ) { smallImage.Save("thumbnail.jpg", ImageFormat.Jpeg); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With