views:

249

answers:

1

I need to create a new, smaller, image from a larger image at runtime. The smaller image size is fixed (square) and represents a specific region of the larger image (the smaller image is a subset of the larger image). Image format doesn't matter.

Thanks.

+3  A: 

Hi!

You can use this function:

Bitmap[] splitImage(Bitmap bitmap, int rCnt, int cCnt) {
    Bitmap[] result = new Bitmap[rCnt * cCnt];
    int w = bitmap.getWidth() / cCnt;
    int h = bitmap.getHeight() / rCnt;
    for (int i = 0; i < rCnt; i++)
        for (int j = 0; j < cCnt; j++) {
            Bitmap bitmapPart = new Bitmap(w, h);
            Graphics g = new Graphics(bitmapPart);
            g.drawBitmap(0, 0, w, h, bitmap, w * j, h * i);
            result[i * cCnt + j] = bitmapPart;
        }
    return result;
}

Take a look at full source of Puzzle game for BB on Google Code

Max Gontar
Thanks for the answer Max. This is very helpful.
Peter Parker
You're welcome!
Max Gontar