Skip to content

FileImageDataProvider will throw IllegalArgumentException on Windows 200% scale with SVG image #938

@Phillipus

Description

@Phillipus

With a display set to 200% scaling on Windows calling GC.drawImage(Image image, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight) when using a FileImageDataProvider can cause an IllegalArgumentException.

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class FileImageDataProviderIllegalArgumentException {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setSize(800, 600);

        Image image = FileImageDataProvider.createImage(FileImageDataProviderIllegalArgumentException.class, "GEF.svg");
        int width = image.getBounds().width;
        int height = image.getBounds().height;
        
        shell.addPaintListener(event -> {
            GC gc = event.gc;
            gc.drawImage(image, 0, 0, width, height, 0, 0, width, height);
        });

        shell.open();

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

Use this SVG image with the Snippet:

Image

See eclipse-platform/eclipse.platform.swt#2916

FileImageDataProvider.getImageData() should really return null for zoom 200 and FileImageDataProvider.createImageData() should check for null InputStream:

	@Override
	public ImageData getImageData(int zoom) {
		if (zoom == 100) { //$NON-NLS-1$
			return createImageData(basePath + '.' + fileExtension);
		}
		if (zoom == 200) {
			return createImageData(basePath + "@2x." + fileExtension); //$NON-NLS-1$
		}
		return null;
	}

    private ImageData createImageData(String name) {
        try (InputStream stream = clazz.getResourceAsStream(name)) {
            return stream != null ? new ImageData(stream) : null;
        } catch (IOException ioe) {
            return null;
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions