Wednesday, July 27, 2022

[FIXED] How to crop bitmap and get the lower 50% of the Image

Issue

I have an Image which I want to crop. I already know that my required content is in the lower part of the image so how can I automatically crop it using Rectangle ?

I tried to use this Code (Below) to crop but it didn't help. I haven't used Rectangle much so please guide me.

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

the cropped image turned out fine but it's hardcoded. I need to make it dynamic so it gives same result for different images


Solution

Thanks @John for your answer. this code will cut 50% of the image and give your it's lower half:

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


Answered By - Abdul Moiz
Answer Checked By - Robin (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.