Bitmap images are a core part of modern graphics. These are used on websites, mobile devices, and desktop applications. Formats like BMP, GIF, PNG, and JPEG are all bitmap-based. In this article, you will learn how to create, load, fill, and draw bitmaps in Java.
Java Image Drawing SDK to Work with Bitmaps
In this article, we will use Aspose.Drawing for Java to create and draw on bitmaps in Java. It is a 2D graphics API that supports drawing shapes, text, and images across platforms. You can load and save various formats easily.
You can download the library from the releases and add the JAR to your project or install via Maven.
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-drawing</artifactId>
<version>25.6</version> <!-- Replace with latest version -->
</dependency>
1. Create a New Bitmap in Java
To create a bitmap from scratch, follow the steps below:
- Create a new
bitmap
with specific width and height. - Create a
Graphics
class object from the bitmap. - Initialize a
Pen
class object with a specific thickness and color. - Draw an ellipse on the bitmap using the pen.
- Save the bitmap as a PNG image file.
The following code sample shows how to create a new bitmap in Java.

Create a New Bitmap in Java
2. Create Bitmap from Memory Stream
To create a bitmap from memory stream, follow the steps below:
- Open a file input stream for the existing image file.
- Create a bitmap from the input stream.
- Save the bitmap to a new image file.
The following code sample shows how to create a bitmap using input stream in Java.
3. Save Bitmap to File
You can save a bitmap image to a file in multiple supported formats. Just specify the file path and desired image format when calling the save()
method. This allows you to export the bitmap as PNG, JPEG, BMP, GIF, and more.
You can save bitmaps to different formats by changing the image format:
- Save a Bitmap as a BMP File
bitmap.save("output.bmp", ImageFormat.getBmp().d());
- Save a Bitmap as a PNG File
bitmap.save("output.png", ImageFormat.getPng().d());
- Save a Bitmap as a JPG File
bitmap.save("output.jpg", ImageFormat.getJpeg().d());
- Save a Bitmap as a GIF File
bitmap.save("output.gif", ImageFormat.getGif().d());
4. Draw Shapes on Bitmap
You can draw a variety of shapes on a bitmap using the Graphics
class provided by Aspose.Drawing for Java. It allows you to create custom graphics and gives you full control over lines, curves, rectangles, ellipses, and more.
The following code sample shows how to draw shapes on a bitmap in Java.

Draw Shapes on Bitmap in Java
You can draw various shapes on a bitmap using the built-in drawing methods provided by the Graphics
class in Aspose.Drawing for Java. Below are the supported shape types and corresponding methods:
- Arc: Use the
drawArc()
method to draw an arc. - Bezier Curve: Use the
drawBezier()
method to draw a single Bezier curve. - Multiple Bezier Curves: Use the
drawBeziers()
method to draw a sequence of Bezier splines. - Curve: Use the
drawCurve()
method to create a smooth, curved shape. - Closed Curve: Use the
drawClosedCurve()
method to draw a closed spline. - Ellipse: Use the
drawEllipse()
method to draw an ellipse shape. - Line: Use the
drawLine()
method to draw a straight line. - Multiple Lines: Use the
drawLines()
method to draw a series of connected lines. - Path: Use the
drawPath()
method to render complex shapes using aGraphicsPath
. - Pie: Use the
drawPie()
method to draw a pie slice shape. - Polygon: Use the
drawPolygon()
method to create a polygon by connecting points. - Rectangle: Use the
drawRectangle()
method to draw a rectangle. - Multiple Rectangles: Use the
drawRectangles()
method to draw several rectangles at once.
In addition to shapes, you can also draw images on the bitmap:
- Icon: Use the
drawIcon()
method to draw an icon inside a specific rectangle. - Image: Use the
drawImage()
method to draw another image onto the bitmap.
These drawing methods make it easy to create custom graphics, diagrams, or annotated images programmatically.
5. Draw Text on Bitmap
To draw text on a bitmap by following the steps below:
- Create a new bitmap with a defined width and height.
- Create a graphics object from the bitmap.
- Clear the bitmap with a light gray background color.
- Define a font with the desired name and size.
- Create a solid brush with black color.
- Draw a text string on the bitmap using the specified font and brush.
- Save the bitmap as a PNG image file.
The following code sample shows how to draw text on a bitmap in Java.

Java Draw Text on Bitmap
6. Fill Bitmap with Solid Color
You can fill part of the image with a solid color by following the steps below:
- Create a new bitmap with the desired width and height.
- Create a graphics object from the bitmap.
- Create a solid gray brush for filling.
- Fill the entire bitmap area with the gray color using a filled rectangle.
- Save the bitmap as a PNG image file.
The following code sample shows how to fill a bitmap with a solid color using a rectangle in Java.

Java Draw Rectangle on Bitmap and Fill with a Color
7. Load an Image into Bitmap
You can also load an image file into a Bitmap
object as follows:
- Create a new blank bitmap with the desired size.
- Load an existing image into a bitmap object.
- Create a graphics object from the blank bitmap.
- Draw the loaded image onto the blank bitmap at the top-left corner.
- Save the final bitmap as a PNG image file.
The following code sample shows how to load an image in a bitmap using Java.

Load an Image in Bitmap using Java
8. Resize an Existing Image
To resize a bitmap image:
- Load the original image into a bitmap object.
- Create a new bitmap with half the width and height of the original image.
- Save the resized bitmap as a PNG image file.
The following code sample shows how to resize an existing image and save it as a new bitmap in Java.

Resize Existing Image using Java
Try Aspose.Drawing for Free
You can get a free temporary license to explore Aspose.Drawing without limits.
Create Bitmap in Java: Free Resources
if you want to explore more about working with bitmaps in Java then browse the following free resources to deepen your understanding and boost your productivity:
Conclusion
In this tutorial, you learned how to create and work with bitmaps using Aspose.Drawing for Java. We covered how to generate a bitmap from scratch, load and resize existing images, draw shapes and text, fill areas with solid colors, and save images in various formats such as PNG, JPG, BMP, and GIF. With these capabilities, you can easily add custom graphics and image processing to your Java applications.
Aspose.Drawing for Java makes it easy to work with 2D graphics programmatically. Visit the official documentation to explore more.