Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Screen Capture methods

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2012
    Posts
    8

    Screen Capture methods

    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.

  2. #2
    Intern Newbie
    Join Date
    Mar 2013
    Posts
    44
    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.

    Code :
    /**
    * 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;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •