General
As a headless browser, HtmlUnit does not display images. Most of the time it doesn't even have to look inside an image. There are some exceptions where HtmlUnit has to decode the image:
-
reading the real size of an image, e.g. via the
width,height,naturalWidthornaturalHeightproperties, orHtmlImage.getWidth()/HtmlImage.getHeight(); the size is also used for layout related methods likeElement.getBoundingClientRect() -
drawing an image onto a canvas with
CanvasRenderingContext2D.drawImage()
For decoding, HtmlUnit uses Java's ImageIO. Out of the box, ImageIO supports JPEG, PNG, BMP, WBMP, GIF and TIFF.
When are images downloaded
To save time and bandwidth, HtmlUnit downloads an image only if
- the image element has an
onloadoronerrorhandler (and JavaScript is enabled), - the image is needed for one of the cases listed above, or
- you enabled the download of all images with
webClient.getOptions().setDownloadImages(true).
Downloading the image is not the same as decoding it. HtmlImage.saveAs(File),
for example, works for every format.
Unsupported image formats
If ImageIO has no reader for the format of an image (e.g. SVG or WebP), HtmlUnit behaves like this:
widthandheightreport the size of a broken image icon (depending on the simulated browser),naturalWidthandnaturalHeightare 0.drawImage()draws nothing and logs an INFO message that points to this page.- The
loadevent depends only on the download. It is fired even if the image can't be decoded, while real browsers fire anerrorevent in this case.
To overcome this limitation, you can add ImageIO plugins to the classpath. They are found automatically, no HtmlUnit configuration is required. The TwelveMonkeys project provides plugins for many formats, e.g. WebP, SVG, ICO and PSD.
Examples
WebP
WebP is used by many websites today. The TwelveMonkeys WebP plugin is pure Java and has no further dependencies:
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-webp</artifactId>
<version>3.15.3</version>
</dependency>
SVG
The TwelveMonkeys SVG plugin is a wrapper for Apache Batik. Batik is not included, you have to add it yourself:
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-batik</artifactId>
<version>3.15.3</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-transcoder</artifactId>
<version>1.20</version>
<exclusions>
<!-- HtmlUnit already depends on commons-io and commons-logging -->
<exclusion>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</exclusion>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<!-- the JDK already contains these XML APIs -->
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
</dependency>
Batik is a large library and has had several security issues in the past (see Apache XML Graphics Security). Use the latest version, and add it only if you need SVG images on a canvas or their size.
Check your setup
To check that a plugin is found, ask ImageIO for a reader. This example checks the SVG plugin:
final String svg = "<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'>"
+ "<circle cx='5' cy='5' r='4' fill='red'/>"
+ "</svg>";
final BufferedImage img = ImageIO.read(new ByteArrayInputStream(svg.getBytes(StandardCharsets.US_ASCII)));
// prints null if there is no reader for SVG
System.out.println(img);
Or list the formats ImageIO supports:
System.out.println(Arrays.toString(ImageIO.getReaderMIMETypes()));
Android
ImageIO is not available on Android. There, HtmlUnit doesn't decode images at all and reports a size of 0 x 0.

