Skip to content

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

Closed

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

Activity

  1. HeikoKlare commented on Sep 12, 2025

    @HeikoKlare
    Contributor

    Thank you for the report! I can reproduce the issue with the given snippet (just replacing the 50x50.png with any image I have at hand). We will have a look at this.

  2. added 2 commits that reference this issue on Sep 15, 2025
    e0927d3
    e696e5c
  3. added a commit that references this issue on Sep 18, 2025
    2b1cc49
  4. added 4 commits that reference this issue on Sep 24, 2025
    61f063d
    deb956d
    15a9920
    28d0e34
  5. added a commit that references this issue on Sep 25, 2025
    d892d79
  6. added a commit that references this issue on Sep 25, 2025
    d5273ee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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