title: Resize an image using Java
body: <p>I have a JPEG Master Image which is of a dimension and an input Jpeg image of another dimension . So , I have written the following code to convert an image from one dimension to the other dimension.
  This code works fine when the input images are of dimensions around 900 * 700 and the master image of dimension 1200 * 800 .
  It does not work when the master/ input images are around dimensions like 3400*1900 and 4400*2400.
  Can anyone tell me where I went wrong! </p>
  
  <pre><code>public void checkSizeandResize(String file1, String file2){
  
     BufferedImage scaled = null;
     BufferedImage image1 = null;
     BufferedImage image2 = null;
     Graphics2D graphics2d = null;
     try{
      image1 = imageToBufferedImage(loadJPG(file1));
      image2 = imageToBufferedImage(loadJPG(file2));
      if(image1.getHeight()!=image2.getHeight() || image1.getWidth()!=image2.getWidth()){
  
              scaled = new BufferedImage(image2.getWidth(),image2.getHeight(), image2.getType());
              graphics2d =  scaled.createGraphics();
              graphics2d.setComposite(AlphaComposite.Src);
              graphics2d.drawImage(image1, 0, 0,null);
              graphics2d.dispose();
              ImageIO.write(image1, extension,new File(file2));
  
          }
      }
      catch(Exception exception)
      {
          exception.printStackTrace();
      }
      finally
      {
          scaled = null;
          image1 = null;
          image2 = null;
          graphics2d = null;
      }
  }
  </code></pre>