PSD 图层
概述
Adobe® Photoshop® PSD 图层是图形处理中最好的概念之一。图层包含有关像素的信息,可以有不同数量的通道。
在 Photoshop 文档中,图层资源是图层的一个最重要部分。您可以在我们的文档中找到受支持的图层资源的完整列表。
您可以在以下截屏中找到用于操纵图层的用户界面:
但是,Aspose.PSD专注于通过C#和Java对 PSD 图层进行编程操纵。
附加文档可以在本文中找到:操纵图像。所有操作可以在 PSD 预览和图层中进行处理,您可以在 Aspose.PSD 位图图像 API 参考中找到更多信息。
可用的 PSD 图层 API
- 图层效果
- 图层常见属性
- 图层元数据
通过 C# 编辑图层的示例
添加新图层
如果您想要在打开的PSD 文件中添加一个空白图层,可以使用以下代码。
使用 API 向 PSD 文件添加新图层
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Make ability to add the newly generated regular layer to PsdImage | |
string sourceFileName = dataDir + "OneLayer.psd"; | |
string exportPath = dataDir + "OneLayerEdited.psd"; | |
string exportPathPng = dataDir + "OneLayerEdited.png"; | |
using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
// Preparing two int arrays | |
var data1 = new int[2500]; | |
var data2 = new int[2500]; | |
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; | |
} | |
var layer1 = im.AddRegularLayer(); | |
layer1.Left = 25; | |
layer1.Top = 25; | |
layer1.Right = 75; | |
layer1.Bottom = 75; | |
layer1.SaveArgb32Pixels(rect1, data1); | |
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()); | |
// Save png | |
im.Save(exportPathPng, new PngOptions()); | |
} |
从 Jpeg、Png、Gif、Ai、Tiff、Bmp 文件添加新图层
任何支持的格式的文件都可以作为一个新图层添加到您的图像中。但您无法直接加载它。
您可以使用下面的代码从流中添加新的 PSD 图层,该流是来自任何受支持格式的文件
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string outputFilePath = dataDir + "PsdResult.psd"; | |
var filesList = new string[] | |
{ | |
"PsdExample.psd", | |
"BmpExample.bmp", | |
"GifExample.gif", | |
"Jpeg2000Example.jpf", | |
"JpegExample.jpg", | |
"PngExample.png", | |
"TiffExample.tif", | |
}; | |
using (var image = new PsdImage(200, 200)) | |
{ | |
foreach (var fileName in filesList) | |
{ | |
string filePath = dataDir + fileName; | |
using (var stream = new FileStream(filePath, FileMode.Open)) | |
{ | |
Layer layer = null; | |
try | |
{ | |
layer = new Layer(stream); | |
image.AddLayer(layer); | |
} | |
catch (Exception e) | |
{ | |
if (layer != null) | |
{ | |
layer.Dispose(); | |
} | |
throw e; | |
} | |
} | |
} | |
image.Save(outputFilePath); | |
} |
合并所有图层或图层组
如果您不希望向用户提供可编辑的 PSD 文件,这将非常有用。此外,您可以通过 API 识别文件是否已被合并。
PSD 文件的图层合并:
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Flatten whole PSD | |
string sourceFileName = dataDir + "ThreeRegularLayersSemiTransparent.psd"; | |
string exportPath = dataDir + "ThreeRegularLayersSemiTransparentFlattened.psd"; | |
using (var im = (PsdImage)(Image.Load(sourceFileName))) | |
{ | |
im.FlattenImage(); | |
im.Save(exportPath); | |
} | |
// Merge one layer in another | |
exportPath = dataDir + "ThreeRegularLayersSemiTransparentFlattenedLayerByLayer.psd"; | |
using (var im = (PsdImage)(Image.Load(sourceFileName))) | |
{ | |
var bottomLayer = im.Layers[0]; | |
var middleLayer = im.Layers[1]; | |
var topLayer = im.Layers[2]; | |
var layer1 = im.MergeLayers(bottomLayer, middleLayer); | |
var layer2 = im.MergeLayers(layer1, topLayer); | |
// Set up merged layers | |
im.Layers = new Layer[] { layer2 }; | |
im.Save(exportPath); | |
} |