Skip to content

On win32, if GC drawable is not autoscalable GC.drawImage can cut of parts of the image #2504

@Tarmean

Description

@Tarmean

Describe the bug

GC.drawImage scales the image data but not the image source coordinates if the gc is not autoScalable.

  • Use a GC with gc.drawable.isAutoScalable()==false
  • Call gc.drawImage(image, 0, 0, 50, 50, 0, 0, 100, 100);

For this, DrawScalingImageToImageOperation.calculateZoomForImage gives a zoom of 200.

Because the gc drawable is not autoScalable, the image source coordinates are not scaled by 200

Rectangle src = Win32DPIUtils.pointToPixel(drawable, new Rectangle(srcX, srcY, srcWidth, srcHeight), scaledImageZoom);

Because the image is scaled independent of isAutoScalable, the coordinates get out of sync

long [] gdipImage = srcImage.createGdipImage(imageZoom);

To Reproduce
A standalone SWT snippet to reproduce the behavior (preferable)

package test.package;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ReproDemo {
	static final float targetScale = 2f;
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);

		Image image = new Image(display, ReproDemo.class.getResourceAsStream("/50x50.png"));
		Rectangle bounds = image.getBounds();

		shell.setLayout(new FormLayout());
		Canvas canvas = new Canvas(shell, 0) {
			@Override
			public boolean isAutoScalable() {
				return false;
			}
		};
		FormData formData = new FormData();
		formData.left = new FormAttachment(0,0);
		formData.top = new FormAttachment(0,0);
		formData.width = Math.round(bounds.width * targetScale);
		formData.height = Math.round(bounds.height * targetScale);
		canvas.setLayoutData(formData);

		canvas.addPaintListener(e -> {
			e.gc.drawImage(image, 0, 0, bounds.width, bounds.height, 0, 0, formData.width, formData.height);
		});

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

		image.dispose();
		display.dispose();
	}
}

Expected behavior
The given source coordinates and the source image are scaled in the same way

Screenshots
If applicable, add screenshots to help explain your problem.

With source and destination at 1:1:
Image

With source and destination at 1:2:
Image

Environment:

  1. Select the platform(s) on which the behavior is seen:
    • All OS
    • Windows
    • Linux
    • macOS
  1. Additional OS info (e.g. OS version, Linux Desktop, etc)
    Checked on Win10 and Win11

  2. JRE/JDK version
    Azul Zulu 21.0.3

Version since
4.37RC2

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