victorio
victorio

Reputation: 6646

How to read an ico format picture in java?

I have a lot of .ico formatted pictures, and I want to use them in my Java SE project, but it doesn't know the format. How can I work around this?

Upvotes: 9

Views: 9076

Answers (3)

user14972917
user14972917

Reputation:

Hint for reading ico files with Apache Commons Imaging 1.0-alpha2:

There seems to be a difference in between reading ico files as a file and reading ico files as a byte[]: Imaging.getAllBufferedImages(File) reads an ico file, Imaging.getAllBufferedImages(new ByteArrayInputStream(byte[] icoFileContent, yourIcoFilename) also reads the ico file. Imaging.getAllBufferedImages(byte[]) does not read the same ico file but throws an ImageReadException. See code below.

    File icoFile = new File("bluedot.ico");

    // Works fine
    List<BufferedImage> images = Imaging.getAllBufferedImages(icoFile);
    Assert.assertFalse(images.isEmpty());
    ImageIO.write(images.get(0), "png", new File("bluedot.png"));

    // Also works fine
    byte[] icoFileContent = Files.readAllBytes(icoFile.toPath());
    images = Imaging.getAllBufferedImages(new ByteArrayInputStream(icoFileContent), "bluedot.ico");
    Assert.assertFalse(images.isEmpty());
    ImageIO.write(images.get(0), "png", new File("bluedot2.png"));

    // Throws an exception
    images = Imaging.getAllBufferedImages(icoFileContent);

Additionally here is a guide how I created the .ico file that is not readable by Apache Commons Imaging 1.0-alpha2 as byte[] (but is readable as File and is readable as ByteArrayInputStream):

  • Start GIMP (in my case version 2.10.22)
  • Window menu "File" > "New..."
  • Template: [empty]
  • Width: 48px
  • Height: 48px
  • Leave the rest as is (see screenshot below)
  • Draw something (e.g. a blue dot)
  • Window menu "File" > "Export as..."
  • File name: "bluedot.ico"
  • Icon Details: "4 bpp, 1-bit alpha, 16-slot palette"
  • Compressed (PNG): Not checked
  • Click "Export"
  • Imaging.getAllBufferedImages(byte[]) will throw org.apache.commons.imaging.ImageReadException: Can't parse this format.
  • Imaging.getAllBufferedImages(File) will read this file.

Dialog for creating an ico file

Upvotes: 0

gouessej
gouessej

Reputation: 4075

Apache Commons Imaging allows to read and write ICO files:

    List<BufferedImage> images = Imaging.getAllBufferedImages(new File("input.ico"));

It supports several popular formats of metadata too (EXIF, IPTC and XMP).

TwelveMonkeys ImageIO allows to extend the ImageIO API to support ICO and numerous other image file formats.

Upvotes: 4

Konrad Reiche
Konrad Reiche

Reputation: 29493

Try out image4j - Image Library for Java

The image4j library allows you to read and write certain image formats in 100% pure Java.

Currently the following formats are supported:

  • BMP (Microsoft bitmap format - uncompressed; 1, 4, 8, 24 and 32 bit)
  • ICO (Microsoft icon format - 1, 4, 8, 24 and 32 bit [XP uncompressed, Vista compressed])

With the library you can easily decode your ico file

List<BufferedImage> image = ICODecoder.read(new File("input.ico"));

Upvotes: 8

Related Questions