【问题标题】:Grey and unsharp images when resizing using GDI+使用 GDI+ 调整大小时出现灰色和不清晰的图像
【发布时间】:2013-05-28 09:30:12
【问题描述】:

我正在使用 asp.net mvc 3 流式传输调整大小的图像。但是即使我将平滑模式和插值模式设置为 highbiqubic,图像的输出也是灰色和模糊的。

public ActionResult ImageTEST(int fileID, int width, int height)
{
    var file = _fileRep.GetFile(fileID);
    byte[] newFile;

    float ratioX = (float)width / (float)file.Width;
    float ratioY = (float)height / (float)file.Height;
    float ratio = Math.Min(ratioX, ratioY);

    int newWidth = (int)(file.Width * ratio);
    int newHeight = (int)(file.Height * ratio);

    using (var resizedImage = new Bitmap(newWidth, newHeight))
    {
        using (var source = new Bitmap(new MemoryStream(file.FileContent)))
        {
            using (var g = Graphics.FromImage(resizedImage))
            {
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(source, 0, 0, newWidth, newHeight);
            }
        }

        using (var ms = new MemoryStream())
        {
            resizedImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

            newFile = ms.ToArray();
        }
    }

    return new FileContentResult(newFile, "image/jpeg");
}

结果:

右侧是完全相同的图片,但在 Photoshop 中调整了大小。

如何调整它以提高质量?

【问题讨论】:

    标签: c# image-resizing


    【解决方案1】:

    首先,尝试以更高的质量保存。

    EncoderParameters ep = new EncoderParameters(); 
    ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100); 
    foo.Save(filename, ici, ep);
    

    如果不能满足,您可能需要使用其他库,例如 Emgu cv。

    灰色问题可能是因为原始图像的色彩空间(AdobeRGB 或 sRGB)与您保存的不一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-06
      • 2019-01-29
      • 2012-07-22
      • 1970-01-01
      • 2013-11-28
      • 2016-04-26
      • 1970-01-01
      • 2011-02-21
      相关资源
      最近更新 更多