How to prepare quality thumbnail image in c#
I am developing the asp.net MVC application in c# language ,where I have given the facility to upload the image. I want to provide to crop the image and show its thumbnail. I bit achieve this by using System.Draw开发者_运维问答ing.Bitmap namespace and its classes but it impacts the quality of the thumbnail while compress. What I have to do . I gone through this link
but could not catch up much. please help. Edited: I had tried before to apply above link: and my code was :
public static void ResizeAndSaveHighQualityImage(System.Drawing.Image image, int width, int height, string pathToSave, int quality) {
// the resized result bitmap
using (Bitmap result = new Bitmap(width, height))
{
// get the graphics and draw the passed image to the result bitmap
using (Graphics grphs = Graphics.FromImage(result))
{
grphs.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
grphs.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grphs.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
grphs.DrawImage(image, 0, 0, result.Width, result.Height);
}
// check the quality passed in
if ((quality < 0) || (quality > 100))
{
string error = string.Format("quality must be 0, 100", quality);
throw new ArgumentOutOfRangeException(error);
}
EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
string lookupKey = "image/jpeg";
var jpegCodec = ImageCodecInfo.GetImageEncoders().Where(i => i.MimeType.Equals(lookupKey)).FirstOrDefault();
//create a collection of EncoderParameters and set the quality parameter
var encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
//save the image using the codec and the encoder parameter
result.Save(pathToSave, jpegCodec, encoderParams);
}
}
this is generating low quality image as well. also I could not get much from above given link. why i need to write handler there? is that necessary ?
Maybe the problem is you didn't set which compression format you want.
Have you tried with JPG or PNG?
Check Bitmap.Save overloads and you'll find some needing encoder parameters, which will let you provide a mime type, quality level and so on.
I hope this was useful :)
精彩评论