I have a bitmap file, test3.bmp
, which I can view and edit with every image viewer I have tested with.
That said, I cannot read it into my Java application. If I edit the BMP in MS Paint, save it, undo the change, and save it (test3_resaved.bmp
), I have the same image, but with a different file size. The different file sizes do not concern me... what does, is that my application can read the re-saved file.
Could anyone enlighten me on why one image works with my code but the other does not?
Images files:
Here is a minimal test application:
package Test;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class Test extends JFrame {
private ImageIcon imageIcon;
public Test(String filename) throws IOException {
super();
BufferedImage image = javax.imageio.ImageIO.read(new File(filename));
imageIcon = new ImageIcon(image);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
repaint();
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
setSize(imageIcon.getIconWidth(), imageIcon.getIconHeight());
if (imageIcon != null)
g2d.drawImage(imageIcon.getImage(), 0, 0, this);
}
/**
* @param args
*/
public static void main(String[] args) {
try {
if (args.length > 0)
new Test(args[0]);
else
System.out.println("usage - specify image filename on command line");
}
catch (Throwable t) {
t.printStackTrace();
}
}
}