LEADTOOLS Image File Support (Leadtools.Codecs assembly)
LEAD Technologies, Inc

LoadPsdLayerAsync Method

Example 





The input PSD stream.

Resulting image pixel depth. Valid values are:

Value Meaning
0 Keep the original file's pixel depth (Do not convert).
1 to 8 The specified bits per pixel in the resulting image.
12 12 bits per pixel in the resulting image.
16 16 bits per pixel in the resulting image.
24 24 bits per pixel in the resulting image.
32 32 bits per pixel in the resulting image.
48 48 bits per pixel in the resulting image.
64 64 bits per pixel in the resulting image.
The desired color order.
Index of the layer to load. This index is zero-based. Pass 0 to load the first layer, 1 to load the second layer, etc.
a CodecsPsdLayerInfo object to be updated with information about the loaded layer. Pass a null reference (Nothing in Visual Basic) for this parameter if layer information is not needed.
Loads the specified layer from the specified PSD file. .NET support WinRT support
Syntax
public IAsyncOperation<RasterImage> LoadPsdLayerAsync( 
   ILeadStream stream,
   int bitsPerPixel,
   CodecsLoadByteOrder order,
   int layer,
   CodecsPsdLayerInfo layerInfo
)
'Declaration
 
Public Function LoadPsdLayerAsync( _
   ByVal stream As ILeadStream, _
   ByVal bitsPerPixel As Integer, _
   ByVal order As CodecsLoadByteOrder, _
   ByVal layer As Integer, _
   ByVal layerInfo As CodecsPsdLayerInfo _
) As IAsyncOperation(Of RasterImage)
'Usage
 
Dim instance As RasterCodecs
Dim stream As ILeadStream
Dim bitsPerPixel As Integer
Dim order As CodecsLoadByteOrder
Dim layer As Integer
Dim layerInfo As CodecsPsdLayerInfo
Dim value As IAsyncOperation(Of RasterImage)
 
value = instance.LoadPsdLayerAsync(stream, bitsPerPixel, order, layer, layerInfo)
public IAsyncOperation<RasterImage> LoadPsdLayerAsync( 
   ILeadStream stream,
   int bitsPerPixel,
   CodecsLoadByteOrder order,
   int layer,
   CodecsPsdLayerInfo layerInfo
)
 function Leadtools.Codecs.RasterCodecs.LoadPsdLayerAsync( 
   stream ,
   bitsPerPixel ,
   order ,
   layer ,
   layerInfo 
)
public:
IAsyncOperation<RasterImage^>^ LoadPsdLayerAsync( 
   ILeadStream^ stream,
   int bitsPerPixel,
   CodecsLoadByteOrder order,
   int layer,
   CodecsPsdLayerInfo^ layerInfo
) 

Parameters

stream
The input PSD stream.
bitsPerPixel

Resulting image pixel depth. Valid values are:

Value Meaning
0 Keep the original file's pixel depth (Do not convert).
1 to 8 The specified bits per pixel in the resulting image.
12 12 bits per pixel in the resulting image.
16 16 bits per pixel in the resulting image.
24 24 bits per pixel in the resulting image.
32 32 bits per pixel in the resulting image.
48 48 bits per pixel in the resulting image.
64 64 bits per pixel in the resulting image.
order
The desired color order.
layer
Index of the layer to load. This index is zero-based. Pass 0 to load the first layer, 1 to load the second layer, etc.
layerInfo
a CodecsPsdLayerInfo object to be updated with information about the loaded layer. Pass a null reference (Nothing in Visual Basic) for this parameter if layer information is not needed.

Return Value

When this method completes, it returns the Leadtools.RasterImage object that this method loads.
Remarks

Use this method to load PSD files only.

This method works similarly to a normal Load(String) or LoadAsync(String,LeadRect,Int32,CodecsLoadByteOrder,Int32,Int32,Object) methods, except that it loads only a layer from a PSD file. It loads the layer specified in layer.

Before calling this method, you may need to get or set file information, such as the number of layers on the file. Refer to CodecsPsdImageInfo.

The number of layers in a file is indicated by CodecsPsdImageInfo.Layers. If this number is 0, the file does not contain any layers and this method should not be called.

Example
 
Public Sub PsdLayersExample()
      Dim codecs As RasterCodecs = New RasterCodecs()

      Dim layerFileNames As String() = _
      { _
         Path.Combine(LEAD_VARS.ImagesDir, "Sample1.cmp"), _
         Path.Combine(LEAD_VARS.ImagesDir, "Sample2.cmp"), _
         Path.Combine(LEAD_VARS.ImagesDir, "Sample3.cmp") _
      }

      Dim srcFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp")
      Dim destFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1.psd")

      ' Load the layer images (as pages in 1 image)
      Dim layersImage As RasterImage = Nothing
      For Each layerFileName As String In layerFileNames
         Dim layerImage As RasterImage = codecs.Load(layerFileName)
         If layersImage Is Nothing Then
            layersImage = layerImage
         Else
            layersImage.AddPage(layerImage)
         End If
      Next layerFileName

      ' Load the image that is made up of all the layers
      Dim image As RasterImage = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.BgrOrGray, 1, 1)

      ' Save this image and all the layers
      codecs.SavePsdWithLayers(image, destFileName, 0, layersImage, Nothing)
      image.Dispose()
      layersImage.Dispose()

      Dim imageInfo As CodecsImageInfo = codecs.GetInformation(destFileName, False)
      If imageInfo.Psd.Layers > 0 Then
         Dim layer As Integer = 0
         Dim layerInfo As CodecsPsdLayerInfo = New CodecsPsdLayerInfo()
         Dim layerImage As RasterImage = codecs.LoadPsdLayer(destFileName, 0, CodecsLoadByteOrder.BgrOrGray, layer, layerInfo)

         Dim blendModeKey As String = Encoding.ASCII.GetString(layerInfo.GetBlendModeKey())

         Console.WriteLine("Loaded layer at index {0}, size is {1} by {2}, Blend mode key:{3}", layer, layerImage.Width, layerImage.Height, blendModeKey)
         Console.WriteLine("TransparencyProtected is {1}, Visible is {2}, Obsolete is {3} and Psd5OrLater is {4}", _
            layerInfo.TransparencyProtected, layerInfo.Visible, layerInfo.Obsolete, layerInfo.Psd5OrLater)
         layerImage.Dispose()
      Else
         Console.WriteLine("No layers found in this PSD file")
      End If

      ' Clean up
      codecs.Dispose()
   End Sub

Public NotInheritable Class LEAD_VARS
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
public void PsdLayersExample()
     {
         RasterCodecs codecs = new RasterCodecs();

         string[] layerFileNames =
      {
         Path.Combine(LEAD_VARS.ImagesDir, "Sample1.cmp"),
         Path.Combine(LEAD_VARS.ImagesDir, "Sample2.cmp"),
         Path.Combine(LEAD_VARS.ImagesDir, "Sample3.cmp"),
      };

         string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp");
         string destFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.psd");

         // Load the layer images (as pages in 1 image)
         RasterImage layersImage = null;
         foreach (string layerFileName in layerFileNames)
         {
             RasterImage layerImage = codecs.Load(layerFileName);
             if (layersImage == null)
                 layersImage = layerImage;
             else
                 layersImage.AddPage(layerImage);
         }

         // Load the image that is made up of all the layers
         RasterImage image = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.BgrOrGray, 1, 1);

         // Save this image and all the layers
         codecs.SavePsdWithLayers(image, destFileName, 0, layersImage, null);
         image.Dispose();
         layersImage.Dispose();

         CodecsImageInfo imageInfo = codecs.GetInformation(destFileName, false);
         if (imageInfo.Psd.Layers > 0)
         {
             int layer = 0;
             CodecsPsdLayerInfo layerInfo = new CodecsPsdLayerInfo();
             RasterImage layerImage = codecs.LoadPsdLayer(destFileName, 0, CodecsLoadByteOrder.BgrOrGray, layer, layerInfo);

             string blendModeKey = Encoding.ASCII.GetString(layerInfo.GetBlendModeKey());

             Console.WriteLine("Loaded layer at index {0}, size is {1} by {2}, Blend mode key:{3}", layer, layerImage.Width, layerImage.Height, blendModeKey);
             Console.WriteLine("TransparencyProtected is {0}, Visible is {1}, Obsolete is {2} and Psd5OrLater is {3}",
                layerInfo.TransparencyProtected, layerInfo.Visible, layerInfo.Obsolete, layerInfo.Psd5OrLater);
             layerImage.Dispose();
         }
         else
             Console.WriteLine("No layers found in this PSD file");

         // Clean up
         codecs.Dispose();
     }

static class LEAD_VARS
{
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
RasterCodecsExamples.prototype.PsdLayersExample = function () {
   Tools.SetLicense();
   with (Leadtools) {
      with (Leadtools.Codecs) {
         var codecs = new RasterCodecs();

         var layerFileNames =
            [
               "Assets\\Sample1.cmp",
               "Assets\\Sample2.cmp",
               "Assets\\Sample3.cmp",
            ];

         var srcFileName = "Assets\\Image1.cmp";
         var destFileName = "Image1.psd";
         var saveFile;

         // Load the layer images (as pages in 1 image)
         var layersImage = null;
         var image = null;
         var loadFile = null;
         var promises = layerFileNames.map(function (layerFileName) {

            return Tools.AppInstallFolder().getFileAsync(layerFileName).then(function (loadFileX) {
               loadFile = loadFileX;
               var async = codecs.loadAsync(LeadStreamFactory.create(loadFile));
               return WinJS.Promise.join(async)
            .then(function (layerImage) {
               if (layersImage == null)
                  layersImage = layerImage;
               else
                  layersImage.addPage(layerImage);
            });
            });
         });

         return WinJS.Promise.join(promises).then(function () {
            // Load the image that is made up of all the layers
            return Tools.AppInstallFolder().getFileAsync(srcFileName)
         })
         .then(function (loadFileX) {
            loadFile = loadFileX;
            return codecs.loadAsync(LeadStreamFactory.create(loadFile), 24, CodecsLoadByteOrder.bgrOrGray, 1, 1)
         })
         .then(function (img) {
            image = img;

            // Save this image and all the layers
            return Tools.AppLocalFolder().createFileAsync(destFileName)
         })
         .then(function (saveFileX) {
            saveFile = saveFileX;
            return codecs.savePsdWithLayersAsync(image, LeadStreamFactory.create(saveFile), 0, layersImage, null)
         })
         .then(function () {
            image.close();
            layersImage.close();

            return codecs.getInformationAsync(LeadStreamFactory.create(saveFile), false, 1)
         })
         .then(function (imageInfo) {
            if (imageInfo.psd.layers > 0) {
               var layer = 0;
               var layerInfo = new CodecsPsdLayerInfo();
               var async = codecs.loadPsdLayerAsync(LeadStreamFactory.create(saveFile), 0, CodecsLoadByteOrder.bgrOrGray, layer, layerInfo);

               return async.then(function (layerImage) {
                  var blendModeKeyBytes = layerInfo.getBlendModeKey();
                  var blendModeKey = decodeToUTF8(blendModeKeyBytes);//Custom function

                  console.info("Loaded layer at index " + layer + ", size is " + layerImage.width + " by " + layerImage.height + ", Blend mode key:" + blendModeKey);
                  console.info("TransparencyProtected is " + layerInfo.transparencyProtected + ", Visible is " + layerInfo.visible + ", Obsolete is " + layerInfo.obsolete + " and Psd5OrLater is ", layerInfo.Psd5OrLater);
                  layerImage.close();
                  // Clean up
                  codecs.close();
               });
            }
            else
               console.info("No layers found in this PSD file");


         });
      }
   }
}
[TestMethod]
public async Task PsdLayersExample()
{
   RasterCodecs codecs = new RasterCodecs();
   string[] layerFileNames =
     {
        @"Assets\Sample1.cmp",
        @"Assets\Sample2.cmp",
        @"Assets\Sample3.cmp",
     };

   string srcFileName = @"Assets\Image1.cmp";
   string destFileName = "Image1.psd";

   // Load the layer images (as pages in 1 image)
   RasterImage layersImage = null;
   StorageFile loadFile = null;
   foreach (string layerFileName in layerFileNames)
   {
      loadFile = await Tools.AppInstallFolder.GetFileAsync(layerFileName);
      RasterImage layerImage = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile));
      if (layersImage == null)
         layersImage = layerImage;
      else
         layersImage.AddPage(layerImage);
   }

   // Load the image that is made up of all the layers
   loadFile = await Tools.AppInstallFolder.GetFileAsync(srcFileName);
   RasterImage image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile), 24, CodecsLoadByteOrder.BgrOrGray, 1, 1);

   // Save this image and all the layers
   StorageFile saveFile = await Tools.AppLocalFolder.CreateFileAsync(destFileName);
   await codecs.SavePsdWithLayersAsync(image, LeadStreamFactory.Create(saveFile), 0, layersImage, null);
   image.Dispose();
   layersImage.Dispose();

   CodecsImageInfo imageInfo = await codecs.GetInformationAsync(LeadStreamFactory.Create(saveFile), false, 1);
   if (imageInfo.Psd.Layers > 0)
   {
      int layer = 0;
      CodecsPsdLayerInfo layerInfo = new CodecsPsdLayerInfo();
      RasterImage layerImage = await codecs.LoadPsdLayerAsync(LeadStreamFactory.Create(saveFile), 0, CodecsLoadByteOrder.BgrOrGray, layer, layerInfo);

      byte[] blendModeKeyBytes = layerInfo.GetBlendModeKey();
      string blendModeKey = Encoding.UTF8.GetString(blendModeKeyBytes, 0, blendModeKeyBytes.Length);

      Debug.WriteLine("Loaded layer at index {0}, size is {1} by {2}, Blend mode key:{3}", layer, layerImage.Width, layerImage.Height, blendModeKey);
      Debug.WriteLine("TransparencyProtected is {0}, Visible is {1}, Obsolete is {2} and Psd5OrLater is {3}",
         layerInfo.TransparencyProtected, layerInfo.Visible, layerInfo.Obsolete, layerInfo.Psd5OrLater);
      layerImage.Dispose();
   }
   else
      Debug.WriteLine("No layers found in this PSD file");

   // Clean up
   codecs.Dispose();
}
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

RasterCodecs Class
RasterCodecs Members

 

 


Products | Support | Contact Us | Copyright Notices

© 2006-2012 All Rights Reserved. LEAD Technologies, Inc.