Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export more than one image to layered PSD #346

Closed
2 tasks done
kwhopper opened this issue Oct 30, 2018 · 4 comments
Closed
2 tasks done

Export more than one image to layered PSD #346

kwhopper opened this issue Oct 30, 2018 · 4 comments
Labels

Comments

@kwhopper
Copy link

Prerequisites

  • I have written a descriptive issue title

  • I have verified that I am using the latest version of Magick.NET

  • Magick.NET version: 7.9.0.1 Q16

  • Environment (Operating system, version and so on): Windows 10 x64

Question

Is it possible to export a MagickImageCollection as a PSD with separate layers? If so, is it possible to control the layer names?

I have tried both MagickImageCollection.Write by itself, and MagickImageCollection.Mosaic().Write but neither seems to give me the desired output. Thanks

@dlemstra
Copy link
Owner

You can control the name of the layer with the Label property. When you want to write a PSD file with multiple layers you will also need to add an extra image to the collection. The first image of the collection is the combined image so you will need to also add that as an extra "layer".

@kwhopper
Copy link
Author

Cool. Thanks!

@DmytroLamashevskyi
Copy link

@dlemstra Could you create some examples of how it should be coded? I tried several options and didn't understand how to create Layers with Masks......
Here is my code:


using(var psdLayers = new MagickImageCollection())
{
    // Read original image into memory stream
    using var memoryStream = new MemoryStream();
    originalImageFile.CopyTo(memoryStream);
    memoryStream.Position = 0;
    // Read alpha (mask) image into memory stream
    using var memoryStream2 = new MemoryStream();
    alphaImageFile.CopyTo(memoryStream2);
    memoryStream2.Position = 0;

    // Create a mask for the second layer (layer1)
    using(var baseImage = new MagickImage(MagickColors.Transparent, width, height))
    {
        // Create a mask for the second layer (layer1)
        using(var layerImage = new MagickImage(memoryStream))
        {
            layerImage.Composite(baseImage, 0, 0, CompositeOperator.Overlay);
            layerImage.Label = "Layer1";
            layerImage.Settings.SetDefines(new PsdWriteDefines());
            layerImage.Format = MagickFormat.Psd;
            psdLayers.Add(layerImage.Clone());
        }
    }
    // Create a mask for the second layer (layer1)
    using(var layerImage = new MagickImage(memoryStream))
    {
        // Create a new MagickImage for the composite result
        using(var mask = new MagickImage(memoryStream2))
        {
            // Composite the mask image onto the composite image
            layerImage.Composite(mask, 0, 0, CompositeOperator.CopyAlpha);
            layerImage.SetWriteMask(mask);
            layerImage.Label = "Layer2";
            layerImage.Settings.SetDefines(new PsdWriteDefines());
            psdLayers.Add(layerImage.Clone());
        }
    }
    // Save the final image as a PSD file with layers and masks
    return File(psdLayers.ToByteArray(), "application/octet-stream", "output.psd");

@dlemstra
Copy link
Owner

Please open a new discussion instead of hijacking an old issue about something else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants