How to add a new image layer to background canvas

Hi, Support:

Would you show me a full demo how to add a new image layer to background canvas at given (x,y) position using apose.psd.dll based on Vb.net.

Thanks for your help.

@ducaisoft

To add a new image layer to the background canvas at a specified (x, y) position using Aspose.PSD in VB.NET, you can follow the example code below. This code demonstrates how to load a PSD file, create a new layer with an image, set its position, and then save the modified PSD file.

Here’s a complete demo:

Imports Aspose.Psd
Imports Aspose.Psd.Layers

Module Module1
    Sub Main()
        ' Load the existing PSD file
        Using image As PsdImage = CType(Image.Load("file.psd"), PsdImage)
            ' Load the image to be added as a layer
            Dim layerImage As RasterImage = CType(Image.Load("picture.png"), RasterImage)
            ' Create a new layer with the loaded image
            Dim layer As New Layer(layerImage, True)

            ' Set the desired position for the layer
            Dim x As Integer = 100
            Dim y As Integer = 200
            layer.Left = x
            layer.Top = y
            layer.Right = x + layer.Width
            layer.Bottom = y + layer.Height

            ' Add the new layer to the image
            image.AddLayer(layer)

            ' Save the modified PSD file
            image.Save("output.psd")
        End Using
    End Sub
End Module

Explanation:

  1. Load the PSD File: The code starts by loading an existing PSD file using Image.Load.
  2. Load the Image for the Layer: It then loads the image that you want to add as a new layer.
  3. Create a New Layer: A new layer is created with the loaded image.
  4. Set Layer Position: The position of the layer is set using the Left, Top, Right, and Bottom properties based on the specified (x, y) coordinates.
  5. Add Layer to Image: The new layer is added to the PSD image.
  6. Save the PSD File: Finally, the modified PSD file is saved.

This code will help you add an image layer to the background canvas at the specified position. Make sure to replace "file.psd" and "picture.png" with the actual paths to your PSD file and image file, respectively.

The result is that layerImage is nothing. How to reach it?

@ducaisoft please check the following example:

    Using stream1 As FileStream = New FileStream("img1.png", FileMode.Open)
        Using stream2 As FileStream = New FileStream("img2.png", FileMode.Open)
            Dim layer1 = New Layer(stream1)
            Dim layer2 = New Layer(stream2)
            Using newImg As PsdImage = New PsdImage(200, 200)
                Dim newLayers = New List(Of Layer)
                newLayers.Add(layer1)

               Dim width = layer2.Width
                Dim height = layer2.Height
                ' Here is position
                Dim givenPositionLeft = 50
                Dim givenPositionTop = 25

                layer2.Left = givenPositionLeft
                layer2.Top = givenPositionTop
                layer2.Right = givenPositionLeft + width
                layer2.Bottom = givenPositionTop + height
                
                ' The order of layers can be changed using theirs order in Layers property.
                newLayers.Add(layer2)
                newImg.Layers = newLayers.ToArray
                newImg.Save("final.psd", New PsdOptions)
            End Using
        End Using
    End Using

Thanks for your demo.
However ,there is error while adding multilayers to the background image, and all the layers are blank except for layer 1 and layer 2. What’s wrong? and how to fix it?
Please see the result generated by Psd.dll v23.4.

123.jpg (93.9 KB)

@ducaisoft could you please provide the minimal source code to reproduce this issue and the input files.

the minimal codes for this as follow:

Dim CanvasW as integer=7087, CanvasH as integer=12000,xyDPI as integer=300
Dim PSDBmp As PsdImage = New PsdImage(CanvasW, CanvasH)
PSDBmp.SetResolution(xyDPI, xyDPI)
Dim Files(4) as string
Files(0)=“C:\Test\Test1.png”
Files(1)=“C:\Test\Test2.png”
Files(2)=“C:\Test\Test3.png”
Files(3)=“C:\Test\Test4.png”
Files(4)=“C:\Test\Test5.png”
Dim Layer As Layer,Result as string,x as integer,y as integer
for each s as string in Files
Dim Stream As FileStream = New FileStream(sFile, FileMode.Open, FileAccess.Read)
Dim Buffer(Stream.Length - 1) As Byte
Stream.Read(Buffer, 0, Buffer.Length)
On Error Resume Next
Layer = New Layer(Stream)

        Result = Err.Number & " " & Err.Description
    'End Using
    Layer.SetResolution(xyDPI, xyDPI)
    Layer.Left = x
    Layer.Top = y
    On Error Resume Next
    PSDBmp .AddLayer(Layer)

next
PSDBmp .save(PSdFile)


'when s=files(2),the layer=new layer(stream), the layer is nothing so that the result of layer 3 is blank.


@ducaisoft I can not reproduce this issue.

Could you please describe environment and write version of Aspose.PSD. What’s size/resolution of input files?

Could you please check the following:

  1. Input PNG files are correct and can be opened by other readers.
  2. Check if here is used correct variable “sFile”, in my case I updated “for each” statement with sFile instead of s.
      for each s as string in Files
      Dim Stream As FileStream = New FileStream(sFile, FileMode.Open, FileAccess.Read)