Warstwa PSD

Przegląd

Warstwa PSD programu Adobe® Photoshop® to jedna z najlepszych koncepcji w przetwarzaniu grafiki. Warstwy zawierają informacje o pikselach, mogą mieć różną liczbę kanałów.

Jedną z najważniejszych części Warstwy w Dokumencie Photoshopa jest Zasoby Warstw. Znajdziesz pełną listę obsługiwanych Zasobów Warstw w formacie PSD w naszej dokumentacji.

Możesz znaleźć interfejs użytkownika do manipulowania warstwą na poniższym zrzucie ekranu:

todo:image_alt_text

Ale Aspose.PSD specjalizuje się w programowej manipulacji Warstwy PSD za pośrednictwem C# i Java.

Dodatkową dokumentację można znaleźć w tym artykule: Manipulowanie obrazami. Cała manipulacja może być przetwarzana na Podgląd i Warstwę PSD, więcej informacji znajdziesz w Aspose.PSD Raster Image API Reference.

Dostępny interfejs API Warstwy PSD

  • Efekty Warstwy
  • Właściwości wspólne warstw
  • Metadane warstwy

Przykłady edycji warstwy za pomocą C#

Dodawanie nowej warstwy

Jeśli chcesz dodać pustą Warstwę do otwartego Pliku PSD możesz skorzystać z poniższego kodu.

Dodaj nową Warstwę do Pliku PSD za pomocą interfejsu API

// 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());
}

Dodawanie nowej Warstwy z plików Jpeg, Png, Gif, Ai, Tiff, Bmp

Pliki dowolnych obsługiwanych formatów mogą zostać dodane jako nowa warstwa do Twojego obrazu. Ale nie można go załadować bezpośrednio.

Możesz skorzystać z poniższego kodu, aby dodać nową Warstwę PSD z pliku dowolnego obsługiwanego formatu ze strumienia

// 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);
}

Spłaszczanie wszystkich warstw lub grup warstw

Może to być przydatne, jeśli nie chcesz udostępniać edytowalnego pliku PSD użytkownikom. Ponadto, można zidentyfikować poprzez interfejs API, czy plik został spłaszczony.

Spłaszczanie Warstw Pliku 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);
}