Image img=new ImageIcon(String fileName).getImage();Images that are created from a URL or filename are preloaded using MediaTracker to monitor the loaded state of the image. so you can get the width and height of img, because it has been loaded.

解决方案 »

  1.   

    import javax.swing.*;
    public class JPGSize {
      private int intWidth;
      private int intHeight;
      public JPGSize(String strFileName) {
        ImageIcon imageA = new ImageIcon(strFileName);
        intWidth=imageA.getIconWidth();
        intHeight=imageA.getIconHeight();
      }
      /**
       Description: 取得图片的宽度。
       当返回值是"-1"时,表示图片格式错误或文件不存在
       */
      public int getWidth() {
        return intWidth;
      }
      /**
       Description: 取得图片的高度
       当返回值是"-1"时,表示图片格式错误或文件不存在
       */
      public int getHeight() {
        return intHeight;
      }  /*
      public static void main(String[] args) {
        JPGSize JPGSize1 = new JPGSize(args[args.length]);
      }
      */
    }