2011-01-11 34 views
0

我正在开发c#语言的asp.net MVC应用程序,在那里我提供了上传图像的工具。我想提供裁剪图像并显示其缩略图。我通过使用System.Drawing.Bitmap命名空间及其类来实现这一点,但它在压缩时会影响缩略图的质量。我必须做的。我经历了这个link如何编写c#中的质量缩略图图像

但不能赶上很多。请帮忙。 编辑: 我试过之前应用上面的链接:和我的代码是:

公共静态无效ResizeAndSaveHighQualityImage(为System.Drawing.Image形象,诠释宽度,高度INT,串pathToSave,INT质量) {

// 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); 

    } 

} 

这也会产生低质量图像。我也无法从给定的链接上获得太多。为什么我需要在那里写处理程序?这是必要的吗?

回答

1

也许问题是你没有设置你想要的压缩格式。

您是否使用过JPG或PNG格式?

检查Bitmap.Save重载,你会发现一些需要的编码器参数,它可以让你提供一个MIME类型,质量等级。

我希望这是有用的:)

+0

感谢您的回复。请检查问题,我已编辑并在那里添加我的代码 – 2011-01-11 10:27:02