翻譯|使用教程|編輯:李顯亮|2020-06-09 10:57:57.110|閱讀 1660 次
概述:為了使這一過(guò)程自動(dòng)化,在本文中,將學(xué)習(xí)如何使用C#.NET在Photoshop的PSD文件中動(dòng)態(tài)查找圖層和更新圖層的文本或圖像。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷售中 >>
PSD是Adobe Photoshop用于將其文檔保存為多層結(jié)構(gòu)的默認(rèn)格式,其中每一層都包含文本,圖像,圖形對(duì)象和其他受支持的元素。在某些情況下,當(dāng)您擁有PSD模板時(shí),需要通過(guò)填充模板中的文本和圖像層來(lái)創(chuàng)建多個(gè)結(jié)果圖像。
這種情況的一個(gè)示例是在組織內(nèi)創(chuàng)建員工卡。為了使這一過(guò)程自動(dòng)化,在本文中,將學(xué)習(xí)如何使用C#.NET在Photoshop的PSD文件中動(dòng)態(tài)查找圖層和更新圖層的文本或圖像。將展示如何:
Aspose.PSD for .NET已升級(jí)至V20.5,如果你還沒(méi)有用過(guò)Aspose.PSD可以點(diǎn)擊這里下載最新版測(cè)試。
用于.NET的Aspose.PSD允許您使用圖層的名稱在PSD文件中找到所需的圖層。找到圖層后,即可更新其內(nèi)容。以下是在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文件的屏幕截圖:
以下是具有更新的文本層的結(jié)果PSD文件:
為了處理圖像等圖形對(duì)象,用于.NET的Aspose.PSD公開(kāi)了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文件的屏幕截圖:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn