title: Convert an ImageIcon to a Base64 String and back to an ImageIcon without saving to disk?
body: <p>I'm trying to store an imageIcon bas a Base64 String.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>public ImageIcon getImageIcon() {
      if(imageIcon == null || imageIcon.isEmpty()){
          return null;
      } else {
          try {
              byte[] btDataFile = Base64.decodeBase64(imageIcon);
              BufferedImage image = ImageIO.read(new ByteArrayInputStream(btDataFile));
              return new ImageIcon(image);
          } catch (IOException ex) {
              System.out.println(ex.getLocalizedMessage());
              return null;
          }
      }
  }
  
  public void setImageIcon(ImageIcon imageIconIn) {
      imageIcon = Base64.encodeBase64String(imageToByteArray(imageIconIn));
  }
  
  public static byte[] imageToByteArray(ImageIcon imageIn) {
      try {
  
          BufferedImage image = new BufferedImage(imageIn.getIconWidth(), imageIn.getIconHeight(),BufferedImage.TYPE_INT_RGB);
          ByteArrayOutputStream b = new ByteArrayOutputStream();
  
          // FIX
          Graphics g;
          g = image.createGraphics();
          imageIn.paintIcon(null, g, 0,0);
          // END FIX
  
          ImageIO.write(image, "jpg", b );
  
          g.dispose();
  
          return b.toByteArray();
  
      } catch (IOException ex) {
          System.out.println(ex.getLocalizedMessage());
          return null;
      }
  }
  </code></pre>
  
  <p>I get a black rectangle instead of the image.</p>
  
  <p>I'm using Java 1.8 on Ubuntu 16.04.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>Thanks for your help.</p>
  
  <p>******************************** . FIXED . ******************************</p>
  
  <p>I found a working solution and updated the above code.</p>
  
  <p>********************************  EDIT  *********************************</p>
  
  <p>Added g.dispose() after painting icon.</p>