In this article I will show you how to read PNG files in Java. I will show you how to do this using ImageIO, and secondly using JDeli.
We previously used ImageIO to read PNG files, but over time became increasingly dissatisfied as we discovered more and more issues. Eventually we wrote our own PNG Decoder which fixes those issues and is now available as part of JDeli.
First a quick introduction to PNGs…
What is a PNG?
PNG stands for Portable Network Graphics. It is a lossless image format popular on the world wide web because it supports transparency in browsers.
How do I read PNG Files?
ImageIO
ImageIO is included in the JDK (Javadoc here). Here is how to read a png file with ImageIO:
File imageFile = new File("image.png"); BufferedImage image = ImageIO.read(imageFile); |
JDeli
You can easily replace ImageIO (and get much better support for PNG files) by just changing ImageIO.read to JDeli.read:
//Read Image (can also be OutputStream or byte array) File imageFile = new File("image.png"); BufferedImage image = JDeli.read(imageFile); |
Or you can use the JDeli PngDecoder directly:
File imageFile = new File("image.png"); PngDecoder decoder = new PngDecoder(); BufferedImage image = decoder.read(imageFile); |
Alternatively, you can use the raw image data:
PngDecoder decoder = new PngDecoder(); BufferedImage image = decoder.read(rawPngData); |
Why use JDeli?
JDeli offers a range of advantages over ImageIO and alternatives, including:
- prevents heap related JVM crashes
- implements unsupported image formats
- reduce output file size
- improve read/write performance
- supports threading
- superior image scaling algorithms
Learn more about JDeli, or download to try it yourself.
This article is part of our series on reading and writing image files in Java.

it throws a exception using imageio.read() where the size of the file >50M
how to solve this exception?
Have you tried adding more memory with -Xmx command. Java converts into ARGB so 50meg file could be a large image.