Advertisement Jump to content
Sign in to follow this  

[java] int array of RGB values saving to an image?!

This topic is 4975 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Howdy ya'll! I've got an array of int RGB values that I'm trying to save to an image! The resulting image's size is right, but it's just BLACK! The array with the pixel data isn't a bunch of 0's, so I think the problem is in the saving part! Here's the code:
			BufferedImage buffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
			buffer.setRGB(0, 0, w, h, pix, 0, w);
			File f = new File(src1+".png");
				ImageIO.write(buffer, "png", f);
			catch(Exception e)

(pix is my int array, w the width, h the height) Any help would be greatly appreciated! *edit: I'm not very good at Java, so please be gentle when you point out my problems :( Thanks! SirGorthon

Share this post

Link to post
Share on other sites
Wild guess, but what does the data in your array look like? Are you storing each RGB (0-255) value in their own integer field, or have you combined them? (as in storing three 0-255 values per (assuming)4-byte int) I don't think I've used ImageIO before so I'm not sure which would be correct. It would be wasteful to use an entire int to store one 0-255 value, so I imagine they'd have chosen to use a char[] if they wanted one 0-255 value per array element?

I guess basically... are you sure the BufferedImage is okay?

Also, have you tried using this yet?

void setRGB(int x, int y, int rgb)
Sets a pixel in this BufferedImage to the specified RGB value.

Can you set a few pixels to some color and write the file then? If you need to use something like 00FFFFFF (hex) as your pixel value for white (and looking at that method I imagine you do), then you probably need to combine your RGB values like I tried to explain above.

Standard disclaimer: "Than again I'm posting this after having stayed up all night, so I could be way off."

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!