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

How to set ImageCollection (PSD) dpi?

Sep 4, 2015 at 7:25 AM
Edited Sep 4, 2015 at 7:30 AM
How to set ImageCollection dpi?
MagickReadSettings settings = new MagickReadSettings();
settings.Density = new PointD(150);

using (MagickImageCollection images = new MagickImageCollection())
{
    MagickImage[] image = new MagickImage[CloneMap.Layers.Count];

    foreach (SharpMap.Layers.ILayer layer in CloneMap.Layers)
    {
        SharpMap.Map psdMap = new SharpMap.Map(CloneMap.Size);

        foreach (SharpMap.Layers.Layer psdLayer in CloneMap.Layers)
        {
            if (layer.LayerName == psdLayer.LayerName)
                psdMap.Layers.Add(psdLayer);
        }

        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)].Format = MagickFormat.Psd;
        image[CloneMap.Layers.IndexOf(layer)].CompressionMethod = CompressionMethod.RLE;
        image[CloneMap.Layers.IndexOf(layer)].Density = new PointD(150);
        image[CloneMap.Layers.IndexOf(layer)].Label = layer.LayerName;
                                    
        images.Add(image[CloneMap.Layers.IndexOf(layer)]);
    }

    images.Write(filepath + "\\" + filename + "_" + filenumber + fileext);
}
Saved psd only 15 dpi. Why?
Coordinator
Sep 5, 2015 at 9:08 AM
Edited Sep 5, 2015 at 9:12 AM
How are you checking the DPI of your output image? The PSD writer use the Density of the first image in the collection. I am getting the correct output when I execute the following code:
using (MagickImageCollection psd = new MagickImageCollection())
{
  MagickImage logo = new MagickImage("logo:");
  logo.Density = new PointD(300);
  psd.Add(logo);

  byte[] data = psd.ToByteArray(MagickFormat.Psd);

  using (MagickImage result = new MagickImage(data))
  {
    Console.WriteLine(result.ToString());
    Console.WriteLine(result.Density.X + "x" + result.Density.Y);
  }
}
My output is below, are you using the latest version of Magick.NET?
Psd 640x480 8-bit sRGB 300.82kB
300x300