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

标签 c# asp.net-mvc crop rectangles drawrectangle

对不起我的英语。我试图在拖动后裁剪图像。它有效,但我的问题是,黑色粗体边框出现在裁剪后的图像上。不知道怎么解决。

这是我的代码:

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);
        }
    }
}

这是裁剪后的图像:enter image description here

最佳答案

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

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

当您将原始图像绘制到这个新位图中时,您使用偏移量 pp 绘制它,但仍然具有相同的高度和宽度(因此您只裁剪了底部和右侧)。此偏移仅从该 y 坐标向下“着色”新位图的内存,因此您有黑色边框。

关于c#当裁剪图像出现黑色边框时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47094384/

相关文章:

python - 使用 Opencv python 从图像中裁剪矩形

python - 裁剪和存储图像集合的边界框图像区域?

c# - IdentityServer4 Asp.Net 核心身份

asp.net-mvc - 仅当不使用角色时,如何将 [Authorize] 重定向到 loginUrl?

c# - MVC : Displaying Roles in a list of checkboxes, 然后保存它们

SimpleModal 中的 JQuery 自动完成

python - 类型错误 : crop() takes from 1 to 2 positional arguments but 5 were given

c# - 项目上的 WPF 工具提示不会重新触发初始延迟

c# - 图像大小调整 - 有时质量很差?

c# - 反序列化 XML。捕获错误?