我创建了一个调整图片大小和裁剪图片的函数,然后保存生成的图像文件。在我的开发环境中,它工作得很好,但在生产中,生成的图像是“颗粒状的”。你可以在这里看到不同的质量http://test.powersport.it/canc2.aspx
这是生成调整大小和裁剪图像的代码
// width: width of cropped img - height: height of cropped img
System.Drawing.Bitmap thumbnail = new Bitmap(width, height);
// image: original System.Drawing.Image containing full size img
thumbnail.SetResolution(image.HorizontalResolution, image.VerticalResolution);
// size[0]: width of resized img - size[1]: height of resized image
System.Drawing.Image mini = new Bitmap(image, size[0], size[1]);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(thumbnail);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.DrawImage(mini, ((width - size[0]) / 2), ((height - size[1]) / 2), size[0], size[1]);
EncoderParameters encoderParameters;
encoderParameters = new EncoderParameters(1);
ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
// img: original file name
switch (Path.GetExtension(img).ToLower())
{
case ".png": // info[4]
thumbnail.Save(dest, System.Drawing.Imaging.ImageFormat.Png);
break;
case ".bmp": // info[0]
thumbnail.Save(dest, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case ".tiff": // info[3]
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW);
thumbnail.Save(dest, info[3], encoderParameters);
break;
case ".tif": // info[3]
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW);
thumbnail.Save(dest, info[3], encoderParameters);
break;
default: //jpeg info[1]
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, compression);
thumbnail.Save(dest, info[1], encoderParameters);
break;
}
任何帮助表示赞赏。谢谢。
更新:我尝试过使用 PNG、BMP 和 TIFF,但它们有同样的问题