Skip to content

Introduce simple API to take a screenshot #2104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.eclipse.swt;


import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.widgets.*;

Expand Down Expand Up @@ -5010,4 +5011,39 @@ public static void error (int code, Throwable throwable, String detail) {
MOD4 = 0;
}
}

/**
* Takes a screenshot of the given {@code display} and returns it as image.
*
* @param display the display to take a screenshot from
* @return the ImageData of the screenshot taken
* @since 3.130
*/
public static ImageData takeScreenShot(Display display) {
return takeScreenShot(display, display, display.getBounds());
}

/**
* Takes a screenshot of the given {@code control} and returns it as image.
*
* @param control the control to take a screenshot from
* @return the ImageData of the screenshot taken
* @since 3.130
*/
public static ImageData takeScreenShot(Control control) {
return takeScreenShot(control, control.getDisplay(), control.getBounds());
}

private static ImageData takeScreenShot(Drawable source, Display display, Rectangle bounds) {
Image image = new Image(display, bounds);
GC gc = new GC(source);
try {
gc.copyArea(image, 0, 0);
Copy link
Contributor

@ptziegler ptziegler May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the source is invisible, this will capture the area underneath the widget. This should then either return null or a blank image.

return image.getImageData();
} finally {
gc.dispose();
image.dispose();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public abstract class Control extends Widget implements Drawable {
long firstFixedHandle = 0;
long keyController;
long redrawWindow, enableWindow, provider;
//TODO: derive alpha from color?
int drawCount, backgroundAlpha = 255;
long dragGesture, zoomGesture, rotateGesture, panGesture;
Composite parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.graphics.Rectangle;
Expand Down Expand Up @@ -54,7 +53,7 @@ public static void main(String[] args) {

shell.open();

snapshot(display, composite, filename);
snapshot(composite, filename);

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
Expand All @@ -79,15 +78,11 @@ private static Composite canvas(Display display, Shell shell) {
return composite;
}

private static void snapshot(Display display, Composite composite, String filename) {
Rectangle bounds = composite.getBounds();
Image image = new Image(display, bounds);
GC gc = new GC(image);
composite.print(gc);
gc.dispose();
private static void snapshot(Composite composite, String filename) {
ImageData screenshot = SWT.takeScreenShot(composite);

ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] { image.getImageData() };
loader.data = new ImageData[] { screenshot };
File output = new File(filename);
output.delete();
loader.save(filename, SWT.IMAGE_PNG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.layout.FillLayout;
Expand Down Expand Up @@ -72,14 +70,10 @@ public void widgetSelected(SelectionEvent e) {
}

private static void saveImage(Control control, String filename, int format) {
Image image = new Image(control.getDisplay(), control.getBounds());
GC gc = new GC(image);
control.print(gc);
gc.dispose();
ImageData data = image.getImageData();
ImageData screenshot = SWT.takeScreenShot(control);
ImageData data = screenshot;
ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] { data };
loader.save(filename, format);
image.dispose();
}
}
Loading