Reputation: 35
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
Upvotes: 0
Views: 1475
Reputation: 35
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;
}
Upvotes: 2