翻譯|使用教程|編輯:李顯亮|2020-06-09 10:57:57.110|閱讀 1660 次
概述:為了使這一過程自動化,在本文中,將學習如何使用C#.NET在Photoshop的PSD文件中動態查找圖層和更新圖層的文本或圖像。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
PSD是Adobe Photoshop用于將其文檔保存為多層結構的默認格式,其中每一層都包含文本,圖像,圖形對象和其他受支持的元素。在某些情況下,當您擁有PSD模板時,需要通過填充模板中的文本和圖像層來創建多個結果圖像。
這種情況的一個示例是在組織內創建員工卡。為了使這一過程自動化,在本文中,將學習如何使用C#.NET在Photoshop的PSD文件中動態查找圖層和更新圖層的文本或圖像。將展示如何:
Aspose.PSD for .NET已升級至V20.5,如果你還沒有用過Aspose.PSD可以點擊這里下載最新版測試。
用于.NET的Aspose.PSD允許您使用圖層的名稱在PSD文件中找到所需的圖層。找到圖層后,即可更新其內容。以下是在PSD文件中查找和更新文本層的步驟。
下面的代碼示例演示如何使用C#查找和更新PSD文件中的文本層。
// 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; }
下面是在此示例中使用的輸入PSD文件的屏幕截圖:
以下是具有更新的文本層的結果PSD文件:
為了處理圖像等圖形對象,用于.NET的Aspose.PSD公開了Graphics類。此類用于清除或繪制PSD層中的圖形。以下是查找PSD圖層并更新其圖像的步驟。
下面的代碼示例演示如何使用C#查找和更新PSD文件中的圖像層。
// 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; }
以下是使用上述代碼更新圖像層后的PSD文件的屏幕截圖:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn