setRGB doesn't work. I have blank image
hello,
I can't understand why setRGB is not working. I tried to create an image with only green pixels to test it. But it create only an empty image... why ?
BufferedImage img = new BufferedImage(Width,Height, BufferedImage.TYPE_INT_ARGB );
for (int x=0; x<Width; x++) {
for (int y=0; y><Height; y++) {
img.setRGB(x,y,0xFF00FF00);
}
}
//write the image to filesystem
ImageIO.write(img,"BMP",new File(pathname+nameFile));>
[496 byte] By [
aneuryzmaa] at [2007-11-27 5:53:10]

# 1
Use [url http://forum.java.sun.com/help.jspa?sec=formatting]code formatting[/url] when posting code.
setRGB is working just fine.
The problem is ImageIO only support 1 bit BMP images.
So try using PNG instead:ImageIO.write(img,"PNG",new File(pathname+nameFile));
# 2
Hi, I tried PNG format, but it is the same...
ImageIO.write(img,"PNG",new File(pathname+nameFile));
I have a blank image (0 bytes). It just create an empty file.
ps. Before i tried to create an IndexColorModel, and it was all ok. I have problems with setRGB method...
Message was edited by:
aneuryzma
# 3
Here's a simple example that displays the image and writes it as PNG:
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;
public class BufferedImageTest {
public static void main(String[] args) {
try {
JFrame frame = new JFrame();
BufferedImage image = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < 300; x++)
for (int y = 0; y < 300; y++)
image.setRGB(x, y, 0xFF00FF00);
ImageIO.write(image,"PNG",new File("c:/test.png"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JLabel(new ImageIcon(image)));
frame.pack();
frame.setVisible(true);
} catch (Exception e) {e.printStackTrace();}
}
}
# 4
I have the same problem when I try to import an image from filesystem with getRGB in another class.I can get only a blank image, or better, all pixels are ox000000 in any case...someone can help me ?
# 5
Yes, it is exactly like my class, with the exception that I don't use frames
# 6
It works fine for me.I get a green image inside the frame, and a green test.png file.
# 7
ok, now it works. Anyway I need to save/load BMP images to/from filesystem.ImageIO only manages png and jpeg.Which is the simpler way to manage BMP? Can I still use BufferedImage ?thanksbye
# 8
I don't think BMP supports alpha channel.
So just create the image with type TYPE_INT_RGB
BufferedImage image = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < 300; x++)
for (int y = 0; y < 300; y++)
image.setRGB(x, y, 0xFF00FF00);
ImageIO.write(image,"BMP",new File("c:/test.bmp"));