Skip to content
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

[Edge] Browser with SWT.EDGE returns blank image when using print() #373

Open
Destrolaric opened this issue Sep 6, 2022 · 6 comments · May be fixed by #1591
Open

[Edge] Browser with SWT.EDGE returns blank image when using print() #373

Destrolaric opened this issue Sep 6, 2022 · 6 comments · May be fixed by #1591
Labels
edge Edge Browser Windows Happens on Windows OS

Comments

@Destrolaric
Copy link

Destrolaric commented Sep 6, 2022

Describe the bug
In our app, we have the functionality of printing images from an internal browser window. We previously used Internet Explorer as an internal browser but we changed it to Edge by default for Windows systems. Previously this functionality correctly created the image of browser composite, but it doesn't work for Edge.
To Reproduce

        browser = new Browser(composite, SWT.EDGE);
        //any URL
        browser.setUrl(file.toURI().toURL().toString())
        Image image = new Image(Display.getDefault(), browser.getBounds());
        GC gc = new GC(image);
        try {
           browser.print(gc);
        } finally {
            gc.dispose();
        }
        ImageTransfer imageTransfer = ImageTransfer.getInstance();
        Clipboard clipboard = new Clipboard(Display.getCurrent());
        clipboard.setContents(new Object[] {image.getImageData()}, new Transfer[]{imageTransfer});

Expected behavior
Correct image with browser content shown on it.

Environment:

  1. Select the platform(s) on which the behavior is seen:
    • Windows
  1. Additional OS info (e.g. OS version, Linux Desktop, etc)
    Windows 10, Edge installed
  2. JRE/JDK version
    java.vendor.version=Temurin-11.0.15+10
    java.version=11.0.15
  3. Version
    found on:
    org.eclipse.swt.browser.EdgeVersion=105.0.1343.27

Workaround (or) Additional context
Probably screenshots can be done via browser functionality. But I was not able to do that.

@Destrolaric Destrolaric changed the title Browser with SWT.EDGE returns blank image when using print() [Edge] Browser with SWT.EDGE returns blank image when using print() Sep 6, 2022
@lshanmug lshanmug added Windows Happens on Windows OS edge Edge Browser labels Sep 15, 2022
@lathapatil
Copy link
Contributor

it would be helpful if you provide complete standalone program to reproduce the issue

@codegoddesskh
Copy link

I've edited Snippet 292 (take a snapshot of a control) to add an IE browser (SWT.NONE) and an Edge browser (SWT.EDGE) to show the difference in control.print behavior. I just tested it on swt 4.28, Windows 11, Edge installed, Temurin-17.0.2+8 and the print control section for the Edge browser is a black box. Hope this helps!

/*
 * Take a snapshot of a control - edited to show Edge bug #373
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 *
 * @since 3.4
 */
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.browser.Browser;

public class Snippet292 {
	public static void main(String[] args) {
		final Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setText("Snippet 292");
		final Group group = new Group(shell, SWT.NONE);
		group.setText("Group");
		group.setLayout(new GridLayout());
		final Tree tree = new Tree(group, SWT.BORDER);
		for (int i = 0; i < 5; i++) {
			TreeItem treeItem = new TreeItem (tree, SWT.NONE);
			treeItem.setText ("TreeItem " + i);
			for (int j = 0; j < 3; j++) {
				TreeItem subItem = new TreeItem(treeItem, SWT.NONE);
				subItem.setText("SubItem " + i + "-" + j);
			}
			if (i % 3 == 0) treeItem.setExpanded (true);
		}

		/// Start code to add in two Browsers - SWT.EDGE and SWT.NONE
		Label ieLabel = new Label(group, SWT.LEFT);
		ieLabel.setText("IE:");
		
		final Browser ieBrowser = new Browser(group, SWT.NONE);
		ieBrowser.setUrl("https://www.eclipse.org/");
		ieBrowser.setSize(500,200);
		GridData gdIE = new GridData(SWT.FILL, SWT.FILL, true, true);
		ieBrowser.setLayoutData(gdIE);

		Label edgeLabel = new Label(group, SWT.LEFT);
		edgeLabel.setText("Edge:");
		
		final Browser edgeBrowser = new Browser(group, SWT.EDGE);
		edgeBrowser.setUrl("https://www.eclipse.org/");
		edgeBrowser.setSize(500,200);
		GridData gdED = new GridData(SWT.FILL, SWT.FILL, true, true);
		edgeBrowser.setLayoutData(gdED);
		/// End code to add in two Browsers - SWT.EDGE and SWT.NONE

		new Button(group, SWT.PUSH).setText("Button");
		final Label label = new Label (shell, SWT.NONE);
		label.addListener (SWT.Dispose, e -> {
			Image image = label.getImage ();
			if (image != null) image.dispose ();
		});
		Button button = new Button (shell, SWT.PUSH);
		button.setText ("Snapshot");
		button.addListener (SWT.Selection, e -> {
			Image image = label.getImage ();
			if (image != null) image.dispose ();
			image = new Image (display, group.getBounds ());
			GC gc = new GC (image);
			boolean success = group.print (gc);
			gc.dispose ();
			label.setImage (image);
			if (!success) {
				MessageBox messageBox = new MessageBox (shell, SWT.OK | SWT.PRIMARY_MODAL);
				messageBox.setMessage ("Sorry, taking a snapshot is not supported on your platform");
				messageBox.open ();
			}
		});
		GridLayout layout = new GridLayout (2, true);
		shell.setLayout(layout);
		group.setLayoutData (new GridData (SWT.FILL, SWT.FILL, true, true));
		label.setLayoutData (new GridData (SWT.FILL, SWT.FILL, true, true));
		button.setLayoutData (new GridData (SWT.FILL, SWT.FILL, true, true, 2, 1));
		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) display.sleep();
		}
		display.dispose();
	}
}

@deepika-u
Copy link
Contributor

@codegoddesskh
Thanks for the snippet, i am able to recreate the problem on windows 11 as the below image shows.
Upon clicking "snapshot" button
image

This is the environment i have tried on
Eclipse SDK
Version: 2023-09 (4.29)
Build id: I20230830-2200
OS: Windows 11, v.10.0, x86_64 / win32
Java vendor: Eclipse Adoptium
Java runtime version: 20+36
Java version: 20

@johnBradley501
Copy link

This is also a problem for me... In my case I've been using the following code

    Rectangle imageArea = browser.getClientArea();
GC gc = new GC(browser);
    final Image image = new Image(browser.getDisplay(), imageArea.width, imageArea.height);
    gc.copyArea(image, 0, 0);
    gc.dispose();

to capture the browser display, which my code then reduces in size to make a kind of thumbnail. This works properly with "browser = new Browser(shell, SWT.NONE);" but returns an empty, white, area with "browser = new Browser(shell, SWT.EDGE);".

I have some basic code (based on Snippet 128) which demonstrates the problem.

My application cannot use SWT.NONE since IE doesn't work properly displaying many of web sites to which the app is directed.

Any hints for a workaround?

@deepika-u
Copy link
Contributor

@johnBradley501
Can you please update your "complete modified snippet 128" here so that it would be easy for anyone to check further? because in the earlier snippet the issue is with print (gc).

@johnBradley501
Copy link

Here is my modified Snippet 128. switch "//" comment between line 65 and 66 to see what happens with SWT.EDGE and SWT.NONE. With NONE the screen is successfully captured and stored to a file, with EDGE it is not. To run this, you'll need to fix the file path in line 141.

package org.eclipse.swt.snippets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.LocationListener;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
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.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class Snippet128JB {

private static Shell shell= null;

public static void main(String [] args) {
	Display display = new Display();
	shell = new Shell(display);
	shell.setText("Snippet 128");
	GridLayout gridLayout = new GridLayout();
	gridLayout.numColumns = 3;
	shell.setLayout(gridLayout);
	ToolBar toolbar = new ToolBar(shell, SWT.NONE);
	ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH);
	itemBack.setText("Back");
	ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH);
	itemForward.setText("Forward");
	ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH);
	itemStop.setText("Stop");
	ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH);
	itemRefresh.setText("Refresh");
	ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH);
	itemGo.setText("Go");

	GridData data = new GridData();
	data.horizontalSpan = 3;
	toolbar.setLayoutData(data);

	Label labelAddress = new Label(shell, SWT.NONE);
	labelAddress.setText("Address");

	final Text location = new Text(shell, SWT.BORDER);
	data = new GridData();
	data.horizontalAlignment = GridData.FILL;
	data.horizontalSpan = 2;
	data.grabExcessHorizontalSpace = true;
	location.setLayoutData(data);

	final Browser browser;
	try {
		// browser = new Browser(shell, SWT.NONE);
		browser = new Browser(shell, SWT.EDGE);
	} catch (SWTError e) {
		System.out.println("Could not instantiate Browser: " + e.getMessage());
		display.dispose();
		return;
	}
	data = new GridData();
	data.horizontalAlignment = GridData.FILL;
	data.verticalAlignment = GridData.FILL;
	data.horizontalSpan = 3;
	data.grabExcessHorizontalSpace = true;
	data.grabExcessVerticalSpace = true;
	browser.setLayoutData(data);

	final Label status = new Label(shell, SWT.NONE);
	data = new GridData(GridData.FILL_HORIZONTAL);
	data.horizontalSpan = 2;
	status.setLayoutData(data);

	final ProgressBar progressBar = new ProgressBar(shell, SWT.NONE);
	data = new GridData();
	data.horizontalAlignment = GridData.END;
	progressBar.setLayoutData(data);

	/* event handling */
	Listener listener = event -> {
		ToolItem item = (ToolItem) event.widget;
		String string = item.getText();
		if (string.equals("Back"))
			browser.back();
		else if (string.equals("Forward"))
			browser.forward();
		else if (string.equals("Stop"))
			browser.stop();
		else if (string.equals("Refresh"))
			browser.refresh();
		else if (string.equals("Go"))
			browser.setUrl(location.getText());
	};
	browser.addProgressListener(new ProgressListener() {
		@Override
		public void changed(ProgressEvent event) {
				if (event.total == 0) return;
				int ratio = event.current * 100 / event.total;
				progressBar.setSelection(ratio);
		}
		@Override
		public void completed(ProgressEvent event) {
			progressBar.setSelection(0);
			captureScreen(browser);
		}
	});
	browser.addStatusTextListener(event -> status.setText(event.text));
	browser.addLocationListener(LocationListener.changedAdapter(event -> {
			if (event.top) location.setText(event.location);
		}
	));
	itemBack.addListener(SWT.Selection, listener);
	itemForward.addListener(SWT.Selection, listener);
	itemStop.addListener(SWT.Selection, listener);
	itemRefresh.addListener(SWT.Selection, listener);
	itemGo.addListener(SWT.Selection, listener);
	location.addListener(SWT.DefaultSelection, e -> browser.setUrl(location.getText()));

	shell.open();
	browser.setUrl("http://eclipse.org");

	while (!shell.isDisposed()) {
		if (!display.readAndDispatch())
			display.sleep();
	}
	display.dispose();
}

public static void captureScreen(Browser browser) {
	String filename = "d:\\temp\\browser.jpg";
	Rectangle imageArea = browser.getClientArea();
        GC gc = new GC(browser);
            final Image image = new Image(browser.getDisplay(), imageArea.width, imageArea.height);
            gc.copyArea(image, 0, 0);
            gc.dispose();
    
            ImageData data = image.getImageData();
            ImageLoader loader = new ImageLoader();
            loader.data = new ImageData[]{data};
            loader.save(filename, SWT.IMAGE_JPEG);
            image.dispose();
     }

}

sratz added a commit to sratz/eclipse.platform.swt that referenced this issue Nov 12, 2024
sratz added a commit to sratz/eclipse.platform.swt that referenced this issue Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
edge Edge Browser Windows Happens on Windows OS
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants