当裁剪图像时,出现黑色边框

时间:2022-06-01 12:21:13

Sorry for my English. I'm trying to crop an image after dragging. It works but my problem is, black bold borders appears on cropped image. I don't know how to solve it.

对不起,我的英语。我试图在拖拽后裁剪一个图像。它可以工作,但我的问题是,黑色粗体边框出现在裁剪后的图像上。我不知道怎么解决。

Here is my code:

这是我的代码:

using (Bitmap sourceBitmap = new Bitmap(fullSizeImage))
{
    Rectangle cropRect = new Rectangle(0, 0, sourceWidth, sourceHeight);
    using (Bitmap newBitMap = new Bitmap(cropRect.Width, cropRect.Height))
    {
       using (Graphics g = Graphics.FromImage(newBitMap))
       {
           g.SmoothingMode = SmoothingMode.HighQuality;
           g.PixelOffsetMode = PixelOffsetMode.HighQuality;
           g.CompositingQuality = CompositingQuality.HighQuality;
           g.CompositingMode = CompositingMode.SourceCopy;
           g.DrawImage(sourceBitmap, new Rectangle(0, pp, sourceWidth, sourceHeight), cropRect, GraphicsUnit.Pixel);
           g.InterpolationMode = InterpolationMode.HighQualityBicubic;
           ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders()[4];
           EncoderParameters parameters = new EncoderParameters(1);
           parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
           newBitMap.Save(filePath);
        }
    }
}

And here is cropped image:当裁剪图像时,出现黑色边框

这里是裁剪过的图片:

1 个解决方案

#1


1  

Your destination image is the same physical size, because you use the same physical dimensions to create it:

您的目标图像具有相同的物理尺寸,因为您使用相同的物理尺寸来创建它:

Rectangle cropRect = new Rectangle(0, 0, sourceWidth, sourceHeight);

When you draw the original image into this new bitmap, you draw it with an offset pp, but still with the same height and width (so you're only cropping the bottom and right). This offset "colours" the memory of the new bitmap only from that y-coordinate downwards, and hence you have the black border.

当你在这个新的位图中绘制原始图像时,你用一个偏移的pp绘制它,但是仍然保持相同的高度和宽度(所以你只裁剪底部和右侧)。这个偏移“颜色”的记忆新的位图只有从y坐标向下,因此你有黑色的边界。

#1


1  

Your destination image is the same physical size, because you use the same physical dimensions to create it:

您的目标图像具有相同的物理尺寸,因为您使用相同的物理尺寸来创建它:

Rectangle cropRect = new Rectangle(0, 0, sourceWidth, sourceHeight);

When you draw the original image into this new bitmap, you draw it with an offset pp, but still with the same height and width (so you're only cropping the bottom and right). This offset "colours" the memory of the new bitmap only from that y-coordinate downwards, and hence you have the black border.

当你在这个新的位图中绘制原始图像时,你用一个偏移的pp绘制它,但是仍然保持相同的高度和宽度(所以你只裁剪底部和右侧)。这个偏移“颜色”的记忆新的位图只有从y坐标向下,因此你有黑色的边界。