【问题标题】:How to reduce the file size of the image but keeping the quality in C# .net? [duplicate]如何减小图像的文件大小但保持 C# .net 中的质量? [复制]
【发布时间】:2018-01-29 01:37:03
【问题描述】:

只是徘徊,如何减少图像的文件大小但保持 C# .net 中的质量?

我有以下代码用于保存从 ipad 拍摄的图像:

Bitmap srcBmp = new Bitmap(file.InputStream);

int w = srcBmp.Width;
int h = srcBmp.Height;
if (srcBmp.Width > 2732 && srcBmp.Height > 2048)
{
    w = 2732;
    h = 2048;
}

SizeF newSize = new SizeF(w, h);
Bitmap target = new Bitmap(w, h);
var destRect = new Rectangle(0, 0, w, h);

HttpContext.Response.Clear();
HttpContext.Response.ContentType = "image/jpeg";
using (var graphics = Graphics.FromImage(target))
{
    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(srcBmp, destRect, 0, 0, w, h, GraphicsUnit.Pixel, wrapMode);
        using (FileStream fileStream = new FileStream(fname, FileMode.Create))
        {
            target.Save(fileStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
    }
}

保存的图像总是超过 14MB。无论如何我可以保持图像质量并将文件大小减小到 2MB 以下?

【问题讨论】:

  • 有很多类似this one的帖子,还有this,所以看看吧。
  • 我认为您的代码是 reduce image size 而不是 reduce file size ;)。

标签: c# .net image


【解决方案1】:

我认为在不降低画质的情况下进行大幅减少是不可能的。但是,您可以使用 Exif library 从图像中删除元数据,而不会降低图片质量

或者如果你擅长降低图片质量,你可以使用GID

【讨论】:

    猜你喜欢
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    • 2018-12-01
    相关资源
    最近更新 更多