title: converting Image in memory to a Blob
body: <p>I have an Image (type: java.awt.Image) in memory, I want to convert it to a Blob (type: java.sql.Blob) using jdk 1.7.</p>
  
  <p>Everything I've been able to find on this subject uses streams and files.  Surely I don't need to save this Image to a file before being able to convert it??</p>
  
  <p>Not much to show here, but an example follows:</p>
  
  <p>import java.sql.Blob;
  import java.awt.Image;</p>
  
  <pre><code>public GenericResponseType savePhoto(Image image)
  {
         Connection conn = ds.getConnection();
  
         &lt;&lt; some DB code and assembly of PreparedStatement &gt;&gt;
  
         Blob blob = conn.createBlob();
  
             &lt;&lt; here's where the magic needs to happen I need to get the image parameter to blob &gt;&gt;
             // I've tried the following but doesn't quite work as it wants a RenderedImage
         // OutputStream os = blob.setBinaryStream(1);
         // ImageIO.write(parameters.getPhoto().getImage(), "jpg", os);
  
  
         pstmt.setBlob(4, blob);
       }
  </code></pre>
  
  <p>Some more detail (though I doubt it matters much) is that the above is generated using web services/JAX-WS from WSDL with an operation declared using MTOM.  So it generates a signature with an Image being passed as a variable.</p>