This project has moved. For the latest updates, please go here.

Opening Layered PSD With Transparency & Exporting to PNG with Transparency

Nov 5, 2013 at 2:37 PM
Hi,

I am trying to open a layered PSD file and then selectively (based on the layers I add to a new collection), export those layers to a PNG file with transparency. I've tried a number of permutations with various settings but have not been successful. The image always is generated but never with transparency. Below is the code that I've been using. What am I missing?

Thanks!
 MagickReadSettings settings = new MagickReadSettings {ColorSpace = ColorSpace.Transparent };

      using (MagickImageCollection collection = new MagickImageCollection(@"c:\\test\\rbg.psd", settings))
      {
        MagickImageCollection images = new MagickImageCollection { collection[3], collection[2], collection[1] };
        MagickImage newImage = new MagickImage(images.Mosaic()) { Format = MagickFormat.Png24, ColorSpace = ColorSpace.RGB };
        newImage.ColorAlpha(new MagickColor(255, 255, 255, 0));
        newImage.Alpha(AlphaOption.Set);
        newImage.Write(@"c:\\test\out2.png");
      }
Coordinator
Nov 5, 2013 at 2:52 PM
Are you aware of the fact that MagickFormat.Png24 is RGB and does not contain an alpha channel? Your "newImage" does not contain an alpha channel. Are you trying to change the white color to transparent?
Nov 5, 2013 at 2:59 PM
Hi,

Thanks for the prompt response.

No, I was not aware of that, nor sure that I fully understand it (the relationship between image formats, color spaces and transparency, that is...). Are you saying that we can't save to PNG with Transparency or that we need to pick either/or different colors spaces/formats?

I am not trying to convert white to transparent. I am just trying to honor the transparency that is in the PSD file. We need to automate the creation of roughly 600 images by selecting permutations of layers in the PSD file. The code that I have above was a proof of concept to see what was possible.
Coordinator
Nov 5, 2013 at 4:29 PM
Edited Nov 5, 2013 at 8:35 PM
If you are trying to preserve transparency then all you have to do is this:
using (MagickImage newImage = images.Mosaic()) // No need to use the copy constructor
{
  newImage.Write(@"c:\test\out2.png");
}
I think I will also add a Reverse method to MagickImageCollection to reverse the order of the images. You should also Dispose the images variable.
Nov 5, 2013 at 4:40 PM
I've simplified my code down to
     using (MagickImageCollection collection = new MagickImageCollection(@"c:\test\rbg.psd"))
      {
        MagickImageCollection images = new MagickImageCollection {collection[3], collection[2], collection[1]};
        using (MagickImage newImage = new MagickImage(images.Mosaic()))
          newImage.Write(@"c:\test\out2.png");
      }

This still produces a PNG without transparency. Where there is supposed to be transparency there is white.

If it helps the two test files are here:
Test files

Reverse method would be a nice addition.
Coordinator
Nov 5, 2013 at 8:34 PM
Edited Nov 5, 2013 at 8:34 PM
The mosaic method uses the background color of the first image when combining your image. With the following code you will get an image with a transparent color:
images[0].BackgroundColor = MagickColor.Transparent;
using (MagickImage newImage = images.Mosaic())
  newImage.Write(@"c:\test\out2.png");
The Reverse method has been added.
Nov 5, 2013 at 8:39 PM
This works -- many thanks!!