PDA

View Full Version : Screen Capture methods



ampersander7
03-26-2013, 01:07 PM
I'm currently using a simple method for screen capture that works fine for Windows XP, and not at all for Windows 7. I create a Bitmap and a Canvas, set the canvas handle equal to the device context handle, then run Bitmap->Canvas->CopyRect. Works for XP. Not W7. Looking around, it sounds like glReadPixels could be an alternative screen capture method, but so far I'm pretty confused about it, and I've come back to wondering why I can't get my original method to work.

Alphaomega86
03-30-2013, 07:23 AM
I have written a screen capture method in LWJGL. It uses glReadPixels too:

The following code reads all current displayed pixels and stores them in a ByteBuffer object.
In the for loop, i convert the r, g, b and a values to a single integer value, and store them in a BufferedImage object.



/**
* Makes a simple screenshot.
*
* @return Screenshot.
*/
public static BufferedImage glScreenshot() {
glReadBuffer(GL_FRONT);
int w = Display.getDisplayMode().getWidth();
int h = Display.getDisplayMode().getHeight();
int bpp = 4;
ByteBuffer buffer = BufferUtils.createByteBuffer(w * h * bpp);

glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
int i = (x + (w * y)) * bpp;
int r = buffer.get(i) & 0xFF;
int g = buffer.get(i + 1) & 0xFF;
int b = buffer.get(i + 2) & 0xFF;
image.setRGB(x, h - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
}
}

return image;
}