tags:

views:

17

answers:

1

I have a Color[][] Array which holds the information of an image. Array[0][0] is the lowest leftmost pixel. I can draw the picture on the screen. I also want to save it as file (png/jpg). But how?

A: 

Are you drawing the array via the Graphics context of the Window? If yes, you can just change the graphic context to a newly created BufferedImage and save this later on via ImageIO.write();

Example:

//Load image 1
BufferedImage img1 = ImageIO.read(new File("someImage.png"));
//Create a second image
BufferedImage img 2 = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
//Create a new file
File f = new File("someImage2.png");
//Get 2D Graphics object from the newly created image
Graphics2D g = img2.createGraphics();
//Draw img1 into img2
g.drawImage(img1, 0, 0, 16, 16, null);
//Save the image
ImageIO.write(img2, "png", f);
yan.kun