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

Multiple layer (PSD) placing images on top of each other

Sep 4, 2015 at 11:00 AM
Edited Sep 4, 2015 at 11:01 AM
How to put multiple images on top of each other, each of the 0,0 position to go?

Image.page() does not work in vain it at the start value 0, and the end value maximum size of the picture.
All images are the same size.

When I save to file separate the layers, they will be the right place.
Coordinator
Sep 5, 2015 at 10:33 AM
I am not sure what you are asking? Do you want to put all the images from your input image (PSD) on top of each other? If that is want you want you could do the following:
using (MagickImageCollection psd = new MagickImageCollection("YourFile.psd"))
{
  using (MagickImage result = psd.Flatten())
  {
    result.Write("Output.jpg");
  }
}
Sep 7, 2015 at 12:37 PM
I want to save more images in the one PSD file.

The first layer exclude the psd file, I would also like to save.
using (MagickImageCollection psd = new MagickImageCollection())
{
    MagickImage[] image = new MagickImage[CloneMap.Layers.Count];
    SharpMap.Map psdMap = new SharpMap.Map(CloneMap.Size);

    foreach (SharpMap.Layers.ILayer layer in CloneMap.Layers)
    {
        if (layer.SRID == 0)
        {
            foreach (SharpMap.Layers.Layer psdLayer in CloneMap.Layers)
            {
                if (layer.LayerName == psdLayer.LayerName)
                    psdMap.Layers.Add(psdLayer);
                else
                {
                    SharpMap.Layers.ILayer lay = psdMap.Layers.GetLayerByName(psdLayer.LayerName);
                    if (lay != null)
                        psdMap.Layers.Remove(lay);
                }
            }
            psdMap.ZoomToBox(bx);
            Image imageSource = psdMap.GetMap();

            ImageConverter converter = new ImageConverter();

            image[CloneMap.Layers.IndexOf(layer)] = new MagickImage((byte[])converter.ConvertTo(imageSource, typeof(byte[])), settings);
            imageSource.Dispose();
            GC.Collect();
            image[CloneMap.Layers.IndexOf(layer)].ResolutionUnits = Resolution.PixelsPerInch;
            image[CloneMap.Layers.IndexOf(layer)].Density = new PointD(dpi);
            image[CloneMap.Layers.IndexOf(layer)].Label = layer.LayerName;
            psd.Add(image[CloneMap.Layers.IndexOf(layer)]);
        }
    }

    using (MagickImage result = psd.Flatten())
    {
        psd.Write(filepath + "\\" + filename + "_" + filenumber + fileext);
    }                                
}
Coordinator
Sep 7, 2015 at 2:34 PM
Edited Sep 7, 2015 at 2:34 PM
I don't understand what you mean by this:
I want to save more images in the one PSD file.
The first layer exclude the psd file, I would also like to save.
You have a PSD file and you want to add extra layers?
Sep 8, 2015 at 7:11 AM
Edited Sep 8, 2015 at 9:11 AM
I create a new ImageCollection, add a multiple layer (min. 38), and save the imagecollection new psd file.

The first layer don't save, and the layers position is not correct.
Coordinator
Sep 8, 2015 at 12:04 PM
Edited Sep 8, 2015 at 12:04 PM
You should call Write on your MagickImageCollection if you want to write all the layers in a PSD file. You should also include an extra image as the first image that is the 'flattened' version of your image. You can change the layer position with the Page property of MagickImage. Below is a simplified example:
using (MagickImageCollection psd = new MagickImageCollection())
{
  // Add all the layers to your image.
  foreach(MagickImage image in CreateLayers())
  {
    // Set the position of the layer.
    image.Page = new MagickGeometry(6, 6, image.Width, image.Height);

    psd.Add(image);
  }

  // Create flattened image of all the layers and insert this as the first image.
  MagickImage flattened = psd.Flatten();
  psd.Insert(0, flattened);

  // Write the output image.
  psd.Write("output.psd");
}
Sep 8, 2015 at 2:05 PM
Thus, the first layer has been is saved.
MagickImage flattened = psd.Flatten();
psd.Insert(0, flattened);
Positioning in turn found a bug:
If separate save layers to file, and next save to psd, at that time layers position is correct and image quality is good, and psd file size cca. half the size of the bad file.
Image

Else if layers load from memory and save to psd, at that time layers position is bad and image quality very poor.
Image
Coordinator
Sep 9, 2015 at 11:50 AM
Are you sure that you used the correct positions? Your first image does not look close to the second image. You should set the positions yourself.
Sep 11, 2015 at 1:19 PM
Yes, I set the positions. If the save a layer and load from there on, it is good for PSD. If don't save the layers, only save the psd, the file is bad.
Coordinator
Sep 11, 2015 at 7:39 PM
I don't understand why your images are so different if you think you are specifying the correct positions. I don't have enough information at the moment to help you further. Would it be possible to create a small console application that demonstrates this incorrect behavior?