Skip to content

Add option to create transparent Image with alpha channel when using ImageGcDrawer [Multi-Monitor][HiDPI] #2046

Closed

Description

@wahlbrink

Problem
When creating an image using the new ImageGcDrawer, the image is currently initialized as opaque white image (at least on Windows) and it is not possible to create an image with alpha transparency.
Or have I overlooked something?

Request
This requests a possibility to create an image using an ImageGcDrawer comparable to this classic code:

var imageData = new ImageData(width, height, 24, new PaletteData(0xff0000, 0x00ff00, 0x0000ff));
imageData.setAlpha(0, 0, 0);
var image = new Image(display, imageData);
GC gc = new GC(image);

Additional context
This is e.g. required for a multi-monitor/HiDPI compatible solution of issue #501 (draw a mark over an existing icon while preserving its transparency).

Activity

  1. HeikoKlare commented on Apr 24, 2025

    @HeikoKlare
    Contributor

    Did you already see the TransparencyColorImageGcDrawer? https://github.com/eclipse-platform/eclipse.platform.swt/blob/master/bundles/org.eclipse.swt/Eclipse%20SWT/common/org/eclipse/swt/internal/TransparencyColorImageGcDrawer.java

    That one allows to define a transparency color, so you could initialize the image with that exact color to make everything transparent that is not explicitly drawn.

    Otherwise, do you have a complete snippet what you would like to do? If I am not mistaken, the line imageData.setAlpha(0, 0, 0); will only set an alpha value for pixel (0,0), so I have to admit that I do not understand how that is supposed to work.

  2. wahlbrink commented on Apr 24, 2025

    @wahlbrink
    ContributorAuthor

    Yes, I know the TransparencyColorImageGcDrawer, IMHO this is only a limited workaround, not a solution.

    If I am not mistaken, the line imageData.setAlpha(0, 0, 0); will only set an alpha value for pixel (0,0), so I have to admit that I do not understand how that is supposed to work.

    No, calling imageData.setAlpha the first time initializes the alpha channel with 0 for all pixels.

  3. HeikoKlare commented on Apr 24, 2025

    @HeikoKlare
    Contributor

    No, calling imageData.setAlpha the first time initializes the alpha channel with 0 for all pixels.

    Okay, I see, thanks for the clarification. It's kind of hack by relying on the implicit initialization of the created array with zeros.

    What if you just call gc.setAlpha(0) as the first operation of the drawer? Doesn't that work either?

  4. wahlbrink commented on Apr 27, 2025

    @wahlbrink
    ContributorAuthor

    What if you just call gc.setAlpha(0) as the first operation of the drawer?

    The function does what it should do, it sets the transparency for the drawing operations executed afterwards. But a call of setAlpha does not change the current image.

  5. HeikoKlare commented on May 5, 2025

    @HeikoKlare
    Contributor

    @wahlbrink can you please check whether using GC style SWT.Transparent in the ImageGcDrawer solves the issue for you, as introduced with #2073?

    ImageGcDrawer drawer =  new ImageGcDrawer() {
    	@Override
    	public void drawOn(GC gc, int imageWidth, int imageHeight) {
    		...
    	};
    
    	@Override
    	public int getGcStyle() {
    		return SWT.TRANSPARENT;
    	}
    };
  6. wahlbrink commented on May 10, 2025

    @wahlbrink
    ContributorAuthor

    Yes, the new option works.

  7. HeikoKlare commented on May 10, 2025

    @HeikoKlare
    Contributor

    Great, thank you for the feedback! Thus closing this a resolved.

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