Editing images

Hey JGO,

How exactly could i create an image of a set width and height and draw to it programmatically, pixel by pixel?

Thanks

You can use the tag .
http://url of your image

I think he’s asking about java…
Look into rasters
Some code examples of image manipulation with/without rasters here

Hi kingroka,

Appears to be a few ways, some which may be more correct than my method. But I usually create a new image like so:


BufferedImage image;

image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

Then if you wanted to use the Java2d Graphics & Graphics2D classes to draw onto it, you can just grab the image’s graphics object like this:


Graphics g;

g = image.getGraphics();
//do some drawing here with g etc.
g.dispose();

To manipulate it ‘pixel by pixel’ you have a couple of options. One of the simplest is using BufferedImage.getRGB(int x, int y) and BufferedImage.setRGB(int x, int y, int RGB).

Alternatively you can grab the image’s ‘data buffer’ directly as an array. This can be a better approach performance wise, as you don’t have the overhead of the get/setRGB() methods. One way to do this is:


int[] imgArray;

imgArray = ((DataBufferInt)image.getRaster().getDataBuffer()).getData()
//Now you can directly mess with the integer RGB values in imgArray[].

Rightly or wrongly, that’s generally how I do it…

Ah, duh! :smiley: I don’t know why I was thinking of images in posts.