翻譯|使用教程|編輯:胡濤|2022-09-21 11:05:04.190|閱讀 574 次
概述:在本文中,我們將學習如何使用 C# 在 PSD 中添加新圖層。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.PSD for .NET 是高級PSD文件格式操作API,沒有任何Adobe Photoshop依賴項。API允許創建或編輯Photoshop文件,并提供更新圖層屬性,添加水印,執行圖形操作或將一種文件格式轉換為另一種文件的功能。
Aspose.PSD for .NET支持PSD和PSB文件格式進行加載和處理,并允許導出為各種光柵圖像格式,例如TIFF,JPEG,PNG,GIF,BMP等。
PSD(Photoshop 文檔)是 Adobe 流行的 Photoshop 應用程序使用的原生圖像文件格式。它通常用于創建 PSD 文件包含多個圖層的徽標、小冊子和其他圖像。在某些情況下,我們可能需要以編程方式操作 PSD 文件。在本文中,我們將學習如何使用 C# 在 PSD 中添加新圖層。
要在 PSD 文件中創建新層,我們將使用Aspose.PSD for .NET API。它支持創建、編輯或操作 PSD 和其他幾種受支持的文件格式。
API的PsdImage類允許加載、編輯和保存 PSD 文件。此類的AddRegularLayer()方法添加了一個新的常規層。而AddTextLayer(string, Rectangle)方法將一個新的文本圖層添加到 PSD。此類還提供將 PSD 保存到指定文件位置的Save(string)方法。
請下載 API 的 DLL或使用NuGet安裝它。
PM> Install-Package Aspose.PSD
我們可以按照以下步驟向 PSD 文件添加新圖層:
以下代碼示例顯示了如何在 C# 中向 PSD 文件添加新圖層。
// This code example demonstrates how to create new layers in PSD image file.
string sourceFileName = @"C:\Files\PSD\OneLayer.psd";
string exportPath = @"C:\Files\PSD\OneLayerEdited.psd";
// Load an existing PSD
var im = (PsdImage)Image.Load(sourceFileName);
// Preparing two int arrays
var data1 = new int[2500];
var data2 = new int[2500];
// Define rectangles
var rect1 = new Rectangle(0, 0, 50, 50);
var rect2 = new Rectangle(0, 0, 100, 25);
for (int i = 0; i < 2500; i++)
{
data1[i] = -10000000;
data2[i] = -10000000;
}
// Add Layer 1
var layer1 = im.AddRegularLayer();
layer1.Left = 25;
layer1.Top = 25;
layer1.Right = 75;
layer1.Bottom = 75;
layer1.SaveArgb32Pixels(rect1, data1);
// Add Layer 2
var layer2 = im.AddRegularLayer();
layer2.Left = 25;
layer2.Top = 150;
layer2.Right = 125;
layer2.Bottom = 175;
layer2.SaveArgb32Pixels(rect2, data2);
// Save PSD
im.Save(exportPath, new PsdOptions());
我們可以按照以下步驟將新的文本圖層添加到 PSD 文件中:
以下代碼示例展示了如何在 C# 中向 PSD 文件添加新的文本層。
// This code example demonstrates how to create new layers in PSD image file.
string sourceFileName = @"C:\Files\PSD\OneLayer.psd";
string exportPath = @"C:\Files\PSD\TextLayer.psd";
// Load an existing PSD
var im = (PsdImage)Image.Load(sourceFileName);
// Add a text layer
var layer1 = im.AddTextLayer("Hello", new Rectangle(0, 0, 120, 50));
layer1.Left = 10;
layer1.Top = 25;
// Save PSD
im.Save(exportPath, new PsdOptions());
我們還可以按照以下步驟將 PNG 或 JPG 圖像中的新圖層添加到 PSD 文件中:
以下代碼示例展示了如何將新圖層從 PNG 圖像添加到 C# 中的 PSD 文件。
// This code example demonstrates how to create new layer from a PNG image in PSD image file.
string outputFilePath = @"C:\Files\PSD\PsdResult.psd";
// Create a new PSD image
var image = new PsdImage(200, 200);
// Load a PNG image
string filePath = @"C:\Files\PSD\aspose_logo.png";
var stream = new FileStream(filePath, FileMode.Open);
Layer layer = null;
try
{
// Add layer to PSD
layer = new Layer(stream);
image.AddLayer(layer);
}
catch (Exception e)
{
if (layer != null)
{
layer.Dispose();
}
throw e;
}
// Save PSD
image.Save(outputFilePath);
以上便是如何使用通過aspose.psd C# 在 PSD 中添加新圖層,希望能對您有所幫助,如果您還有其他疑問,歡迎查閱本系列其他教程,或者私信我們獲取幫助~
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn