Created
June 8, 2020 17:11
-
-
Save aspose-com-gists/15000fd8740fd1f404dce8a1276fe94d to your computer and use it in GitHub Desktop.
Update Text and Image Layers in PSD Files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
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
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,