Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.