减小图像尺寸会增加文件大小 C#
Reducing image dimension increases the file size C#
我正在尝试调整图像大小,我当前的图像尺寸为 (1800*1197)
并且具有 file size = 305kb
,当我尝试将尺寸减小到 (1200*900)
并将文件保存为 [=16] =].
我调用了下面的方法 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)或相应地减少它。
注意:我只想就如何处理这种情况提出建议,如果有其他解决方案,也可以解释一下是什么原因导致了这个问题有吗
示例图片:
更新:文件格式为'.jpg'
MainImage.Save(Path.Combine(pathForSaving, fileName));
我假设你正在使用文件的原始ImageFormat来写入新图像......如果原始图像是位图,结果格式将是,只改变扩展名......你需要设置一个编码器。
另外,您可以尝试将 IterpolationMode 更改为中低质量或文件格式为 PNG 格式更适合互联网使用...
我正在尝试调整图像大小,我当前的图像尺寸为 (1800*1197)
并且具有 file size = 305kb
,当我尝试将尺寸减小到 (1200*900)
并将文件保存为 [=16] =].
我调用了下面的方法 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)或相应地减少它。
注意:我只想就如何处理这种情况提出建议,如果有其他解决方案,也可以解释一下是什么原因导致了这个问题有吗
示例图片:
更新:文件格式为'.jpg'
MainImage.Save(Path.Combine(pathForSaving, fileName));
我假设你正在使用文件的原始ImageFormat来写入新图像......如果原始图像是位图,结果格式将是,只改变扩展名......你需要设置一个编码器。 另外,您可以尝试将 IterpolationMode 更改为中低质量或文件格式为 PNG 格式更适合互联网使用...