gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:32:22 26 4
gpt4 key购买 nike

我在 .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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com