5

我正在尝试在 .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);
        }
4

3 回答 3

13

只需为 DrawImage 方法提供一个 ImageAttributes 实例,该实例将 WrapMode 设置为 TileFlipXY。这将防止边缘与背景颜色混合。

对于不会像此处的其他答案那样泄漏内存的示例代码,请参阅此要点

于 2012-08-07T17:20:29.227 回答
3

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

如果你想使用 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 方式。压缩和质量似乎更好......

于 2011-03-28T00:29:52.287 回答
2

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

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

只需将 PixelFormat 删除为:

new Bitmap(width, height);

那时一切都很好。

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

于 2017-02-13T20:36:52.190 回答