【问题标题】:Reducing image dimension increases the file size C#减小图像尺寸会增加文件大小 C#
【发布时间】: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


【解决方案1】:

我假设您正在使用文件的原始 ImageFormat 写入新图像...如果原始图像是位图,结果格式将是,只需更改扩展名...您需要设置一个编码器。 此外,您可以尝试将 IterpolationMode 更改为中低质量或将文件格式更改为 PNG 格式更适合互联网使用...

【讨论】:

    猜你喜欢
    • 2015-01-27
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    相关资源
    最近更新 更多