Here's some example code that won't work if the snapshot is taken while the screen is locked (and it was locked after application start) in both Java 7 and 8.
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class SnapshotTester extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
final BorderPane borderPane = new BorderPane();
final Rectangle rectangle = new Rectangle(20, 20, Color.BLUE);
borderPane.setCenter(rectangle);
Scene scene = new Scene(borderPane);
primaryStage.setScene(scene);
primaryStage.show();
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
Platform.runLater(new Runnable() {
@Override
public void run() {
Image snapshot = rectangle.snapshot(null, null);
System.out.println("Snapshot taken: " + snapshot);
}
});
} catch (InterruptedException ignored) {
}
}
}).start();
}
public static void main(String[] args) {
launch(args);
}
}
Steps to reproduce:
1) Start application
2) Lock screen as soon as window is shown
3) Unlock after two seconds
You'll get the following exception which is unfortunately not very expressive:
java.lang.IllegalArgumentException: Unrecognized image loader: null
at javafx.scene.image.WritableImage.loadTkImage(WritableImage.java:230)
at javafx.scene.image.WritableImage.access$000(WritableImage.java:44)
at javafx.scene.image.WritableImage$1.loadTkImage(WritableImage.java:49)
at javafx.scene.Scene.doSnapshot(Scene.java:1144)
at javafx.scene.Node.doSnapshot(Node.java:1632)
at javafx.scene.Node.snapshot(Node.java:1710)
Might be related to RT-30362