【问题标题】:How to crop bitmap and get the lower 50% of the Image如何裁剪位图并获得图像的下 50%
【发布时间】:2019-07-18 06:58:00
【问题描述】:

我有一张想要裁剪的图片。我已经知道我需要的内容位于图像的下部,那么如何使用 Rectangle 自动裁剪它?

我尝试使用此代码(如下)进行裁剪,但没有帮助。 Rectangle我用的不多,请指导一下。

private Bitmap cropImage(Bitmap img)
{
    int height= img.Height / 2;
    Rectangle CropArea = new Rectangle(100,height, 1400, 900);
    Bitmap bmpCrop = img.Clone(CropArea, img.PixelFormat);
    return bmpCrop;
}

裁剪后的图像效果很好,但它是硬编码的。我需要使其动态化,以便为不同的图像提供相同的结果

【问题讨论】:

  • 只使用尺寸?
  • 不太清楚你在问什么。您已经意识到可以通过img.Widthimg.Height 获取图像大小,那么问​​题出在哪里?
  • @John 宽度和高度可能会根据裁剪的图像而改变。我该如何处理?
  • @TheGeneral 请解释一下

标签: c# image-processing crop


【解决方案1】:

感谢@John 的回答。此代码将削减 50% 的图像并给你它的下半部分:

private Bitmap cropImage(Bitmap img)
{
    int height= img.Height / 2;
    int newWidth = img.Width -100;
    int newHeight = img.Height - height;
    Rectangle CropArea = new Rectangle(100,height,newWidth,newHeight);
    Bitmap bmpCrop = img.Clone(CropArea, img.PixelFormat);
    return bmpCrop;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 2019-11-07
    • 2020-09-23
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多