.NET - 调整大小的图像周围的边框

发布于 2024-10-27 06:03:03 字数 1253 浏览 0 评论 0原文

我正在尝试在 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最终幸福 2024-11-03 06:03:03

只需向 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

夏至、离别 2024-11-03 06:03:03

尝试这样,我想我从来没有黑色边框...

如果你想使用System.Drawing库:

using (var sourceBmp = new Bitmap(sourcePath))
{
  decimal aspect = (decimal)sourceBmp.Width / (decimal)sourceBmp.Height;
  int newHeight = (int)(newWidth / aspect);

   using (var destinationBmp = new Bitmap(newWidth, newHeight))
   {
     using (var destinationGfx = Graphics.FromImage(destinationBmp))
     {
       destinationGfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
       destinationGfx.DrawImage(sourceBmp, new Rectangle(0, 0, destinationBmp.Width, destinationBmp.Height));
       destinationBmp.Save(destinationPath, ImageFormat.Jpeg);
      }
    }
}

或者你可以用wpf做同样的事情,就像这样:

using (var output = new FileStream(outputPath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None))
{
   var imageDecoder = BitmapDecoder.Create(inputStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
   var imageFrame = imageDecoder.Frames[0];

   decimal aspect = (decimal)imageFrame.Width / (decimal)imageFrame.Height;
   var height = (int)(newWidth / aspect);

   var imageResized = new TransformedBitmap(imageFrame,new ScaleTransform(
                                                                 newWidth / imageFrame.Width * Dpi / imageFrame.DpiX,
                                                                 height / imageFrame.Height * Dpi / imageFrame.DpiY, 0, 0));

   var targetFrame = BitmapFrame.Create(imageResized);

   var targetEncoder = new JpegBitmapEncoder();
   targetEncoder.Frames.Add(targetFrame);
   targetEncoder.QualityLevel = 80;
   targetEncoder.Save(output);
}

我推荐WPF方式。压缩和质量好像更好...

Try it like this, i think i've never got a black border...

If you want to use System.Drawing libraries:

using (var sourceBmp = new Bitmap(sourcePath))
{
  decimal aspect = (decimal)sourceBmp.Width / (decimal)sourceBmp.Height;
  int newHeight = (int)(newWidth / aspect);

   using (var destinationBmp = new Bitmap(newWidth, newHeight))
   {
     using (var destinationGfx = Graphics.FromImage(destinationBmp))
     {
       destinationGfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
       destinationGfx.DrawImage(sourceBmp, new Rectangle(0, 0, destinationBmp.Width, destinationBmp.Height));
       destinationBmp.Save(destinationPath, ImageFormat.Jpeg);
      }
    }
}

or you can do the same with wpf, like this:

using (var output = new FileStream(outputPath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None))
{
   var imageDecoder = BitmapDecoder.Create(inputStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
   var imageFrame = imageDecoder.Frames[0];

   decimal aspect = (decimal)imageFrame.Width / (decimal)imageFrame.Height;
   var height = (int)(newWidth / aspect);

   var imageResized = new TransformedBitmap(imageFrame,new ScaleTransform(
                                                                 newWidth / imageFrame.Width * Dpi / imageFrame.DpiX,
                                                                 height / imageFrame.Height * Dpi / imageFrame.DpiY, 0, 0));

   var targetFrame = BitmapFrame.Create(imageResized);

   var targetEncoder = new JpegBitmapEncoder();
   targetEncoder.Frames.Add(targetFrame);
   targetEncoder.QualityLevel = 80;
   targetEncoder.Save(output);
}

I recommend the WPF way. The compression & quality seems better...

情话已封尘 2024-11-03 06:03:03

对我来说这是一个糟糕的位图参数。而不是这样:

new Bitmap(width, height, PixelFormat.Format32bppPArgb);

只需将 PixelFormat 删除为:

new Bitmap(width, height);

然后一切都很好。

使用 PixelFormat,我在顶部和左侧边框上有黑色边框。然后我尝试了 g.PixelOffsetMode = PixelOffsetMode.HighQuality;起初看起来不错。但后来我注意到整个图像周围有浅灰色边框。

For me it was a bad Bitmap parameter. Instead of this:

new Bitmap(width, height, PixelFormat.Format32bppPArgb);

Just remove the PixelFormat to this:

new Bitmap(width, height);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文