All software

Win32 Icon Loader

Reads Windows .ico files into Java images, palette and transparency mask included, with nothing but the JDK.

2002JavaWin32

A 100% win32 icon loader (*.ico) in Java. The library is distributed under the GNU Public License.

In 2002 Java could not read a Windows icon. ImageIO knew GIF, JPEG and PNG, so if you wanted a Swing toolbar to carry the icons that came with a Windows program, you converted them by hand, one by one, or you read the format yourself. This reads it. It is a small format (a directory, then one bitmap per entry), and the whole loader is three classes.

What it does

Win32IconResource.loadIcon("app.ico") reads the directory at the top of the file. One .ico holds several images (different sizes, different colour depths), so what comes back is a list of Win32Icon objects, each loaded on demand with loadIcon(): the 40-byte info header, the palette (stored blue-green-red, as Windows does), the XOR bitmap at 1, 4 or 8 bits per pixel, and the 1-bit AND mask. The mask becomes the alpha channel, so the result is a BufferedImage with real transparency that goes straight into an ImageIcon.

Win32IconResource res = Win32IconResource.loadIcon("app.ico");
for (Win32Icon icon : res.getWin32Icon()) {
    icon.loadIcon();
    toolbar.add(new JButton(icon.getImageIcon()));
}

Two extras came with it, because a toolbar needs them: getGrayscaledImage() for a disabled button and getBlendedImage(Color, opacity) for a tinted one. The third class, CDataInputStream, exists because Java's DataInputStream reads big-endian and the file is little-endian; it reads the "reversed" unsigned integers and shorts the format is made of.

Where it stops

At 8 bits per pixel. A 24- or 32-bit icon, which is anything with an alpha channel of its own (standard since Windows XP), stops with Undefined bitCount, and the PNG-compressed icons of Vista and later are not recognised at all. I checked in 2026, on JDK 26: it compiles (with the warnings about raw Vectors you would expect from 2002 code), a 16-colour 32×32 icon from one of my old programs loads and renders as it should, and a 32-bit favicon is refused exactly as described. The test program, IconTest, opens each icon three ways (plain, greyed out, tinted blue); it imports a LibsUtil from a larger collection of mine that is not in the archive, so drop that one println and it compiles too.

One of the small utilities (2002–2007); the others are listed there, each with a page like this one.

Downloads

  • icon.zipsource and the test program
  • bmp.zipMicrosoft's description of the BMP, ICO and CUR formats (1994), which the loader was written against

These are the original files, kept as they were published. Most are decades old and are here as a record rather than as working software.