【问题标题】:C#.net image rescaling color issuesC#.net 图像重新缩放颜色问题
【发布时间】:2019-03-18 02:18:47
【问题描述】:

我正在尝试使用 c#.net 即时重新缩放图像。一切似乎都正常工作,但仔细检查后,颜色看起来不正确。

代码看起来很简单,并且重新缩放很好,但为什么原始图像看起来比缩放后的图片更粉红?

using (Bitmap origBitmap = new Bitmap("my_picture.jpg"))
{
    using (Bitmap outputImage = new Bitmap(1024, 768, origBitmap.PixelFormat))
    {
        outputImage.SetResolution(origBitmap.HorizontalResolution, origBitmap.VerticalResolution);
        using (Graphics g = Graphics.FromImage(outputImage))
        {
            g.Clear(Color.Black);
            g.CompositingMode = CompositingMode.SourceCopy;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(
                origBitmap,
                new Rectangle(0, 0, 1024, 768),
                new Rectangle(0, 0, origBitmap.Width, origBitmap.Height),
                GraphicsUnit.Pixel
            );

            context.Response.ContentType = "image/jpeg";
            outputImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
        }
    }
}

附上你可以看到颜色的差异。希望我错过了一些简单的东西?

picture_scaling_issues.jpg

【问题讨论】:

  • 这不是重新缩放图像的好方法。你是放大还是缩小?同时,请参阅InterpolationModeCompositingQuality
  • 示例代码和一些提示here.
  • 出于好奇,为什么这是一种不好的做法?我使用 ImageFormat 的原因是它具有 RotateFlip 功能,我需要能够将图像翻转到不同的方向。
  • 抱歉,我应该先阅读您的链接,然后再询问为什么这是一个不好的方法。 :) 好的,今晚我会看看这些例子,看看它是否有所作为。感谢您的快速回复!
  • ImageFormat可能也有它的作用。 GDI+ 针对 PNG 渲染进行了优化。这是它的默认格式。 .Jpg 格式(缺乏对其有损压缩的有效控制)确实不是。这并不意味着图像颜色被错误处理。但是重新采样时质量可能会令人失望。应用像RotateFlip 这样的矩阵变换不会影响输出质量。

标签: c# bitmap scaling


【解决方案1】:

看起来我回答了我自己的问题!

using (Bitmap origBitmap = (Bitmap) Bitmap.FromFile("my_file.jpg", true))
{ … }

“true”参数用于“useEmbeddedColorManagement”。将其设置为 true 可以解决问题...

【讨论】:

  • 这将在图像实际保存并存储此信息时起作用。无论如何,尝试使用嵌入的信息是一个好习惯。如果您愿意,我可以发布和编码器质量设置,因此您可以控制生成的图像 DPI 和整体图像质量设置(仅供参考,然后您可以勾选您的答案)。
猜你喜欢
  • 2016-05-05
  • 2018-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-14
  • 2013-09-09
相关资源
最近更新 更多