How to keep the EMF background as transparent

Currently, we have EMF file with background as Transparent, but when we conver the EMF to PNG or EMF to PDF, the background changed to solid, how could we keep the transparent background as the original?

Please refer to below test EMF image.
test6.zip (548 Bytes)

@tldyzem

Can you please share the source code along with generated output with us.

test.zip (59.3 KB)
import com.aspose.imaging.Image;
import com.aspose.imaging.fileformats.emf.EmfImage;
import com.aspose.imaging.imageoptions.PngOptions;
PngOptions pngOpts = new PngOptions();
EmfImage image = (EmfImage)Image.load(emfPath);
image.save(pngPath, pngOpts);

@tldyzem

I have created a ticket with ID IMAGINGJAVA-7775 in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be notified once the issue will be fixed.

@tldyzem

Can you please try using following sample code where we have set transparent settings:

string basePath = @"D:\";
string inputFilePath = Path.Combine(basePath, "test6.emf");
using (Image image = Image.Load(inputFilePath))
{
     image.Save(inputFilePath + ".png", new PngOptions()
     {
        ColorType = PngColorType.TruecolorWithAlpha, 
        VectorRasterizationOptions = new EmfRasterizationOptions()
        {
            BackgroundColor = Color.Transparent, 
            PageSize = image.Size
        }
     });
}

test6.zip (548 Bytes)
I use below code, but conversion error

EmfImage image = (EmfImage)Image.load(emfPath);
EmfRasterizationOptions VectorRasterizationOptions = new EmfRasterizationOptions();
VectorRasterizationOptions.setBackgroundColor(Color.getTransparent());
// VectorRasterizationOptions.setPageSize(image.Size());
VectorRasterizationOptions.setPageWidth(image.getWidth());
VectorRasterizationOptions.setPageHeight(image.getHeight());
PngOptions pngOpts = new PngOptions();
pngOpts.setColorType(PngColorType.TruecolorWithAlpha);
pngOpts.setVectorRasterizationOptions(VectorRasterizationOptions);
image.save(pngPath, pngOpts);

error is:

Aspose error: class com.aspose.imaging.coreexceptions.ImageLoadException: Image saving failed. —> java.lang.ExceptionInInitializerError
— End of inner exception stack trace —
com.aspose.imaging.Image.save(Unknown Source)
com.aspose.imaging.Image.save(Unknown Source)

Use below code:

EmfImage image = (EmfImage)Image.load(emfPath);
EmfRasterizationOptions VectorRasterizationOptions = new EmfRasterizationOptions();
VectorRasterizationOptions.setBackgroundColor(Color.getTransparent());
VectorRasterizationOptions.setPageSize(image.Size());
// VectorRasterizationOptions.setPageWidth(image.getWidth());
// VectorRasterizationOptions.setPageHeight(image.getHeight());
PngOptions pngOpts = new PngOptions();
pngOpts.setColorType(PngColorType.TruecolorWithAlpha);
pngOpts.setVectorRasterizationOptions(VectorRasterizationOptions);
image.save(pngPath, pngOpts);

compile error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Aspose.Imaging.Test: Compilation failure
[ERROR] /mnt/d/src/Aspose.Imaging.Test/src/main/java/com/guoyp/App.java:[52,57] cannot find symbol
[ERROR] symbol: method Size()
[ERROR] location: variable image of type com.aspose.imaging.fileformats.emf.EmfImage
[ERROR]
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]

could you provide sample code in java, so I can try it directly? Thankstest6.zip (548 Bytes)

@tldyzem

I have tried using following sample code and it worked well on my end. The generated PNG is also attached.

 public static void testEmfTransparentIssue()
 {
        String basePath = "//Users//mudassirkhan//Downloads//";
        String inputFilePath = basePath+ "test6.emf";
        Image image = Image.load(inputFilePath);
        
        PngOptions options=new PngOptions();
        options.setColorType(PngColorType.TruecolorWithAlpha); 
        
        EmfRasterizationOptions emfraster=new EmfRasterizationOptions();
        emfraster.setBackgroundColor(Color.getTransparent()); 
        SizeF size=new SizeF(image.getSize().getWidth(),image.getSize().getHeight());
        emfraster.setPageSize(size);

       options.setVectorRasterizationOptions(emfraster);
       
        image.save(inputFilePath + ".png", options);
    
        
 }

test6.emf.png (1.7 KB)

Thanks for reply.
I tried again, and it works on win10, but failed on the ubuntu 18.04 LTS using same code.
It’s ubuntu 18.04 LTS installed on win10 platform.

error as below:

Aspose error: class com.aspose.imaging.coreexceptions.ImageLoadException: Image saving failed. —> java.lang.ExceptionInInitializerError
— End of inner exception stack trace —
com.aspose.imaging.Image.save(Unknown Source)
com.aspose.imaging.Image.save(Unknown Source)

@tldyzem

Can you please also share the Java details with us too.

Tow problems:

  1. It works well on win10, but not on ubuntu 18.04 LTS
  2. For tranparent settings, if we setBackgroundColor to transparent, all picture will be converted as transparent, could it be detected automatically based on the emf itself? We just want to keep the converted png file the same as the original emf files.

Below is the source, you can run below to compile and run:
// Build: mvn clean compile assembly:single
// Run: java -Xmx3550m -jar Aspose.Imaging.Test-1.0-SNAPSHOT.jar “” “emf2png” “…/test/test6.emf” “…/test/test6.png”
Aspose.Imaging.Test.1.zip (5.9 KB)

@tldyzem

Thank you for sharing the information with us. A ticket with ID IMAGINGJAVA-7798 has been created in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be notified once the issue will be fixed.

@tldyzem

We have worked further over the issue:

1: It works well on win10, but not on ubuntu 18.04 LTS

We have tested the issue in following environments:

  • Ubuntu Linux 21.04
  • WSL (Windows Subsystem for Linux) for Win10, Docker.

In both environments the test code produce correct output image in short time (less than 1 second) and nothing failed.

2: For transparent settings, if we setBackgroundColor to transparent, all picture will be converted as transparent, could it be detected automatically based on the emf itself? We just want to keep the converted png file the same as the original emf files.

The answer is “No”. Since the output image format, including transparency, depends on options passed by code. So you should always use setBackgroundColor.

If you can provide us more information about your test server configuration (OS, if you use Docker we need Dockerfile, if you use WSL we need more information about installed packages and X Window configuration).

There is only one idea to avoid the error

Aspose error: class com.aspose.imaging.coreexceptions.ImageLoadException: Image saving failed. —> java.lang.ExceptionInInitializerError

to use Java property: headless

Run: java -Djava.awt.headless=true -Xmx3550m -jar Aspose.Imaging.Test-1.0-SNAPSHOT.jar “” “emf2png” “…/test/test6.emf” “…/test/test6.png”

@tldyzem

Can you please share if we may close the issue on our end or there is any further feedback in this regard from your side.

Thanks for answering the question, no more question on this issue.