Amy Pearson Amy is the product lead for JDeli with expertise in image code, Java, web development, and cloud computing. She focuses on JDeli and has also contributed to JPedal, cloud services, and support. Outside work, she enjoys gaming, F1, and music.

How to write PNG Images in Java (Tutorial)

3 min read

PNG icon

PNG images in Java

TL;DR:

Java’s ImageIO can write PNG files, but gives you almost no control over the output — no compression level, no colour depth options, no metadata handling. For basic use that is fine. If you need control over PNG output, you need an external library. JDeli is a pure Java option that works as a drop-in ImageIO plugin and provides a direct API with full encoder options.

Does Java support PNG natively?

Yes, but only at a basic level. ImageIO can write PNG files without any additional libraries:

File file = new File("/path/to/output.png");
ImageIO.write(bufferedImage, "PNG", file);

The problem is that ImageIO’s PNG writer exposes no encoder options. You cannot set compression level, choose colour depth, control interlacing, or embed metadata. For many use cases — especially where file size or image quality matter — this is a real limitation.

Which Java PNG library should you use?

ImageIO (native)Apache ImagingJDeli
Pure Java (no native binaries)
Works as ImageIO plugin
Direct API
Compression level controlLimited
Colour depth options
Supports other formats (AVIF, HEIC, WebP…)
CostFreeFreeCommercial
SupportCommunityApache / communityIncluded

Native ImageIO is sufficient for straightforward PNG output where file size and quality tuning are not a concern. Apache Imaging is a reasonable free option if you need more control. JDeli is the better choice for production use, or where you need consistent behaviour across formats and the ability to tune output precisely.

Option 1: Write PNG with native ImageIO

If you just need to produce a PNG file with no particular constraints, native ImageIO is the simplest route:

File file = new File("/path/to/output.png");
ImageIO.write(bufferedImage, "PNG", file);

Be aware that ImageIO will make its own decisions about compression and colour depth. For files destined for the web or storage-constrained environments, this can produce larger files than necessary.

Option 2: Add controlled PNG output to ImageIO (no code changes)

JDeli registers itself as an ImageIO plugin. Once it is on the classpath, your existing ImageIO.write() calls use JDeli automatically with no code changes required.

Maven

<dependency>
    <groupId>com.idrsolutions</groupId>
    <artifactId>jdeli</artifactId>
    <version>[JDELI_VERSION]</version>
</dependency>

Gradle

implementation 'com.idrsolutions:jdeli:[JDELI_VERSION]'

Alternatively, download the trial jar and add it to your classpath manually. See the ImageIO configuration guide for setup details.

// Your existing ImageIO code — no changes needed
File file = new File("/path/to/output.png");
ImageIO.write(bufferedImage, "PNG", file);

Option 3: Write PNG directly with the JDeli API

The JDeli API gives you explicit control over output and does not depend on ImageIO’s plugin registration order.

  1. Add JDeli to your class or module path. (download the trial jar)
  2. Create a File or OutputStream for the output.
  3. Call JDeli.write() with your image, format, and output target.
// String format
JDeli.write(bufferedImage, "PNG", new File("/path/to/output.png"));
// Type-safe enum (preferred)
JDeli.write(bufferedImage, OutputFormat.PNG, new File("/path/to/output.png"));

Controlling output with PngEncoderOptions

For production use, pass a PngEncoderOptions object to control compression and other output settings:

PngEncoderOptions options = new PngEncoderOptions();
JDeli.write(bufferedImage, options, new File("/path/to/output.png"));

See the JDeli PNG documentation for the full list of encoder options.

Frequently asked questions

Does Java support PNG natively?

Yes, but with limited control. ImageIO can write PNG files without any additional library, but exposes no encoder options. You cannot control compression level, colour depth, or interlacing without an external library such as JDeli.

Why is my PNG file larger than expected?

ImageIO’s PNG writer makes its own compression decisions and offers no way to tune them. Using JDeli with PngEncoderOptions gives you direct control over compression level, which can significantly reduce file size without quality loss — PNG compression is lossless.

What is JDeli?

JDeli is a pure Java image library from IDR Solutions. It has no native binaries, works as both an ImageIO plugin and a standalone API, and supports PNG alongside other formats including AVIF, HEIC, TIFF, and WebP.

Can I read PNG files in Java with JDeli?

Yes. See our companion article: how to read PNG files in Java.

Is JDeli pure Java?

Yes. JDeli has no native binaries and makes no calls to platform-specific libraries, making it straightforward to deploy on any Java-supported platform including containerised and server environments.

Other useful PNG resources

As experienced Java developers, we help you work with images in Java and bring over a decade of hands-on experience with many image file formats.



Are you a Java Developer working with Image files?

Amy Pearson Amy is the product lead for JDeli with expertise in image code, Java, web development, and cloud computing. She focuses on JDeli and has also contributed to JPedal, cloud services, and support. Outside work, she enjoys gaming, F1, and music.