title: Java BufferedImage to PNG format Base64 String
body: <p>I'm trying to get a screenshot output as a base64 encoded string but not getting very far. The code I have so far uses a Base64 library ( <a href="http://iharder.sourceforge.net/current/java/base64/">http://iharder.sourceforge.net/current/java/base64/</a> ):</p>
  
  <pre><code>    Robot robot = new Robot();
      Rectangle r = new Rectangle( Toolkit.getDefaultToolkit().getScreenSize() );
      BufferedImage bi = robot.createScreenCapture(r);
      ByteArrayOutputStream os = new ByteArrayOutputStream();
      OutputStream b64 = new Base64.OutputStream(os);
      ImageIO.write(bi, "png", os);
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      out.writeTo(b64);
      String result = out.toString("UTF-8");
  </code></pre>
  
  <p>Each time I run this, "result" is always an empty string but I don't understand why. Any ideas?</p>
  
  <p>Note: I don't want to have to write the png to a file on disk.</p>