.NET - 调整大小的图像周围的边框
我正在尝试在 .NET 中调整图像大小,但调整大小后的图像周围出现微弱的黑色边框。我找到了一篇文章 - http ://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/cf765094-c8c1-4991-a1f3-cecdbd07ee15/ 某人说使目标矩形大于画布是有效的,但这对我不起作用。它去掉了顶部和左侧边框,但右侧和底部仍然存在,并且是全 1px 厚的黑色。
我错过了什么吗?我的代码如下。
Image image = ... // this is a valid image loaded from the source
Rectangle srcRectangle = new Rectangle(0,0,width, height);
Size croppedFullSize = new Size(width+3,height+3);
Rectangle destRect = new Rectangle(new Point(-1,-1), croppedFullSize);
using(Bitmap newImage = new Bitmap(croppedFullSize.Width, croppedFullSize.Height, format))
using(Graphics Canvas = Graphics.FromImage(newImage)) {
Canvas.SmoothingMode = SmoothingMode.AntiAlias;
Canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
Canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
Canvas.FillRectangle(Brushes.Transparent, destRect);
Canvas.DrawImage(image, destRect, srcRectangle, GraphicsUnit.Pixel);
newImage.Save(filename, image.RawFormat);
}
I'm trying to resize an image in .NET, but get a faint black border around the resized image. I found a post - http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/cf765094-c8c1-4991-a1f3-cecdbd07ee15/ which from someone who said making the destination rectangle larger than the canvas worked, but this doesn't work for me. It gets riid of the top and left borders, but the right and bottom are still there, and are a full 1px thick black.
Am I missing something? My code is below.
Image image = ... // this is a valid image loaded from the source
Rectangle srcRectangle = new Rectangle(0,0,width, height);
Size croppedFullSize = new Size(width+3,height+3);
Rectangle destRect = new Rectangle(new Point(-1,-1), croppedFullSize);
using(Bitmap newImage = new Bitmap(croppedFullSize.Width, croppedFullSize.Height, format))
using(Graphics Canvas = Graphics.FromImage(newImage)) {
Canvas.SmoothingMode = SmoothingMode.AntiAlias;
Canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
Canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
Canvas.FillRectangle(Brushes.Transparent, destRect);
Canvas.DrawImage(image, destRect, srcRectangle, GraphicsUnit.Pixel);
newImage.Save(filename, image.RawFormat);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需向 DrawImage 方法提供一个 ImageAttributes 实例,并将 WrapMode 设置为 TileFlipXY。这将防止边缘与背景颜色混合。
对于不会像此处其他答案那样泄漏内存的示例代码,请参阅此要点
Simply provide the DrawImage method with an ImageAttributes instance which has WrapMode set to TileFlipXY. This will prevent the edge from blending against the background color.
For sample code that doesn't leak memory like the other answers here, see this gist
尝试这样,我想我从来没有黑色边框...
如果你想使用System.Drawing库:
或者你可以用wpf做同样的事情,就像这样:
我推荐WPF方式。压缩和质量好像更好...
Try it like this, i think i've never got a black border...
If you want to use System.Drawing libraries:
or you can do the same with wpf, like this:
I recommend the WPF way. The compression & quality seems better...
对我来说这是一个糟糕的位图参数。而不是这样:
只需将 PixelFormat 删除为:
然后一切都很好。
使用 PixelFormat,我在顶部和左侧边框上有黑色边框。然后我尝试了 g.PixelOffsetMode = PixelOffsetMode.HighQuality;起初看起来不错。但后来我注意到整个图像周围有浅灰色边框。
For me it was a bad Bitmap parameter. Instead of this:
Just remove the PixelFormat to this:
And everything was ok then.
With the PixelFormat I had black border on the top and left border. Then I tried g.PixelOffsetMode = PixelOffsetMode.HighQuality; which seemed fine at first. But then I noticed light gray borders around the whole image.