操作Adobe Photoshop格式
将PSD图层合并到其他图层中
将图像导出为PSD
PSD,即PhotoShop文档,是Adobe Photoshop用于处理图像的默认文件格式。Aspose.PSD允许您加载、编辑和保存PSD文件,以便可以在Photoshop中打开和编辑这些文件。本文展示了如何使用Aspose.PSD将文件保存为PSD,并讨论了保存到此格式时可以使用的一些设置。PsdOptions是ImageOptions命名空间中的专用类,用于将图像导出为PSD。要导出到PSD,创建Image类的实例,可以从现有图像文件(例如缩略图)加载,也可以从头开始创建。本文解释了如何操作。在下面的示例中,图像是从头开始创建的。创建并填充像素数据后,使用Image类的Save方法保存图像,并将PsdOptions对象作为第二个参数。可以为高级转换设置PsdOptions类的一些属性。其中一些属性是ColorMode,CompressionMethod和Version。Aspose.PSD通过CompressionMethod枚举支持以下压缩方法:
- CompressionMethod.Raw
- CompressionMethod.RLE
- CompressionMethod.ZipWithoutProtection
- CompressionMethod.ZipWithProtection
通过ColorModes枚举支持以下颜色模式:
- ColorModes.Bitmap
- ColorModes.Grayscale
- ColorModes.RGB
可以添加其他资源,例如PSD v4.0、v5.0及更高版本的缩略图资源,或PSD v4.0及更高版本的网格和引导资源。以下代码通过从头创建一个Image文件,填充像素,并使用RLE压缩和灰度色彩模式将其保存为PSD。以下代码片段显示如何将图像导出为PSD。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Create a new image from scratch. | |
using (PsdImage image = new PsdImage(300, 300)) | |
{ | |
// Fill image data. | |
Graphics graphics = new Graphics(image); | |
graphics.Clear(Color.White); | |
var pen = new Pen(Color.Brown); | |
graphics.DrawRectangle(pen, image.Bounds); | |
// Create an instance of PsdOptions, Set it's various properties Save image to disk in PSD format | |
PsdOptions psdOptions = new PsdOptions(); | |
psdOptions.ColorMode = ColorModes.Rgb; | |
psdOptions.CompressionMethod = CompressionMethod.Raw; | |
psdOptions.Version = 4; | |
image.Save(dataDir + "ExportImageToPSD_output.psd", psdOptions); | |
} |
将图像导入到PSD图层
本文演示了使用Aspose.PSD将图像添加到或导入到PSD图层。Aspose.PSD API公开了高效且易于使用的方法来实现此目标。Aspose.PSD公开了Layer类的DrawImage方法,以将图像添加/导入到PSD文件中。DrawImage方法需要指定位置和图像值以将图像添加/导入到PSD文件中。将图像导入到PSD图层的步骤如下:
- 使用由Image类提供的工厂方法Load将PSD文件加载为图像。
- 从Png、Jpeg、Tiff、Gif、Bmp、Psd或j2k文件的流创建Layer类的实例
- 使用AddLayer方法将Layer添加到Psd
- 保存结果。
以下代码片段显示了如何将图像导入到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图层中替换颜色
本文演示了如何使用Aspose.PSD将图像添加到或导入到PSD图层。Aspose.PSD API公开了高效且易于使用的方法来实现此目标。Aspose.PSD公开了Layer类的DrawImage方法,以添加/导入图像到PSD文件。DrawImage方法需要指定位置和图像值以将图像添加/导入到PSD文件。将图像导入到PSD图层的步骤如下:
- 使用由Image类提供的工厂方法Load将PSD文件加载为图像。
- 创建Layer类的实例并将PSD图像图层分配给它。
- 加载需要添加的图像或从头开始创建一个图像。
- 调用Layer.DrawImage方法,同时指定位置和图像实例。
- 保存结果。
以下代码片段显示了如何将图像导入到PSD图层。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and caste it into PsdImage | |
using (PsdImage image = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
foreach (var layer in image.Layers) | |
{ | |
if (layer.Name == "Rectangle 1") | |
{ | |
layer.HasBackgroundColor = true; | |
layer.BackgroundColor = Color.Orange; | |
} | |
} | |
image.Save(dataDir + "asposeImage02.psd"); | |
} |
从PSD文件创建缩略图
PSD是Adobe Photoshop应用程序的本机文档格式。Adobe Photoshop(版本5.0及更高版本)会将缩略图信息存储在一个图像资源块中,该块由一个28字节的初始标头组成,后面跟随着RGB(红色、绿色、蓝色)顺序的JFIF缩略图。Aspose.PSD API提供了一种简单易用的机制来访问PSD文件的资源。这些资源还包括可以获取和根据应用程序需要保存到磁盘的缩略图资源。以下代码片段显示了如何从PSD文件创建缩略图。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and caste it into PsdImage | |
using (PsdImage image = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
int index = 0; | |
// Iterate over the PSD resources | |
foreach (var resource in image.ImageResources) | |
{ | |
index++; | |
// Check if the resource is of thumbnail type | |
if (resource is ThumbnailResource) | |
{ | |
// Retrieve the ThumbnailResource and Check the format of the ThumbnailResource | |
var thumbnail = (ThumbnailResource)resource; | |
if (thumbnail.Format == ThumbnailFormat.KJpegRgb) | |
{ | |
// Create a new image by specifying the width and height, Store the pixels of thumbnail on to the newly created image and save image | |
PsdImage thumnailImage = new PsdImage(thumbnail.Width, thumbnail.Height); | |
thumnailImage.SavePixels(thumnailImage.Bounds, thumbnail.ThumbnailData); | |
thumnailImage.Save(dataDir + "CreateThumbnailsFromPSDFiles_out_" + index.ToString() + ".bmp", new BmpOptions()); | |
} | |
} | |
} | |
} |
创建索引PSD文件
Aspose.PSD for .NET API可以从头开始创建索引PSD文件。本文演示了使用PsdOptions和PsdImage类来创建带有一些形状的索引PSD。创建索引PSD文件需要执行以下简单步骤:
- 创建PsdOptions的实例并设置其源。
- 将PsdOptions的ColorMode属性设置为ColorModes.Indexed。
- 从RGB空间创建新的颜色调色板,并将其设置为PsdOptions的Palette属性。
- 将CompressionMethod属性设置为所需的压缩算法。
- 通过调用PsdImage.Create方法创建一个新的PSD图像。
- 根据需求绘制图形或执行其他操作。
- 调用PsdImage.Save方法以提交所有更改。
以下代码片段显示了如何创建索引PSD文件。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Create an instance of PsdOptions and set it's properties | |
var createOptions = new PsdOptions(); | |
createOptions.Source = new FileCreateSource(dataDir + "Newsample_out.psd", false); | |
createOptions.ColorMode = ColorModes.Indexed; | |
createOptions.Version = 5; | |
// Create a new color palette having RGB colors, Set Palette property & compression method | |
Color[] palette = { Color.Red, Color.Green, Color.Blue, Color.Yellow }; | |
createOptions.Palette = new PsdColorPalette(palette); | |
createOptions.CompressionMethod = CompressionMethod.RLE; | |
// Create a new PSD with PsdOptions created previously | |
using (var psd = Image.Create(createOptions, 500, 500)) | |
{ | |
// Draw some graphics over the newly created PSD | |
var graphics = new Graphics(psd); | |
graphics.Clear(Color.White); | |
graphics.DrawEllipse(new Pen(Color.Red, 6), new Rectangle(0, 0, 400, 400)); | |
psd.Save(); | |
} |
将PSD图层导出为光栅图像
Aspose.PSD for .NET允许您将PSD文件中的图层导出为光栅图像。请使用Aspose.PSD.FileFormats.Psd.Layers.Layer.Save方法将图层导出到图像。以下示例代码加载一个PSD文件,并使用Aspose.PSD.FileFormats.Psd.Layers.Layer.Save将其每个图层导出为PNG图像。一旦所有图层都导出为PNG图像,您可以使用您喜欢的图像查看器打开它们。以下代码片段显示了如何将PSD图层导出为光栅图像。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and caste it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
// Create an instance of PngOptions class | |
var pngOptions = new PngOptions(); | |
pngOptions.ColorType = PngColorType.TruecolorWithAlpha; | |
// Loop through the list of layers | |
for (int i = 0; i < psdImage.Layers.Length; i++) | |
{ | |
// Convert and save the layer to PNG file format. | |
psdImage.Layers[i].Save(string.Format("layer_out{0}.png", i + 1), pngOptions); | |
} | |
} |
更新PSD文件中的文本图层
Aspose.PSD for .NET允许您操作PSD文件中文本图层中的文本。请使用Aspose.PSD.FileFormats.Psd.Layers.TextLayer类更新PSD图层中的文本。以下代码片段加载一个PSD文件,访问文本图层,更新文本,然后使用Aspose.PSD.FileFormats.Psd.Layers.TextLayer.UpdateText方法将PSD文件另存为新名称。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "layers.psd")) | |
{ | |
foreach (var layer in psdImage.Layers) | |
{ | |
if (layer is TextLayer) | |
{ | |
TextLayer textLayer = layer as TextLayer; | |
textLayer.UpdateText("test update", new Point(0, 0), 15.0f, Color.Purple); | |
} | |
} | |
psdImage.Save(dataDir + "UpdateTextLayerInPSDFile_out.psd"); | |
} |
检测合并的PSD
Aspose.PSD for .NET允许您检测给定的PSD文件是否已合并。Aspose.PSD.FileFormats.Psd.PsdImage类引入了IsFlatten属性以实现此功能。以下代码片段加载了一个PSD文件,并访问IsFlatten属性。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "layers.psd")) | |
{ | |
// Do processing, Get the true value if PSD is flatten and false in case the PSD is not flatten. | |
Console.WriteLine(psdImage.IsFlatten); | |
} |
合并PSD图层
本文展示了如何在转换PSD文件为JPG时将图层合并到PSD文件中。在下面的示例中,通过将文件路径传递给Image类的静态Load方法加载现有的PSD文件。加载后,将图像转换/转换为PsdImage。创建一个JpegOptions类的实例,并设置其各种属性。现在调用PsdImage实例的Save方法。以下代码片段显示了如何合并PSD图层。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "layers.psd")) | |
{ | |
// Create JPEG option class object, Set its properties and save image | |
var jpgOptions = new JpegOptions(); | |
psdImage.Save(dataDir + "MergePSDlayers_output.jpg", jpgOptions); | |
} |
支持带Alpha的PSD灰度
本文展示了如何在将PSD文件转换为PNG时,支持PSD文件中具有Alpha的灰度。在下面的示例中,通过将文件路径传递给Image类的静态Load方法加载现有的PSD文件。加载后,将图像转换/转换为PsdImage。创建一个PngOptions类的实例,并设置其各种属性。将ColorType属性设置为TruecolorWithAlpha。现在调用PngOptions实例的Save方法。以下代码片段显示了如何转换为带Alpha的灰度PNG。
支持PSD图层效果
本文展示了如何支持PSD图层中的不同效果,如模糊、内部 发光、外部发光。以下代码片段显示了如何支持图层效果。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string sourceFileName = dataDir + "layers.psd"; | |
string output = dataDir + "layers.png"; | |
using (PsdImage image = (PsdImage)Image.Load(sourceFileName, | |
new ImageLoadOptions.PsdLoadOptions() | |
{ | |
LoadEffectsResource = true, | |
UseDiskForLoadEffectsResource = true | |
})) | |
{ | |
Debug.Assert(image.Layers[2] != null, "Layer with effects resource was not recognized"); | |
image.Save(output, new ImageOptions.PngOptions() | |
{ | |
ColorType = | |
FileFormats.Png | |
.PngColorType | |
.TruecolorWithAlpha | |
}); | |
} |
支持图层创建日期和时间
本文展示了如何支持PSD图层的创建日期以及时间。以下代码片段显示了如何创建图层。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string SourceName = dataDir + "OneLayer.psd"; | |
// Load a PSD file as an image and cast it into PsdImage | |
using (var im = (PsdImage)(Image.Load(SourceName))) | |
{ | |
var layer = im.Layers[0]; | |
var creationDateTime = layer.LayerCreationDateTime; | |
var expectedDateTime = new DateTime(2018, 7, 17, 8, 57, 24, 769); | |
if (expectedDateTime != creationDateTime) | |
{ | |
throw new Exception("Assertion fails"); | |
} | |
var now = DateTime.Now; | |
var createdLayer = im.AddLevelsAdjustmentLayer(); | |
// Check if Creation Date Time Updated on newly created layers | |
if (!(now <= createdLayer.LayerCreationDateTime)) | |
{ | |
throw new Exception("Assertion fails"); | |
} | |
} |
支持表色高亮
本文展示了如何加载PSD图像,然后更改和突出显示表颜色,将其保存为图像。已提供代码片段。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string exportPath = dataDir + "SheetColorHighlightExampleChanged.psd"; | |
// Load a PSD file as an image and cast it into PsdImage | |
using (var im = (PsdImage)(Image.Load(sourceFileName))) | |
{ | |
var layer1 = im.Layers[0]; | |
var layer2 = im.Layers[1]; | |
if (layer1.SheetColorHighlight != SheetColorHighlightEnum.Violet || | |
layer2.SheetColorHighlight != SheetColorHighlightEnum.Orange) | |
{ | |
throw new Exception("Assertion failed"); | |
} | |
layer1.SheetColorHighlight = SheetColorHighlightEnum.Yellow; | |
im.Save(exportPath); | |
} |
支持图层蒙版
本文展示了如何支持PSD图像的图层蒙版,然后保存这些图像。以下代码片段已提供。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Export of the psd with complex mask | |
string sourceFileName = dataDir + "MaskComplex.psd"; | |
string exportPath = dataDir + "MaskComplex.png"; | |
using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
// Export to PNG | |
var saveOptions = new PngOptions(); | |
saveOptions.ColorType = PngColorType.TruecolorWithAlpha; | |
im.Save(exportPath, saveOptions); | |
} |
支持图层矢量蒙版
本文展示了Aspose.PSD对图层矢量蒙版的支持。以下代码片段显示了Aspose.PSD如何支持图层矢量蒙版。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string exportPath = dataDir + "DifferentLayerMasks_Export.psd"; | |
string exportPathPng = dataDir + "DifferentLayerMasks_Export.png"; | |
// reading | |
using (PsdImage image = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
// make changes to the vector path points | |
foreach (var layer in image.Layers) | |
{ | |
foreach (var layerResource in layer.Resources) | |
{ | |
var resource = layerResource as VectorPathDataResource; | |
if (resource != null) | |
{ | |
foreach (var pathRecord in resource.Paths) | |
{ | |
var bezierKnotRecord = pathRecord as BezierKnotRecord; | |
if (bezierKnotRecord != null) | |
{ | |
Point p0 = bezierKnotRecord.Points[0]; | |
bezierKnotRecord.Points[0] = bezierKnotRecord.Points[2]; | |
bezierKnotRecord.Points[2] = p0; | |
break; | |
} | |
} | |
} | |
} | |
} | |
// exporting | |
image.Save(exportPath); | |
image.Save(exportPathPng, new PngOptions() { ColorType = PngColorType.TruecolorWithAlpha }); | |
} |
运行时支持文本图层
本文展示了如何为PSD图像在运行时添加文本图层。以下代码片段已提供。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Add color overlay layer effect at runtime | |
string sourceFileName = dataDir + "ThreeRegularLayers.psd"; | |
string exportPath = dataDir + "ThreeRegularLayersChanged.psd"; | |
var loadOptions = new PsdLoadOptions() | |
{ | |
LoadEffectsResource = true | |
}; | |
using (var im = (PsdImage)Image.Load(sourceFileName, loadOptions)) | |
{ | |
var effect = im.Layers[1].BlendingOptions.AddColorOverlay(); | |
effect.Color = Color.Green; | |
effect.Opacity = 128; | |
effect.BlendMode = BlendMode.Normal; | |
im.Save(exportPath); | |
} |
调整图层支持
本文展示了如何调整图层,如果图层为空,则合并图层,并保存为PSD图像。以下代码片段已提供。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_PSD(); | |
// Channel Mixer Adjustment Layer | |
string sourceFileName1 = dataDir + "ChannelMixerAdjustmentLayer.psd"; | |
string exportPath1 = dataDir + "ChannelMixerAdjustmentLayerChanged.psd"; | |
using (var im = (PsdImage)(Image.Load(sourceFileName1))) | |
{ | |
for (int i = 0; i < im.Layers.Length; i++) | |
{ | |
var adjustmentLayer = im.Layers[i] as AdjustmentLayer; | |
if (adjustmentLayer != null) | |
{ | |
adjustmentLayer.MergeLayerTo(im.Layers[0]); | |
} | |
} | |
// Save PSD | |
im.Save(exportPath1); | |
} | |
// Levels adjustment layer | |
var sourceFileName2 =dataDir+ "LevelsAdjustmentLayerRgb.psd"; | |
var exportPath2 = dataDir + "LevelsAdjustmentLayerRgbChanged.psd"; | |
using (var im = (PsdImage)(Image.Load(sourceFileName2))) | |
{ | |
for (int i = 0; i < im.Layers.Length; i++) | |
{ | |
var adjustmentLayer = im.Layers[i] as AdjustmentLayer; | |
if (adjustmentLayer != null) | |
{ | |
adjustmentLayer.MergeLayerTo(im.Layers[0]); | |
} | |
} | |
// Save PSD | |
im.Save(exportPath2); | |
} |
管理亮度和对比度
本文展示了如何通过各图层循环遍历并管理亮度和对比度,然后将其保存为PSD图像。以下代码片段已提供。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Brightness/Contrast layer editing | |
string sourceFileName = dataDir + "BrightnessContrastModern.psd"; | |
string psdPathAfterChange = dataDir + "BrightnessContrastModernChanged.psd"; | |
using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
foreach (var layer in im.Layers) | |
{ | |
if (layer is BrightnessContrastLayer) | |
{ | |
var brightnessContrastLayer = (BrightnessContrastLayer)layer; | |
brightnessContrastLayer.Brightness = 50; | |
brightnessContrastLayer.Contrast = 50; | |
} | |
} | |
// Save PSD | |
im.Save(psdPathAfterChange); | |
} |
管理曝光图层
本文展示了如何添加和编辑曝光图层,管理曝光图层,然后将其保存为PSD图像。以下代码片段已提供。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Exposure layer editing | |
string sourceFileName = dataDir + "ExposureAdjustmentLayer.psd"; | |
string psdPathAfterChange = dataDir + "ExposureAdjustmentLayerChanged.psd"; | |
using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
foreach (var layer in im.Layers) | |
{ | |
if (layer is ExposureLayer) | |
{ | |
var expLayer = (ExposureLayer)layer; | |
expLayer.Exposure = 2; | |
expLayer.Offset = -0.25f; | |
expLayer.GammaCorrection = 0.5f; | |
} | |
} | |
im.Save(psdPathAfterChange); | |
} | |
// Exposure layer adding | |
sourceFileName = dataDir + "PhotoExample.psd"; | |
psdPathAfterChange = dataDir + "PhotoExampleAddedExposure.psd"; | |
using (PsdImage im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
var newlayer = im.AddExposureAdjustmentLayer(); | |
newlayer.Exposure = 10; | |
newlayer.Offset = -0.25f; | |
newlayer.GammaCorrection = 2f; | |
im.Save(psdPathAfterChange); | |
} |
管理通道混合器调整图层
本文展示了如何管理调整图层并设置不同颜色,然