-1
    try
    {
        BufferedImage original = ImageIO.read(OriginalImage);
        BufferedImage resized = new BufferedImage(width, null, original.getType());
        Graphics2D g2 = resized.createGraphics();
        g2.drawImage(original, 0, 0, width, null, null);
        g2.dispose();
        ImageIO.write(resized, format, resizedImage);
    }

I want to increase image width only height automatic adjust by width pixel.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Yes .. i want to increase image width and height aspect ratio auto maintain by width. example:i have image 900X600 pixel. i just increase width 50px like (950 ) height will auto adjust 631. – Shaikh Ahmed Jun 23 '18 at 05:46
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 3) The above code has noting to do with Swing. Don't add irrelevant tags! 4) Don't add extra information in comments. Instead [edit] the question.5) *"height automatic adjust"* Why? It's a simple calculation. – Andrew Thompson Jun 23 '18 at 06:06
  • I find solution by manual calculate aspect ratio. – Shaikh Ahmed Jun 23 '18 at 11:57

1 Answers1

0

Use the method Image.getScaledInstance as it show in the example below

import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class ImageTest {

    private static final int WIDTH_INCREMENT = 100;
    public static void main(String[] args) throws Exception {
        BufferedImage image = ImageIO.read(new File("D:\\DDownloads\\Download.png"));
        int targetWidth = image.getWidth() + WIDTH_INCREMENT;
        // compute height using the aspect ratio
        int targetHeight = (int) ((double) targetWidth * image.getHeight()) / image.getWidth();
        Image target = image.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH);
        // also you can use -1 as new height
        // image.getScaledInstance(targetWidth, -1, Image.SCALE_SMOOTH);
        // in this case the method computes the correct width by himself
        BufferedImage toWrite = new BufferedImage(
            targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = toWrite.createGraphics();
        g2.drawImage(target, 0, 0, targetWidth, targetHeight, null);
        g2.dispose();
        ImageIO.write(toWrite, "png", new File("D:\\\\DDownloads\\\\converted.png"));
        // next code is to show the both original and converted image on the screen

        // Use invokeLater ofr correct initialization of Swing components
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frm = new JFrame("Images");
                frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frm.add(new JLabel(new ImageIcon(image)), BorderLayout.NORTH);
                frm.add(new JLabel(new ImageIcon(target)), BorderLayout.SOUTH);
                frm.pack();
                frm.setLocationRelativeTo(null); // center the window
                frm.setVisible(true);
            }
        });
    }
}
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
  • 1
    Note: From [`Image.getScaledInstance(w,h,hints)`](https://docs.oracle.com/javase/9/docs/api/java/awt/Image.html#getScaledInstance-int-int-int-) *".. If either width or height is a **negative number** then a value is substituted to maintain the aspect ratio of the original image dimensions."* ;) – Andrew Thompson Jun 23 '18 at 10:24
  • @AndrewThompson many thanks. But in this case I think is better to show the author, how to compute the target height using the aspect ratio. – Sergiy Medvynskyy Jun 23 '18 at 10:28