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