【发布时间】:2010-12-24 02:17:43
【问题描述】:
我在 .NET 中遇到图像缩放问题。我使用标准的 Graphics 类型来调整图像的大小,如下例所示:
public static Image Scale(Image sourceImage, int destWidth, int destHeight)
{
Bitmap toReturn = new Bitmap(sourceImage, destWidth, destHeight);
toReturn.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
using (Graphics graphics = Graphics.FromImage(toReturn))
{
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.DrawImage(sourceImage, 0, 0, destWidth, destHeight);
}
return toReturn;
}
但是我对调整大小的图像有一个大问题:它们有灰色和黑色边框,让图像没有它们非常重要。
它们为什么会出现,我能做些什么让它们消失?
样本输出:
【问题讨论】:
-
为这些图像发送到浏览器的 HTML 是什么样的?
-
图片的原始类型是什么?
-
我遇到了同样的问题并发布了我的解决方案。
-
图像不再向上,但如果您在边缘看到 1px 边框,您可以通过设置 TileFlipXY 将 ImageAttributes 实例传递给 DrawImage() 来解决此问题。这使得插值重新使用外部像素边缘,而不是根据背景或透明颜色对其进行平均。来源:imageresizing.net