This documentation is for the old version. Go to the latest Graphics Mill docs

Loading Raster Layers

As it is said in Advanced PSD Add-on Concepts section, Advanced PSD Add-on for Graphics Mill supports several types of layers. In particular it supports raster layers. Using this feature of the add-on you can work with raster layers as you do with a bitmap. This article describes how to do it.

Class Structure

The main class you should use to load layers with Advanced PSD Add-on is Aurigma.GraphicsMill.Codecs.AdvancedPsdReader. Further this class will be referred as advanced PSD reader (do not mix with Aurigma.GraphicsMill.Codecs.PsdReader which can load only the first frame from the PSD file).

Advanced PSD reader can be interpreted as a collection of layers. Each layer is the Aurigma.GraphicsMill.Codecs.AdvancedPsdFrame object or its descendant.

Start Using Advanced PSD Reader

In order to start reading layers from PSD file you should open it by advanced PSD reader. You can either use the Open method or pass PSD file name to the constructor of the reader.

Note

When the file is opened by the reader, no data actually is loaded into memory. The Open method initializes internal memory structures, and loads video data only on demand. However it locks the file, so another application can neither delete nor modify it.

Get Access to Layers

As soon as you opened PSD file you can access to layers. You can do it using the foreach statement as follows:

Visual Basic
' Getting layers in Foreach statement 
Dim frame As Aurigma.GraphicsMill.Codecs.AdvancedPsdFrame
For Each frame In psdReader

    ' ... frame processing code is omitted for brevity
    Console.WriteLine("Frame #{0} is processed.", frame.Index)
Next
C#
// Getting layers in foreach statement 
foreach (Aurigma.GraphicsMill.Codecs.AdvancedPsdFrame frame in psdReader)
{
    // ... frame processing code is omitted for brevity
    Console.WriteLine("Frame #{0} is processed.", frame.Index);
}

Alternatively you can load raster layers using the LoadFrame(Int32) method. In order to get the total number of frames, use the FrameCount property. Iterate through layers using the for statement:

Visual Basic
' Getting layers in For statement by index
Dim frame As Aurigma.GraphicsMill.Codecs.AdvancedPsdFrame
Dim I As Integer
For I = 0 To psdReader.FrameCount

    frame = psdReader.LoadFrame(I)
    ' ... frame processing code is omitted for brevity
    Console.WriteLine("Frame #{0} is processed.", frame.Index)
Next
C#
// Getting layers in for statement by index
Aurigma.GraphicsMill.Codecs.AdvancedPsdFrame frame;
for (int i=0;i<psdReader.FrameCount;i++)
{ 
    frame = (Aurigma.GraphicsMill.Codecs.AdvancedPsdFrame)psdReader.LoadFrame(i);
    // ... frame processing code is omitted for brevity
    Console.WriteLine("Frame #{0} is processed.", frame.Index);
}

Extract Image from Layer

After you have got the layer you can extract the bitmap from it. Use the GetBitmap(Bitmap) method of the AdvancedPsdFrame object for this purpose. This method will initialize the specified instance of Aurigma.GraphicsMill.Bitmap class by the image stored in the layer.

Note

Make sure that the layer is not of unknown type. Such layers do not contain a bitmap. That is why you will be unable to extract the bitmap from them.

Let's examine how to pull layers out of the PSD file and save them into separate files. The following code snippet demonstrates how to do it.

Visual Basic
' Create advanced PSD reader object to work with the PSD file.
Dim psdReader As New Aurigma.GraphicsMill.Codecs.AdvancedPsdReader

' Create the bitmap object which will store the image extracted from layers.
Dim Bitmap As New Aurigma.GraphicsMill.Bitmap

' Open advanced PSD reader to be able to get layers.
psdReader.Open("C:\Temp\WorkingWithLayers.psd")

' Within each frame in reader check out that it is not unknown.
Dim frame As Aurigma.GraphicsMill.Codecs.AdvancedPsdFrame
For Each frame In psdReader

    ' Skip if the layer type is unknown.
    If (frame.Type <> Aurigma.GraphicsMill.Codecs.PsdFrameType.Unknown) Then

        ' Extract the image from the layer.
        frame.GetBitmap(Bitmap)

        ' Save layer image in PNG format.
        Bitmap.Save("C:\Temp\frame_" & frame.Index & ".png")
    End If

Next
' Clean up.
psdReader.Close()
C#
// Create advanced PSD reader object to work with the PSD file.
Aurigma.GraphicsMill.Codecs.AdvancedPsdReader psdReader = 
    new Aurigma.GraphicsMill.Codecs.AdvancedPsdReader();

// Create the bitmap object which will store the image extracted from layers.
Aurigma.GraphicsMill.Bitmap bitmap = new Aurigma.GraphicsMill.Bitmap();

// Open advanced PSD reader to be able to get layers.
psdReader.Open(@"C:\Temp\WorkingWithLayers.psd");

// Within each frame in reader check out that it is not unknown.
foreach (Aurigma.GraphicsMill.Codecs.AdvancedPsdFrame frame in psdReader)
{
    // Skip if the layer type is unknown.
    if (frame.Type != Aurigma.GraphicsMill.Codecs.PsdFrameType.Unknown)
    {
        // Extract the image from the layer.
        frame.GetBitmap(bitmap);
        
        // Save layer image in PNG format.
        bitmap.Save(@"C:\Temp\frame_" + frame.Index + ".png");
    }
}

// Clean up.
psdReader.Close();

See Also

Manual