我已经按照这个post的答案来减小上传的图片大小。图像尺寸减小了,但它也旋转了图像本身(不应旋转)。
我看不到代码的任何部分都在旋转图像,也不知道代码在做什么。
因此,我希望可以进一步解释代码的作用以及仅降低质量时为何旋转图像。
为方便起见,我在此处复制了post答案
// Make sure to include this at the top
using System.Drawing.Imaging;
/// <summary>
/// Saves an image as a jpeg image, with the given quality
/// </summary>
/// <param name="path"> Path to which the image would be saved. </param>
/// <param name="quality"> An integer from 0 to 100, with 100 being the highest quality. </param>
public static void SaveJpeg (string path, Image img, int quality)
{
if (quality<0 || quality>100)
throw new ArgumentOutOfRangeException("quality must be between 0 and 100.");
// Encoder parameter for image quality
EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality);
// JPEG image codec
ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
img.Save(path, jpegCodec, encoderParams);
}
/// <summary>
/// Returns the image codec with the given mime type
/// </summary>
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
// Get image codecs for all image formats
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
// Find the correct image codec
for(int i=0; i<codecs.Length; i++)
if(codecs[i].MimeType == mimeType)
return codecs[i];
return null;
}