Skip to main content

Capturing images from a buffered system...

5 replies [Last post]
Joined: 2003-06-10

(Woohoo! First post on the forum!)

It seemed easy enough...all I had to do was grab what was on the front buffer and save it to a file right? Right?

I'm currently using the Fullscreen API and I thought it would be cool to add in a way for players to take a snapshot. Problem is, my only hook to the buffers is the back buffer by taking a Graphics object from BufferStrategy.getDrawGraphics();

anyone know of a simple way to create an Image object off of a Graphics object, or is Graphics a one way street headed in the wrong direction? It seems there's gotta be a way to replicate what the Print Screen button is doing but send the output to an Image object rather than the OS clipboard...


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2006-02-17

This would be very nice !

Then we could build a VNC-server of every swing-application and
run it on a wireless PDA !


Joined: 2003-06-11

Unfortunately a Graphics (or Graphics2D) object is one-way. You cannot extract the image of what it points to.

You would5{dle to the image being used as the back buffer in order to copy it somewhere, but unfortunately there is no API for that.

The best way to handle this would be make your own BufferedImage, get a graphics from it and ask your rendering engine to redraw the current frame to that Graphics. You could then save or print that BufferedImage to get a "snapshot" of the screen.

BufferedImage bimg =
new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bimg.createGraphics();
// snapshot of frame is now in bimg

Joined: 2003-06-10

Ahh...that's what I thought about the one way Graphics object...

an interesting idea, Flar, instead of referencing my backbuffer have it reference the BufferedImage instead...let's see if I can find a place low enough in my API to do that...


Joined: 2003-08-07

Just a quick note, as much a reminder to myself as a note to readers: I think the lack of a handle to the back buffer image is a hole in the current API that should be fixed. Most of the time, we just want the graphics context so that we can draw into it, but sometimes we may want a handle to the image itself. This is on my list of updates to the BufferStrategy APIs....

Chet Haase

Joined: 2003-06-10

That would be simply wonderful, Chet! ^_^