【发布时间】:2014-10-08 22:14:00
【问题描述】:
我继承了一个代码来调整图像的宽度和高度。 我还观察到的是,即使图像高度和宽度增加,它也会减小文件大小。 这是保存图像的调整大小代码和调用函数代码
例如,这可能会更好地解释。我有 709*653 像素的原始图像和 670 kb 的文件大小
调整大小后,它是预期的 1000*921,但它的大小是 176 kb
Image orgImage = Image.FromFile(originalimagepath);
//resizes image using below function
transformedImage = ImageUtils.resizeImage(orgImage, Settings.MaxLargeImage);
memstrImage = new MemoryStream();
//saves image to memorystream which is in turn saved in destination as resized image.
transformedImage.Save(memstrImage,ImageFormat.Jpeg);
public static Image resizeImage(Image imgToResize, int resizeType)
{
//resizetype is maximum resize width I want image to take.
int height = imgToResize.Height;
int width = imgToResize.Width;
float ratio = 0;
Size size = new Size();
if (height >= width)
{
ratio = ((float)resizeType / (float)height);
}
else
{
ratio = ((float)resizeType / (float)width);
}
size.Height = Convert.ToInt32((float)ratio * (float)imgToResize.Height);
size.Width = Convert.ToInt32((float)ratio * (float)imgToResize.Width);
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destHeight = Convert.ToInt32(sourceHeight * nPercent);
int destWidth = Convert.ToInt32(sourceWidth * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (Image)b;
}
我假设,并且可以看到纵横比没有受到影响,但是是否建议保持原样,或者当图像高度和宽度增加时,理想情况下文件大小应该增加是否合乎逻辑
对于这个问题,我想在高度和宽度增加时增加文件大小,但欢迎提出最佳实践建议。
有什么建议吗?
【问题讨论】:
-
你如何测量以前和新的文件大小?
-
您确定正确处理位图吗?可能是图像文件尚未最终确定
-
@Euphoric 下载两者。
-
@Sayse 你能解释更多吗?你发现代码有什么问题吗?
-
您使用的是什么图像格式?如果您使用的是“jpg”,则每次保存时都会重新压缩。这会导致尺寸(好)和质量(坏)的减少。如果只使用
Image.Save(),默认质量相信为75(source)。
标签: c# asp.net image-processing image-resizing