Question: Basically, I have a rectangular bitmap and want to create a new Bitmap with squared dimensions which will contain the rectangular bitmap inside of it.

So, for example, if the source bitmap has width:100 and height:400, I want a new bitmap with width:400 and height:400. Then, draw the source bitmap centered inside of this new bitmap (see attached image for a better understanding).

![Example of the expected result](https://i.stack.imgur.com/FHBOt.png)

My code below creates the square bitmap fine, but the source bitmap is not being drawn into it. As a result, I'm left with a bitmap that is completely black.

Here is the code:

```
Bitmap sourceBitmap = BitmapFactory.decodeFile(sourcePath);

Bitmap resultBitmap= Bitmap.createBitmap(sourceBitmap.getHeight(), sourceBitmap.getHeight(), Bitmap.Config.ARGB_8888);

Canvas c = new Canvas(resultBitmap);

Rect sourceRect = new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight());
Rect destinationRect = new Rect((resultBitmap.getWidth() - sourceBitmap.getWidth())/2, 0, (resultBitmap.getWidth() + sourceBitmap.getWidth())/2, sourceBitmap.getHeight());
c.drawBitmap(resultBitmap, sourceRect, destinationRect, null);

// save to file
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp");
File file = new File(mediaStorageDir.getPath() + File.separator + "result.jpg");
try {
    result.compress(CompressFormat.JPEG, 100, new FileOutputStream(file));
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

```

Any idea what I'm doing wrong?

Answer: 
Try this:

```
    private static Bitmap createSquaredBitmap(Bitmap srcBmp) {
        int dim = Math.max(srcBmp.getWidth(), srcBmp.getHeight());
        Bitmap dstBmp = Bitmap.createBitmap(dim, dim, Config.ARGB_8888);

        Canvas canvas = new Canvas(dstBmp);
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(srcBmp, (dim - srcBmp.getWidth()) / 2, (dim - srcBmp.getHeight()) / 2, null);

        return dstBmp;
    }

```
```
/**
 * Add a shape to bitmap.
 */
fun addShapeToBitmap(originBitmap: Bitmap): Bitmap {

    val stream = ByteArrayOutputStream()

    // **create RectF for a rect shape**
    val rectF = RectF()
    rectF.top = originBitmap.height / 2f - 100f
    rectF.bottom = originBitmap.height / 2f + 100f
    rectF.left = originBitmap.width / 2f - 100f
    rectF.right = originBitmap.width / 2f + 100f

    // **create a new bitmap which is an empty bitmap**
    val newBitmap = Bitmap.createBitmap(originBitmap.width, 
    originBitmap.height, originBitmap.config)
    // **create a canvas and set the empty bitmap as the base layer**
    val canvas = Canvas(newBitmap)
    // **draw the originBitmap on the first laye
    canvas.drawBitmap(originBitmap, 0f, 0f, null)
    // **draw the shape on the second layer**
    // **the second layer cover on the first layer**
    canvas.drawRect(rectF, paint)
    newBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
    newBitmap.recycle()
    return newBitmap
}

```