
Image clipping is the process of selecting a portion of an image and displaying only that specific area. It can be useful in a variety of scenarios, such as creating eye-catching thumbnails, drawing attention to specific sections of an image, etc. In this article, we will learn how to implement image clipping in Java. We will also explore step-by-step techniques to extract objects and enhance your designs.
This article covers the following topics:
- Java Image Clipping API - Free Download
- Image Clipping in Java
- Extract Object from Image in Java
- Free Resources
Java Image Clipping API - Free Download
For image clipping in Java, we will be leveraging the robust Aspose.Drawing for Java API, a cutting-edge solution for efficient and precise image manipulation. It is a versatile 2D graphics library for programmatic drawing in Java. It enables developers to load, save, and edit a wide range of supported file formats, making it an ideal choice for image clipping needs.
Please download the JAR of the API or add the following pom.xml configuration in a Maven-based Java application.
<repositories>
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-drawing</artifactId>
<version>24.4</version>
</dependency>
</dependencies>
Image Clipping in Java
We can implement image clipping programmatically in Java by following the steps below:
- Create an instance of the Bitmap class.
- Initialize the Graphics class object using the fromImage() method.
- Define the clip path using the GraphicsPath class.
- After that, set the clip path using the setClip() method.
- Finally, save the output image using the save() method.
The following code sample shows how to implement image clipping in Java.
// Create a new bitmap | |
Bitmap bitmap = new Bitmap(1000, 800); | |
// Initialize Graphics | |
Graphics graphics = Graphics.fromImage(bitmap); | |
graphics.setTextRenderingHint(TextRenderingHint.AntiAliasGridFit); | |
// Define Clip | |
RectangleF rectangle = new RectangleF(200, 200, 600, 400); | |
GraphicsPath clipPath = new GraphicsPath(); | |
clipPath.addEllipse(rectangle); | |
graphics.setClip(clipPath); | |
// Specify string | |
StringFormat stringFormat = new StringFormat(); | |
stringFormat.setAlignment(StringAlignment.Center); | |
stringFormat.setLineAlignment(StringAlignment.Center); | |
Brush brush = new SolidBrush(Color.getBlue()); | |
Font arial = new Font("Arial", 20, FontStyle.Regular); | |
String text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sapien tellus, mattis et condimentum eget, commodo ut ipsum. Maecenas elit sapien, tempus sit amet mauris sit amet, hendrerit laoreet nisi. Nulla facilisi. Sed commodo, mauris eget porta commodo, nunc tellus volutpat mi, eu auctor diam libero vel neque. Vestibulum nec mattis dui, nec molestie nisl. Etiam in magna felis. Praesent non nulla tortor. Integer nec convallis purus. Fusce vitae mollis mauris. Cras efficitur dui at mi viverra scelerisque. Morbi quis magna elit. Nulla facilisis id ante sit amet fringilla. Sed iaculis consectetur lectus a interdum. Etiam ut sollicitudin lectus, et congue lectus."; | |
// Draw string | |
graphics.drawString(text, arial, brush, rectangle, stringFormat); | |
// Save | |
bitmap.save("Clipping.png"); |

Java Image Clipping
Extract Object from Image in Java
We can extract a specific part of an image by following the steps mentioned earlier. However, we just need to load an existing image and draw it using the drawImage() method.
The following code sample shows how to extract object from an image in Java.
// This code example demonstrates how to extract an object from an image | |
// Create a new bitmap | |
Bitmap bitmap = new Bitmap(1000, 800); | |
// Initialize Graphics | |
Graphics graphics = Graphics.fromImage(bitmap); | |
graphics.setTextRenderingHint(TextRenderingHint.AntiAliasGridFit); | |
// Define Clip | |
RectangleF rectangle = new RectangleF(0, 0, 225, 225); | |
GraphicsPath clipPath = new GraphicsPath(); | |
clipPath.addEllipse(rectangle); | |
graphics.setClip(clipPath); | |
// Load the image | |
Bitmap image = new Bitmap("aspose-logo.png"); | |
// Draw image at specified location | |
graphics.drawImage(image,0,0); | |
// Save | |
bitmap.save("extract-object.png"); |

Extract Object from Image in Java
Get a Free License
You can get a free temporary license to try Aspose.Drawing for Java without evaluation limitations.
Aspose.Drawing – Free Resources
Besides image clipping in Java, you can learn more about various features of the library using the resources below:
Conclusion
In this article, we have explored the process of implementing image clipping in Java. By following the steps outlined in this article, you can now extract objects from images with precision and ease. In case of any ambiguity, please feel free to contact us on our free support forum.