title: Saving an image to .jpg file : Java
body: <p>The way I am saving a jFreeChart to a jpeg file is :</p>
  <pre><code>JFreeChart chart = ChartFactory.createXYLineChart(
  &quot;Hysteresis Plot&quot;, // chart title
  &quot;Pounds(lb)&quot;, // domain axis label
  &quot;Movement(inch)&quot;, // range axis label
  dataset, // data
  PlotOrientation.VERTICAL, // orientation
  false, // include legend
  true, // tooltips
  false // urls
  ); 
  </code></pre>
  <p>Then:</p>
  <pre><code> image=chart.createBufferedImage( 300, 200);
  </code></pre>
  <p>The image appeas as:
  <img src="https://i.stack.imgur.com/nkpQ8.png" alt="enter image description here" /></p>
  <p>My save function is:</p>
  <pre><code>public static void saveToFile(BufferedImage img)
      throws FileNotFoundException, IOException
      {
          FileOutputStream fos = new FileOutputStream(&quot;D:/Sample.jpg&quot;);
          JPEGImageEncoder encoder2 =
          JPEGCodec.createJPEGEncoder(fos);
          JPEGEncodeParam param2 =
          encoder2.getDefaultJPEGEncodeParam(img);
          param2.setQuality((float) 200, true);
          encoder2.encode(img,param2);
          fos.close();
      }
  </code></pre>
  <p>I am calling it as:</p>
  <pre><code>try{
              saveToFile(image);
             }catch(Exception e){
             e.printStackTrace();
           }
  </code></pre>
  <p>The saved image appeas as:</p>
  <p><img src="https://i.stack.imgur.com/HQPRx.jpg" alt="enter image description here" /></p>
  <p>Any suggestion, where I am wrong or how to save it the way it appears or may be I need to save as .png. Can anyone let me know how to save as .png?</p>
  <p>Thanks</p>