Ernest Duodu Ernest is a Java developer. Outside programming, he also enjoys a wide variety of hobbies which includes sky-diving, photography, exercising and listening to music.

How to write PNG Images in Java

1 min read

In this article, I will show you how to write PNG images in Java.

ImageIO is able to write PNG files, but it offers very little control over the PNG output and produces very large PNG files. So we will also show you our JDeli image library, which can produce much smaller PNG files.

What is PNG?

PNGPNG stands for Portable Network Graphics. It was created as a replacement for the GIF image file format which was limited by patents owned by Unisys. PNG is a non-patented replacement. It is a raster graphic file format that supports lossless data compression. It is used widely on the world wide web as it supports transparency in browsers. PNG files use non-lossy compression and support 8 and 24 bit colors.

The file name extension for PNG files is: .png

How to write out an image as a PNG file in ImageIO

Step 1 Create a File (or OutputStream) object for the PNG output.

File file = new File("/path/to/outputFile.png"));

Step 2 Pass image, png type and File (or OutputStream) object into write method

ImageIO.write(bufferedImage, "PNG", file);

How to write out an image as a PNG file with JDeli

Step 1 Add JDeli to your class or module path. (download link to the trial jar).

Step 2 Create a File (or OutputStream) object

File file = new File("/path/to/outputFile.png"));

Step 3 Pass image, PNG type and File (or OutputStream) object into write method

JDeli.write(bufferedImage, "PNG", file);

In JDeli you can also use a typesafe version

JDeli.write(bufferedImage, OutputFormat.PNG, file);

or pass in a PngEncoderOptions object for more control over PNG image output such as level of Compression.

PngEncoderOptions options = new PngEncoderOptions();
JDeli.write(bufferedImage, options, file);

Why use JDeli to write PNG?

JDeli offers a range of advantages over ImageIO and alternatives, including:

  • prevents heap related JVM crashes
  • More control over PNG file output
  • implements unsupported image formats
  • reduce output file size
  • improve write performance
  • supports threading
  • superior image scaling algorithms

Learn more about JDeli, or download to try it yourself.

Did you know...

IDRsolutions offers a whole range of online file converters to convert PDF and Microsoft Excel, Word and Office Documents to HTML5, SVG or image formats?

It is free to use for single file conversions and also includes Developer links if you want to use our commercial software for bulk conversions. Find out more on this page

Ernest Duodu Ernest is a Java developer. Outside programming, he also enjoys a wide variety of hobbies which includes sky-diving, photography, exercising and listening to music.

How to convert WMF files to SVG in java…

This article will show you how to convert WMF files into SVG files using our JDeli Java Image library. What is WMF? WMF is...
Amy Pearson
1 min read

How to convert EMF files to SVG in Java…

This article shows you how to convert EMF files into SVG files using our JDeli Java Image library. What is EMF? EMF is a...
Mark Stephens
1 min read

Leave a Reply

Your email address will not be published. Required fields are marked *

IDRsolutions Ltd 2020. All rights reserved.