【发布时间】:2017-02-07 03:07:45
【问题描述】:
在我的项目中,我必须调整图像大小,然后将其保存到文件夹中。我已经发布了很多问题,以找出什么方法可以调整图像大小而不会破坏图像,但我仍然没有找到最好的方法..
为了测试该方法,我不尝试调整图像大小,而是在 resize 方法中输出 100% 大小的图像。
调整大小方法:
public Image reduce(Image sourceImage, string size)
{
//for testing, i want to use the size of the source image
//double percent = Convert.ToDouble(size) / 100;
int width = (int)(sourceImage.Width); //sourceImage.Width * percent
int height = (int)(sourceImage.Height); //sourceImage.Height *percent
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(sourceImage, destRect, 0, 0, sourceImage.Width, sourceImage.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
使用:
//the code to get the image is omitted (in my testing, jpg format is fixed, however, other image formats are required)
//to test the size of original image
oImage.Save(Path.Combine(oImagepath), System.Drawing.Imaging.ImageFormat.Jpeg);
Image nImage = resizeClass.reduce(oImage,"100");
nImage .Save(Path.Combine(nImagepath), System.Drawing.Imaging.ImageFormat.Jpeg);
结果:
第一次保存图片:fileSize:10721KB
第二次保存图片:fileSize: 4033KB
问题是如果通过设置 90% - 100% 传递 10MB 图像,用户如何接受接收 4MB 图像?这太荒谬了,所以我必须重写程序:(
图片:
【问题讨论】:
-
这很可能是 jpeg 压缩的结果。为什么它让你如此困扰?尝试保存文件的setting compression level。