【发布时间】:2017-04-07 08:03:34
【问题描述】:
我正在尝试调整图像大小,当我尝试将尺寸减小到 (1200*900) 并将文件保存为 size drastically increases to 1.75mb 时,我当前的图像尺寸为 (1800*1197) 并具有 file size = 305kb。
我已经调用了以下方法 resizeImage :
Image MainImage = resizeImage(BaseImage, 1200, 900, false);
我正在使用以下功能调整图像大小:
public static Bitmap resizeImage(Image imgToResize, int lnWidth, int lnHeight, bool _FixHeightWidth)
{
System.Drawing.Bitmap bmpOut = null;
Graphics g;
try
{
Bitmap loBMP = new Bitmap(imgToResize);
ImageFormat loFormat = loBMP.RawFormat;
decimal lnRatio;
int lnNewWidth = 0;
int lnNewHeight = 0;
//*** If the image is smaller than a thumbnail just return it
if (loBMP.Width < lnWidth && loBMP.Height < lnHeight && _FixHeightWidth == false)
{
bmpOut = new Bitmap(loBMP.Width, loBMP.Height);
g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
g.FillRectangle(Brushes.White, 0, 0, loBMP.Width, loBMP.Height);
g.DrawImage(loBMP, 0, 0, loBMP.Width, loBMP.Height);
loBMP.Dispose();
return bmpOut;
}
if (loBMP.Width > loBMP.Height)
{
lnRatio = (decimal)lnWidth / loBMP.Width;
lnNewWidth = lnWidth;
decimal lnTemp = loBMP.Height * lnRatio;
lnNewHeight = (int)lnTemp;
}
else
{
lnRatio = (decimal)lnHeight / loBMP.Height;
lnNewHeight = lnHeight;
decimal lnTemp = loBMP.Width * lnRatio;
lnNewWidth = (int)lnTemp;
}
if (_FixHeightWidth == true)
{
lnNewHeight = lnHeight;
lnNewWidth = lnWidth;
}
bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
loBMP.Dispose();
}
catch
{
return null;
}
return bmpOut;
}
转换成功后,我只是保存文件,但不是减小,而是增加文件大小。所以我只是希望文件大小保持不变(305kb)或相应减小。
注意:我只是想要关于如何处理这种情况的建议,也可以解释一下是什么导致了这个问题,如果还有其他解决方案的话。 p>
更新:文件格式为'.jpg'
MainImage.Save(Path.Combine(pathForSaving, fileName));
【问题讨论】:
-
输入输出图像files是什么文件格式? JPEG?骨肉瘤? PNG?
-
你以什么格式保存文件?
-
抱歉忘了说 :) '.jpg'
-
bmp != bmp 。查看位图压缩,您会发现有很多方法可以将图像存储为位图(同样适用于 jpg)。为了获得更好的压缩效果,我建议使用 png。(或 jpg 用于照片)。
-
请显示将文件保存为jpeg的代码。
标签: c# asp.net-mvc image-processing image-resizing