Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active August 11, 2022 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/fd8505559a45a2590d2b6180a4aa0583 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/fd8505559a45a2590d2b6180a4aa0583 to your computer and use it in GitHub Desktop.
Create a New Layer in PSD Programmatically using C#
// This code example demonstrates how to create new layer from a PNG image in PSD image file.
string outputFilePath = @"C:\Files\PSD\PsdResult.psd";
// Create a new PSD image
var image = new PsdImage(200, 200);
// Load a PNG image
string filePath = @"C:\Files\PSD\aspose_logo.png";
var stream = new FileStream(filePath, FileMode.Open);
Layer layer = null;
try
{
// Add layer to PSD
layer = new Layer(stream);
image.AddLayer(layer);
}
catch (Exception e)
{
if (layer != null)
{
layer.Dispose();
}
throw e;
}
// Save PSD
image.Save(outputFilePath);
// This code example demonstrates how to create new layers in PSD image file.
string sourceFileName = @"C:\Files\PSD\OneLayer.psd";
string exportPath = @"C:\Files\PSD\OneLayerEdited.psd";
// Load an existing PSD
var im = (PsdImage)Image.Load(sourceFileName);
// Preparing two int arrays
var data1 = new int[2500];
var data2 = new int[2500];
// Define rectangles
var rect1 = new Rectangle(0, 0, 50, 50);
var rect2 = new Rectangle(0, 0, 100, 25);
for (int i = 0; i < 2500; i++)
{
data1[i] = -10000000;
data2[i] = -10000000;
}
// Add Layer 1
var layer1 = im.AddRegularLayer();
layer1.Left = 25;
layer1.Top = 25;
layer1.Right = 75;
layer1.Bottom = 75;
layer1.SaveArgb32Pixels(rect1, data1);
// Add Layer 2
var layer2 = im.AddRegularLayer();
layer2.Left = 25;
layer2.Top = 150;
layer2.Right = 125;
layer2.Bottom = 175;
layer2.SaveArgb32Pixels(rect2, data2);
// Save PSD
im.Save(exportPath, new PsdOptions());
// This code example demonstrates how to create new layers in PSD image file.
string sourceFileName = @"C:\Files\PSD\OneLayer.psd";
string exportPath = @"C:\Files\PSD\TextLayer.psd";
// Load an existing PSD
var im = (PsdImage)Image.Load(sourceFileName);
// Add a text layer
var layer1 = im.AddTextLayer("Hello", new Rectangle(0, 0, 120, 50));
layer1.Left = 10;
layer1.Top = 25;
// Save PSD
im.Save(exportPath, new PsdOptions());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment