diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/SWT.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/SWT.java index db336185913..58ce4fd9e92 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/SWT.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/SWT.java @@ -15,6 +15,7 @@ package org.eclipse.swt; +import org.eclipse.swt.graphics.*; import org.eclipse.swt.internal.*; import org.eclipse.swt.widgets.*; @@ -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); + return image.getImageData(); + } finally { + gc.dispose(); + image.dispose(); + } +} + } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Control.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Control.java index b6e6d58d2d4..5c016f0a981 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Control.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Control.java @@ -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; diff --git a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug531667_CanvasPrint_does_not_work.java b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug531667_CanvasPrint_does_not_work.java index 97a6567e6fa..e0c9f51fa51 100644 --- a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug531667_CanvasPrint_does_not_work.java +++ b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug531667_CanvasPrint_does_not_work.java @@ -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; @@ -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()) { @@ -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); diff --git a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug547529_ImageLoaderStriping.java b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug547529_ImageLoaderStriping.java index d4fb5cc5284..dae88c690aa 100644 --- a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug547529_ImageLoaderStriping.java +++ b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug547529_ImageLoaderStriping.java @@ -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; @@ -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(); } }