How to make a simple screenshot method using LWJGL?


So basically I was messing about with LWJGL for a while now, and I came to a sudden stop with with annoyances surrounding glReadPixels().<br /> And why it will only read from left-bottom -> top-right.<br /> So I am here to answer my own question since I figured all this stuff out,<br /> And I am hoping my discoveries might be of some use to someone else.<br />

As a side-note I am using:

glOrtho(0, WIDTH, 0 , HEIGHT, 1, -1);


So here it is my screen-capture code which can be implemented in any LWJGL application C:

//=========================getScreenImage==================================// private void screenShot(){ //Creating an rbg array of total pixels int[] pixels = new int[WIDTH * HEIGHT]; int bindex; // allocate space for RBG pixels ByteBuffer fb = ByteBuffer.allocateDirect(WIDTH * HEIGHT * 3); // grab a copy of the current frame contents as RGB glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, fb); BufferedImage imageIn = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB); // convert RGB data in ByteBuffer to integer array for (int i=0; i < pixels.length; i++) { bindex = i * 3; pixels[i] = ((fb.get(bindex) << 16)) + ((fb.get(bindex+1) << 8)) + ((fb.get(bindex+2) << 0)); } //Allocate colored pixel to buffered Image imageIn.setRGB(0, 0, WIDTH, HEIGHT, pixels, 0 , WIDTH); //Creating the transformation direction (horizontal) AffineTransform at = AffineTransform.getScaleInstance(1, -1); at.translate(0, -imageIn.getHeight(null)); //Applying transformation AffineTransformOp opRotated = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR); BufferedImage imageOut = opRotated.filter(imageIn, null); try {//Try to screate image, else show exception. ImageIO.write(imageOut, format , fileLoc); } catch (Exception e) { System.out.println("ScreenShot() exception: " +e); } }

I hope this has been useful.<br /> For any questions or comments on the code, ask/suggest as you like. C:<br /> Hugs,<br /> Rose.


sorry for the late reply but this is for anybody still looking for a solution.

public static void saveScreenshot() throws Exception { System.out.println("Saving screenshot!"); Rectangle screenRect = new Rectangle(Display.getX(), Display.getY(), Display.getWidth(), Display.getHeight()); BufferedImage capture = new Robot().createScreenCapture(screenRect); ImageIO.write(capture, "png", new File("doc/saved/screenshot.png")); }


  • BufferedImage causes a program freeze on MacOs but not on Windows
  • Is there any short cuts for Xcode to tidy up the code?
  • JAVA NIO Bytebuffer.allocateDirect() size limit over the int
  • Load a bitmap from file in RGB format (without alpha)
  • Draw an Image based texture in openGl android(Wear)
  • Java how to parse a double value from received UDP bytes?
  • Android MediaCodec How to Frame Accurately Trim Audio
  • An equivalent of javax.nio.Buffer.flip() in c#
  • How to display a scaled down GL ES texture without clamp or repeat on android
  • Xcode 4 - Is there something in Interface Builder like “reveal in canvas”?
  • Prevent session from being replicated when JSESSIONID cookie copied
  • Define height of select box
  • JVM Monitor char array memory usage
  • How should I organize my c# solution?
  • JPanel error - J Component cannot be resolved
  • How can I pass the index of a for loop as the argument for pthread_create
  • How to logout from facebook connect in asp.net?
  • “'1.0.0' is not a valid short file name” when installing Xamarin IOS.Setup and Android.Set
  • Fortran array automatically growing when adding a value
  • How to show different validation messages for email validation in Angular2 using Validators class?
  • How to remove a list of accidently deleted databases from Sql Server Management Studio's tree-l
  • How do I use SFSafariViewController with a dark theme like this user?
  • How to Hide Navigation bar back button title globally in xamarin.ios
  • CHAR vs VARCHAR for password security
  • Xcode duplicated devices bug
  • QTP UFT Unable to find row count from table
  • Netlink sockets and libnl - nl_recvmsgs_default returning -16 (EBUSY)
  • Adding directive inside the directive programatically
  • npm 5.4.1 install/uninstall all failing
  • Django model inheritance, filtering models
  • Google Custom Search with transparent background
  • Control modification in presentation layer
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Splitting given String into two variables - php
  • Fetching methods from BroadcastReceiver to update UI
  • 0x202A in filename: Why?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How does Linux kernel interrupt the application?
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal