# How to create a Pixmap from an OrthographicCamera in LibGDX

I am trying to create a picture based on what one of my OrthographicCamera can see. LibGDX has a ScreenUtils class that has a getFrameBufferPixels method, but it does not what I want as I want to get only what one camera sees, not the whole screen.

You need to render what the camera sees to an FBO and then get the pixmap from the FBO itself.

// Construct an FBO and keep a reference to it. Remember to dispose of it.
final FrameBuffer fbo = new FrameBuffer(Format.RGB565, width, height, false);

//Start rendering to the fbo.
fbo.begin();
//From the camera's perspective.
batch.setProjectionMatrix(camera.combined);
batch.begin();

//Draw whatever you want to draw with the camera.

batch.end();
// Finished drawing, get pixmap.
Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(0, 0, width, height);
fbo.end();

//Now you can draw your normal screen as usual.


This will draw whatever you draw with your batch to your FBO from your camera's perspective. Remember to reset the projection matrix when you want to render from your main camera.

• What is the utility of ScreenUtils.getFrameBufferPixels(flipY); if I will get the Texture with fbo.getColorBufferTexture(); – Cedric Martens Jun 22 '17 at 18:14
• @CedricMartens Not sure what the exact reason for that is but one reason might be that that method doesn't require you to know about the frame buffer that's currently in use. Hence it can be used for other OpenGL stuff that require you to know of the pixels. – Charanor Jun 22 '17 at 19:11
• Yeah and I can't seem to get it into a Pixmap from since it gives me a GdxRuntimeException This TextureData implementation does not return a Pixmap and when I try by doing byte[] bytes = ScreenUtils.getFrameBufferPixels(false); Pixmap pixmap = new Pixmap(bytes, 0, bytes.length); I'll get another GdxRuntimeException saying Couldn't load pixmap from image data – Cedric Martens Jun 22 '17 at 19:45
• @CedricMartens That's because that constructor takes image data not pixel data i.e a png / jpg similar file. You actually have to do some slight hacking to get a pixmap from the pixels. I updated my answer with the solution (bottom of first code block). – Charanor Jun 22 '17 at 20:38
• @CedricMartens That might be the cause. Try changing the frame buffer to RGBA8888 instead. Editing the buffer should edit the pixmap since the buffer is the data of the pixmap. PixmapIO also uses this buffer to get the pixel data if you look at the source code. – Charanor Jun 22 '17 at 21:05