Java Image Load getImageFromFile(String fileName)

Here you can find the source of getImageFromFile(String fileName)

Description

get Image From File

License

Open Source License

Parameter

Parameter Description
fileName a parameter

Declaration

public static ImageIcon getImageFromFile(String fileName) 

Method Source Code

//package com.java2s;
/**/* w w w  . j a v  a2s . co  m*/
 * Copyright? 2014-2016 LIST (Luxembourg Institute of Science and Technology), all right reserved.
 * Authorship : Olivier PARISOT, Yoanne DIDRY
 * Licensed under GNU General Public License version 3
 */

import java.io.*;
import javax.swing.ImageIcon;

public class Main {
    /**
     * 
     * @param fileName
     * @return
     */
    public static ImageIcon getImageFromFile(String fileName) {
        try {
            final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            int nRead;
            byte[] data = new byte[16384];
            while ((nRead = is.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, nRead);
            }
            buffer.flush();
            return new ImageIcon(buffer.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. getImage(String path)
  2. getImage(String text, boolean clockwise)
  3. getImage(String url)
  4. getImage(URL url)
  5. getImageFromClassResource(Class cls, String resource)
  6. getImageFromSource(JPanel source, Dimension dim)
  7. getLocal(String imagePath)
  8. loadImage(File original)
  9. loadImage(final String src)