我在网站上有一个用户图库,访问者可以上传一些图片.上传后,图像应调整为一些预定义的预设.另外原始图像也应该保存.所有工作正常png
和bmp
图像格式.但是,如果我上传gif
格式或jpeg
以一种颜色占优势上传的原始图像似乎被压缩.
例如:
原版的:
上传时间:
我在谷歌搜索过,并找到了一些如何正确上传图片的例子.最后我写了下一个方法:
void UploadImagePreset(Image image, ImageFormat imgFormat, string imgPath)
{
using (var uploadStream = imageUploader.CreateUploadStream(imagePath))
{
var newWidth = image.Width;
var newHeight = image.Height;
using (var outBitmap = new Bitmap(newWidth, newHeight))
{
using (var outGraph = Graphics.FromImage(outBitmap))
{
outGraph.CompositingQuality = CompositingQuality.HighQuality;
outGraph.SmoothingMode = SmoothingMode.HighQuality;
outGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
outGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRect = new Rectangle(0, 0, newWidth, newHeight);
outGraph.DrawImage(image, imageRect);
outBitmap.Save(uploadStream, imgFormat);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它没有帮助.上载的图像与上述相同.我试图像这样指定结果图像的qaulity:
var encoderParameters …
Run Code Online (Sandbox Code Playgroud)