Zenyte
Zenyte

Reputation: 11

Save a BufferedImage to BMP/PNG/JPG and then open it back

I have made a project like Paint from windows and now I want to make the save/open buttons. I have found out how to save the bufferedImage, but the problem is how do I open it back in the correct location and be able to draw on it again?

Upvotes: 0

Views: 183

Answers (1)

To read in an image, use ImageIO.

File myPath = new FIle("path to image");
BUfferedImage img = ImageIO.read(myPath);

Also what you could (should) do is load the image into your user space so that you don't edit the original image:

public static BufferedImage userSpace(BufferedImage image)
{
    BufferedImage newImage  = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    Graphics2D    graphics   = newImage.createGraphics();
    graphics.drawRenderedImage(image, null);
    graphics.dispose(); 
    return newImage;
}

Upvotes: 2

Related Questions