C# 调整大小的图像有黑色边框

标签 c# .net image scaling

我在 .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;
    }

但我对调整大小的图像有一个大问题:它们有灰色和黑色边框,制作没有它们的图像非常重要。

它们为什么会出现,我该怎么做才能让它们消失?

示例输出:

sample output

最佳答案

真正的解决方案是使用 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/

相关文章:

.net - 如果控件设置为 visible = false (.NET),则 Windows 句柄

cocoa - ABPerson 使用旧图片数据

c# - "Pin"控制ToolstripMenu中的项目

c# - HTML 标签如何转换为 HtmlControl?

c# - 使用 SSL 通过 .Net DirectoryServices 连接到 LDAP

android - 像android中的常规一样工作的不可见/透明按钮?

android - 如何让我的 Android 应用程序充当图像选择器

c# - MultiExtended ListBox 选择问题

c# - IValueConverter 接口(interface)中的 ConvertBack 方法有什么用?

c# - 正则表达式在 C# 中不匹配