我编写了以下代码来上传不同类型的图片。
public static void CompressImageWithNewDimensions(Stream stream, string targetPath, double fileSizeKB)
{
try
{
using (var image = Image.FromStream(stream))
{
double scaleFactor;
if (fileSizeKB <= 900)
{
scaleFactor = 0.9;
}
else if (fileSizeKB <= 1500)
{
scaleFactor = 0.8;
}
else if (fileSizeKB <= 2000)
{
scaleFactor = 0.7;
}
else
{
scaleFactor = 0.3;
}
var newWidth = (int)(image.Width * scaleFactor);
var newHeight = (int)(image.Height * scaleFactor);
var CompressImage = new Bitmap(newWidth, newHeight);
var CompressImageGraph = Graphics.FromImage(CompressImage);
CompressImageGraph.CompositingQuality = CompositingQuality.HighQuality;
CompressImageGraph.SmoothingMode = SmoothingMode.HighQuality;
CompressImageGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
CompressImageGraph.DrawImage(image, imageRectangle);
//CompressImage.Save(targetPath, image.RawFormat);
CompressImage.Save(targetPath, System.Drawing.Imaging.ImageFormat.Gif);
}
}
catch (Exception)
{
}
}
现在我已经在我的文件夹中成功上传了GIF文件,但在我打开图片时的文件夹中,它不是动画。