How to copy a layer from a psd file to another

I want to load psd file and copy a layer from the psd to another psd created from by PsdImage
like following:

PsdImage image = (PsdImage)Image.Load(sourceFile)
PsdImage anotherImage = new PsdImage(100, 200);
anotherImage.AddLayers(image.Layers[0]);
anotherImage.Save(outputPath, new PsdOption())

But it doesn’t work when the layer is Smart Object, what should I do?

@rufuzhanshi i tested your example and faced with exception because of anotherImage.GlobalLayerResources not initialized. After initialization of anotherImage.GlobalLayerResources SmartObjectLayer was added to anotherImage correctly and anotherImage was saved with smart object from another image.
Code with solution:

  using (PsdImage image = (PsdImage)Image.Load(sourceFile))
  {
      using (PsdImage anotherImage = new PsdImage(100, 200))
      {
          anotherImage.GlobalLayerResources = new LayerResource[0];
          anotherImage.AddLayer(image.Layers[0]);
          anotherImage.Save(outputPath);   
      }
  }

@yaroslav.lisovskyi hello, your solution still doesn’t work.
it cause some problems that some functions like free transform tools can‘t work normally.
I guess it can be related to the resources. And I can give you 2 photo to check problems.

Load File:
input.png (73.1 KB)

Output File:
output.png (37.9 KB)

it can be easily to copy whole layer from a psd file to another without any loss by using ctrl+c and ctrl+v in windows operation system. now i hope realize it by code. Whether is there a function to do it?

@rufuzhanshi Currently we dont have a specific function to copy-paste layers between PsdImage’s.
Can you provide your sorce file (or analogic file) and code so i can check issue and create related ticket or find solution for you?

@yaroslav.lisovskyi Actually, what i want to do is to save every layer in a psd file into single psd files.
Like following:

Input file:
input.zip (280.1 KB)

Output file:
output.zip (408.3 KB)

I hope I can make every single layer into single psd file without any loss. That means i can use any function like transform, move and so on. If it is Smart Object originally, the output file should be also Smart object.

@rufuzhanshi, i runned code on Aspose.PSD 24.1 and used your input file.
I compared my output files and yours and it seems to be the same (except layer names), my output smart object also can be opened correctly.
Please check it on your system and if it’s still not work give us more information about your platform, version of Aspsoe.PSD and NetFramework.
Code and files attached.

  string dir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  string sourceFile = Path.Combine(dir, "testpsd.psd");

  using (PsdImage image = (PsdImage)Image.Load(sourceFile))
  {
      foreach (var layer in image.Layers)
      {
          using (PsdImage anotherImage = new PsdImage(image.Width, image.Height))
          {
              anotherImage.GlobalLayerResources = new LayerResource[0];
              anotherImage.AddLayer(layer);
              anotherImage.Layers = new Layer[] { anotherImage.Layers[1] }; // remove default background layer
              
              string outputPath = Path.Combine(dir, layer.DisplayName + "_output.psd");
              anotherImage.Save(outputPath);
          }
      }
  }

279700.zip (678.9 KB)

@yaroslav.lisovskyi Thank you for solving my problem. my version is not the newest.

And next I have another question.
I want to realize the effect that the the size of canvas in exported single layer psd file should be same with the layer image. I’ve tried crop, but it doesn’t work normally. The details you can see following

input file:
input.zip (280.1 KB)

output file:
output.zip (230.5 KB)

@rufuzhanshi for your task can help method Crop.
Try this code:

  string dir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  string sourceFile = Path.Combine(dir, "testpsd.psd");
  
  using (PsdImage image = (PsdImage)Image.Load(sourceFile))
  {
      foreach (var layer in image.Layers)
      {
          using (PsdImage anotherImage = new PsdImage(image.Width, image.Height))
          {
              anotherImage.GlobalLayerResources = new LayerResource[0];
              anotherImage.AddLayer(layer);
              Layer newLayer = anotherImage.Layers[1];
              anotherImage.Layers = new Layer[] { newLayer }; // remove default background layer
  
              var rect = new Rectangle(
                  newLayer.Left,
                  newLayer.Right,
                  anotherImage.Width - newLayer.Width,
                  anotherImage.Height - newLayer.Height);
              if (!rect.IsEmpty)
              {
                  anotherImage.Crop(new Rectangle(newLayer.Left, newLayer.Top, newLayer.Width, newLayer.Height));
              }
              
              string outputPath = Path.Combine(dir, layer.DisplayName + "_output.psd");
              anotherImage.Save(outputPath);
          }
      }
  }

Hello, I used your code and get the cropped image, but there is something strange happened —— the output psd image get aliasing. That means it is not same with origianal one.

details following:

input:
input.png (15.2 KB)

output:
output.png (14.8 KB)

@yaroslav.lisovskyi

@rufuzhanshi a rotary transformation is performed over the specified layer, Aspose.PSD has its own implementation of all functions and their accuracy may differ from that in Photoshop.
At the moment we can’t create the same pixel perfect rendering.