// Copy the scaled image into the new one
BufferedImage imageBuf = new BufferedImage(chosenWidth, chosenHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D newImage = imageBuf.createGraphics();
newImage.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
newImage.setBackground(hexToColor(bgColor)); // Set the background color
newImage.clearRect(0, 0, chosenWidth, chosenHeight);
newImage.drawImage(imgScaled, getImageOffset(chosenWidth, newWidth), getImageOffset(chosenHeight, newHeight),
newWidth, newHeight, null);
return imageBuf;
}