我在 .NET 中遇到图像缩放问题。我使用标准图形类型来调整图像大小,如本例所示:
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;
}
但我对调整大小的图像有一个大问题:它们有灰色和黑色边框,制作没有它们的图像非常重要。
它们为什么会出现,我该怎么做才能让它们消失?
示例输出:
最佳答案
真正的解决方案是使用 DrawImage
的重载,它允许您传递 ImageAttributes
对象。
在 ImageAttributes
实例上,在将其传递给 DrawImage
之前调用以下方法:
using (var ia = new ImageAttributes())
{
ia.SetWrapMode(WrapMode.TileFlipXY);
aGraphic.DrawImage(..., ia);
}
另见 this answer
关于C# 调整大小的图像有黑色边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1861305/