Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save SWT Image to File with Transparency Mask

Tags:

java

swt

This is my code thus far

ImageLoader saver = new ImageLoader();
saver.data = new ImageData[]
    { toSave.getImageData() };
saver.save(fileName, SWT.IMAGE_PNG);

toSave is an image that was loaded using the SWTResourceManager that is transparent in the program. fileName is a string representing the file I want to save the image to (ends with .png)

The result is an image with a black background instead of a transparent background that I want. How do I make the background transparent? I think it has something to do with the transparency mask, but I could be wrong.

Thanks in advance!

like image 362
connory3 Avatar asked Sep 18 '25 09:09

connory3


1 Answers

SWTResourceManager seems to be causing your problem. I would not recommend to use it.

Try this code, it works for me:

Display d = Display.getDefault();
Image image = new Image(d, "/pictures/Llama.gif");

ImageLoader saver = new ImageLoader();
saver.data = new ImageData[] { image.getImageData() };
saver.save("output.png", SWT.IMAGE_PNG);

image.dispose();

Remember to dispose the Image when you don't need it anymore.

like image 68
Baz Avatar answered Sep 19 '25 22:09

Baz