Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created June 8, 2020 17:11
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/15000fd8740fd1f404dce8a1276fe94d to your computer and use it in GitHub Desktop.
Save aspose-com-gists/15000fd8740fd1f404dce8a1276fe94d to your computer and use it in GitHub Desktop.
Update Text and Image Layers in PSD Files
// Load PSD file
using (PsdImage image = (PsdImage)Image.Load(@"updated-psd.psd"))
{
// Let's find layer that we want to replace
var layerToReplace = FindLayer("ProfilePicture", image);
using (Stream stream = new FileStream(@"avatar.png", FileMode.Open))
{
var newLayer = new Layer(stream);
// Drawing of new layer on the old
var graphic = new Graphics(layerToReplace);
graphic.Clear(Color.Empty);
graphic.DrawImage(newLayer, new Rectangle(new Point(), new Size(layerToReplace.Width, layerToReplace.Height)));
}
// Save the updated PSD file
image.Save("updated-psd2.psd");
}
//-------------------FindLayer()-------------
public static Layer FindLayer(string layerName, PsdImage image)
{
// Get aa layers in PSD file
var layers = image.Layers;
// Find desired layer
foreach (var layer in layers)
{
// Match layer's name
if (string.Equals(layer.DisplayName, layerName, StringComparison.InvariantCultureIgnoreCase))
{
return layer;
}
}
return null;
}
// Load PSD file
using (PsdImage image = (PsdImage)Image.Load(@"template.psd"))
{
// Find Layer using layer's name
var layerToUpdateText = (TextLayer)FindLayer("Name", image);
// Simple way to update text
layerToUpdateText.UpdateText("John Doe");
// Save the updated PSD file
image.Save("updated-psd.psd");
}
//-------------------FindLayer()-------------
public static Layer FindLayer(string layerName, PsdImage image)
{
// Get aa layers in PSD file
var layers = image.Layers;
// Find desired layer
foreach (var layer in layers)
{
// Match layer's name
if (string.Equals(layer.DisplayName, layerName, StringComparison.InvariantCultureIgnoreCase))
{
return layer;
}
}
return null;
}
@dvcuong05
Copy link

dvcuong05 commented May 15, 2021

Hi There,
Do you know how to replace smartObjectLayer image content with another image content and keep transform (curved, rotate,...)?
I can replace smartLayer image by another image with all the format/transform is lost.. it just put new image content in the right position on PSD file.
thank you,

@shahzad-latif
Copy link

@dvcuong05

Please post this query in our free Aspose.PSD forum so our team can help and guide you appropriately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment