Cut the Portion of Bitmap

cut the portion of bitmap

Easiest way I am aware of is to use XFer mode processing from the Graphics package. Function below cuts region starting from (30,30) till (100,100) to the 320x480 image loaded from resources. Adapt coordinates to change dynamically:

private Bitmap cropBitmap1() {
Bitmap bmp2 = BitmapFactory.decodeResource(this.getResources(), R.drawable.image1);
Bitmap bmOverlay = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);

Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));

Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp2, 0, 0, null);
canvas.drawRect(30, 30, 100, 100, paint);

return bmOverlay;
}

Android Crop Center of Bitmap

Sample Image

This can be achieved with: Bitmap.createBitmap(source, x, y, width, height)

if (srcBmp.getWidth() >= srcBmp.getHeight()){

dstBmp = Bitmap.createBitmap(
srcBmp,
srcBmp.getWidth()/2 - srcBmp.getHeight()/2,
0,
srcBmp.getHeight(),
srcBmp.getHeight()
);

}else{

dstBmp = Bitmap.createBitmap(
srcBmp,
0,
srcBmp.getHeight()/2 - srcBmp.getWidth()/2,
srcBmp.getWidth(),
srcBmp.getWidth()
);
}

How do I cut out the middle area of the bitmap?

You might want to use the other overload of createBitmap - it has x, y, width and height parameters which you could use to crop the middle portion of the bitmap into a new bitmap.

Something like this:

Bitmap cropped = Bitmap.createBitmap(sourceBitmap, 50, 50, sourceBitmap.getWidth() - 100, sourceBitmap.getHeight() - 100);

to clip out everything 50 pixels in from the edges.

Crop bottom portion of a bitmap programmatically

I am not familiar with operations on Bitmaps but from inspecting your code and looking at the API my guess would be that you need to specify the y coordinates on the following line to match the starting point:

Bitmap croppedBitmap = Bitmap.createBitmap(toBeCropped, 0, "here", toBeCropped.getWidth(), fromHere);

So my guess would be something like the following:

Bitmap croppedBitmap = Bitmap.createBitmap(toBeCropped, 0, (toBeCropped.getHeight() * 0.8), toBeCropped.getWidth(), fromHere);

in this case fromHere will define the number of rows you want to crop not the starting point (which is 20% of the total as you have pointed out)

How to cut a part of image in C#

Check out the Graphics Class on MSDN.

Here's an example that will point you in the right direction (notice the Rectangle object):

public Bitmap CropImage(Bitmap source, Rectangle section)
{
var bitmap = new Bitmap(section.Width, section.Height);
using (var g = Graphics.FromImage(bitmap))
{
g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
return bitmap;
}
}

// Example use:
Bitmap source = new Bitmap(@"C:\tulips.jpg");
Rectangle section = new Rectangle(new Point(12, 50), new Size(150, 150));

Bitmap CroppedImage = CropImage(source, section);

How crop bitmap of selected area on canvas?

I know its too late for your solution but this may help to others Use of this code
help you to come out from this problem.



Related Topics



Leave a reply



Submit