0

I'm am trying to get the Icon from a .lnk file, put it into a javafx Image and then save it as a .png file (to ensure it's working).

My current code compiles but does not work:

import java.io.*;
import java.util.*;
import javax.swing.filechooser.FileSystemView;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javafx.embed.swing.SwingFXUtils;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import javafx.scene.image.Image;
import javax.imageio.ImageIO;
class Iconic {
    public static void main(String[] args) throws IOException{
        File origin = new File("C:\\Users\\00001\\OneDrive.lnk");
        Icon icn = FileSystemView.getFileSystemView().getSystemIcon(origin);
        ImageIcon ico = ((ImageIcon) icn);
        BufferedImage bi = new BufferedImage(
            ico.getIconWidth(),
            ico.getIconHeight(),
            BufferedImage.TYPE_INT_RGB);
        Graphics g = bi.createGraphics();
        ico.paintIcon(null,g,0,0);
        g.dispose();
        Image img = SwingFXUtils.toFXImage(bi,null);

        File output = new File("C:\\Users\\00001\\");
        BufferedImage bim = SwingFXUtils.fromFXImage(img,null);
        ImageIO.write(bim,".png",output);
    }
}
  • "Does not work". So what happens? Do you get an exception? If so, post the stack trace. And it looks like you are trying to write the image to a directory at the end...? – James_D Nov 16 '16 at 19:09
  • Currently, it does not throw any exceptions, however it does not save the .lnk file's icon as a .jpg – JoNation_ Nov 17 '16 at 17:36

1 Answers1

0

You are almost there, however, there are two issues in your code. Both are related to the ImageIO.write(...) method.

From the API doc of ImageIO.write(RenderedImage, String, File):

Writes an image using an arbitrary ImageWriter that supports the given format to a File. If there is already a File present, its contents are discarded.

Parameters:

im - a RenderedImage to be written.

formatName - a String containg the informal name of the format.

output - a File to be written to.

  1. The second parameter is the format name, not the file extension. So, the second argument should be "PNG", not ".png". Because ImageIO does not find any plugins that can write ".png" format, the write(...) invocation will just silently return false. I recommend always checking the return value of ImageIO.write(...).

  2. The third parameter is the destination file to write. The path of this file has to point to a file, however yours point only to a directory. If you fix only the format name above, you'll see that most likely you get an IOException.

So, a fixed version of your writing code would like like:

File output = new File(origin.getParentFile(), origin.getName().replace(".lnk", ".png"));

if (!ImageIO.write(bi, "PNG", output)) {
    System.err.println("Could not write icon");
}

I deliberately left the conversion from FX Image out. You should be able to write bi directly, without conversion to and from FX Image(unless, of course, you manipulate the image in FX).

Community
  • 1
  • 1
Harald K
  • 26,314
  • 7
  • 65
  • 111