Manipulating Adobe Photoshop Formats
Merge PSD layers into Other layers
Exporting Image to PSD
PSD, PhotoShop document, is the default file format used by Adobe Photoshop for working with images. Aspose.PSD allows you to load, edit and save files to PSD so that they can be opened and edited in Photoshop. This article shows how to save a file to PSD with Aspose.PSD, and it also discusses some of the settings that can be used when saving to this format. PsdOptions is a specialized class in the ImageOptions namespace used to export images to PSD. To export to PSD, create an instance of the Image class, either loaded from an existing image file (thumbnails for example) or created from scratch. This article explains how. In the examples below, an image is created from scratch. Once it is created and pixel data is populated, save the image using the Image class Save method, and supply a PsdOptions object as the second argument. Several of the PsdOptions class properties can be set for advanced conversion. Some of the properties are ColorMode, CompressionMethod and Version. Aspose.PSD supports the following compression methods through the CompressionMethod enumeration:
- CompressionMethod.Raw
- CompressionMethod.RLE
- CompressionMethod.ZipWithoutProtection
- CompressionMethod.ZipWithProtection
The following color modes are supported through the ColorModes enumeration:
- ColorModes.Bitmap
- ColorModes.Grayscale
- ColorModes.RGB
Additional resources can be added, such as thumbnail resources for PSD v4.0, v5.0 and higher, or grid and guide resources for PSD v4.0 and higher. The code below creates an Image file from scratch, populates the pixels and saves it to PSD with RLE compression and grayscale color mode. The following code snippet shows you how to export Image to 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); | |
} |
Importing image to PSD layer
This article demonstrates the usage of Aspose.PSD to add or import an image to a PSD layer. Aspose.PSD APIs have exposed efficient & easy to use methods to achieve this goal. Aspose.PSD has exposed the DrawImage method of the Layer class to add/import an image into a PSD file. DrawImage method needs location and image values to add/import an image into a PSD file. The steps to import an image into PSD layer are as simple as below:
- Load a PSD file as an image using the factory method Load exposed by Image class.
- Create an instance of Layer class from the stream with Png, Jpeg, Tiff, Gif, Bmp, Psd or j2k file
- Add Layer to Psd using AddLayer method
- Save the results.
The following code snippet shows you how to import the image to PSD layer.
// 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); | |
} |
Color replacement in PSD layers
This article demonstrates the usage of Aspose.PSD to add or import an image to a PSD layer. Aspose.PSD APIs have exposed efficient & easy to use methods to achieve this goal. Aspose.PSD has exposed the DrawImage method of the Layer class to add/import an image into a PSD file. DrawImage method needs location and image values to add/import an image into a PSD file. The steps to import an image into PSD layer are as simple as below:
- Load a PSD file as an image using the factory method Load exposed by Image class.
- Create an instance of Layer class and assign the PSD image layer to it.
- Load the image that needs to be added or create a one from scratch.
- Call the Layer.DrawImage method while specifying location and image instance.
- Save the results.
The following code snippet shows you how to import image to PSD layer.
// 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"); | |
} |
Creating Thumbnails from PSD Files
PSD is the native document format of Adobe’s Photoshop application. Adobe Photoshop (version 5.0 and later) stores thumbnail information for preview display in an image resource block that consists of an initial 28-byte header, followed by a JFIF thumbnail in RGB (red, green, blue) order. Aspose.PSD API provides an easy to use mechanism to access the resources of the PSD file. These resources also include the thumbnail resource that in turn can be fetched and saved to disc as per application needs. The following code snippet shows you how to create thumbnails from PSD Files.
// 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()); | |
} | |
} | |
} | |
} |
Creating Indexed PSD Files
Aspose.PSD for .NET API can create Indexed PSD files from scratch. This article demonstrates the use of PsdOptions and PsdImage classes to create an Indexed PSD while drawing some shapes over the newly created canvas. The following simple steps are required to create an Indexed PSD file.
- Create an instance of PsdOptions and set its source.
- Set ColorMode property of the PsdOptions to ColorModes.Indexed.
- Create a new palette of colors from RGB space and set it as Palette property of PsdOptions.
- Set CompressionMethod property to the required compression algorithm.
- Create a new PSD image by calling the PsdImage.Create method.
- Draw graphics or perform other operations as per requirement.
- Call PsdImage.Save method to commit all changes.
The following code snippet shows you how to create indexed PSD files.
// 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(); | |
} |
Exporting PSD Layer to Raster Image
Aspose.PSD for .NET allows you to export layers in a PSD file into raster images. Please use the Aspose.PSD.FileFormats.Psd.Layers.Layer.Save method to export the layer to the image. The following sample code loads a PSD file and exports each of its layers into a PNG image using Aspose.PSD.FileFormats.Psd.Layers.Layer.Save. Once all layers are exported into PNG images, you can open them using your favorite image viewer. The following code snippet shows you how to export PSD layer to a raster image.
// 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); | |
} | |
} |
Update Text Layer In PSD File
Aspose.PSD for .NET allows you to manipulate the text in the text layer of a PSD file. Please use the Aspose.PSD.FileFormats.Psd.Layers.TextLayer class to update text in the PSD layer. The following code snippet loads a PSD file, access the text layer, update the text and save the PSD file with a new name using Aspose.PSD.FileFormats.Psd.Layers.TextLayer.UpdateText method.
// 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"); | |
} |
Detecting Flattened PSD
Aspose.PSD for .NET allows you to detect whether a given PSD file is flattened or not. IsFlatten property has been introduced in the Aspose.PSD.FileFormats.Psd.PsdImage class to achieve this functionality. The following code snippet loads a PSD file and access the property 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); | |
} |
Merge PSD layers
This article shows how to merge layers in a PSD file while converting a PSD file to JPG with Aspose.PSD. In the example below, an existing PSD file is loaded by passing the file path to the Image class static Load method. Once it is loaded, convert/cast the image to PsdImage. Create an instance of the JpegOptions class and set it various properties. Now call the Save method of the PsdImage instance. The following code snippet shows you how to merge PSD layers.
// 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); | |
} |
Grayscale Support With Alpha for PSD
This article shows how to support Grayscale with alpha for PSD file while converting a PSD file to PNG with Aspose.PSD. In the example below, an existing PSD file is loaded by passing the file path to the Image class static Load method. Once it is loaded, convert/cast the image to PsdImage. Create an instance of the PngOptions class and set its various properties. Set the ColorType property to TruecolorWithAlpha. Now call the Save method of the PngOptions instance. The following code snippet shows you how to convert to Grayscale PNG with Alpha.
Support Layer Effects For PSD
This article shows how support different effects like Blur, Inner Glow, Outer Glow for PSD layer. The following code snippet shows you how to support layer effects.
// 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 | |
}); | |
} |
Support Layer Creation Date and Time
This article shows how to support date and time layer creation for PSD layer. The following code snippet shows you how to create layers.
// 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"); | |
} | |
} |
Support for Sheet Color High Lighting
This article shows how to load PSD images than change and highlight sheet colors and save that as an image. The code snippet has been provided.
// 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); | |
} |
Support of Layer Mask
This article shows how to support layer of mask for PSD images than save those images. The code snippet has been provided below.
// 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); | |
} |
Support of Layer Vector Mask
This article shows Support of layer vector masks for Aspose.PSD. The following code snippet shows you how Aspose.PSD supports layer vector masks.
// 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 }); | |
} |
Support of Text Layers on Runtime
This article shows how to add text layers on runtime for PSD images. The code snippet has been provided below.
// 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); | |
} |
Support of Adjustment Layers
This article shows how to adjust layers and then if layer is null than merge layer and save as PSD images. The code snippet has been provided below.
// 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); | |
} |
Manage Brightness and Contrast in Adjustment Layers
This article shows how to iterate through layers and manage brightness and contrast in layers and then save as PSD images. The code snippet has been provided below.
// 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); | |
} |
Manage Exposure Layers
This article shows how to add and edit exposure layers and manage exposure layers and then save as PSD images. The code snippet has been provided below.
// 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); | |
} |
Manage Channel Mixer Adjust Layers
This article shows how to manage adjustment layers and set different colors and then save as PSD images. The code snippet has been provided below.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Rgb Channel Mixer | |
string sourceFileName = dataDir + "ChannelMixerAdjustmentLayerRgb.psd"; | |
string psdPathAfterChange = dataDir + "ChannelMixerAdjustmentLayerRgbChanged.psd"; | |
using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
foreach (var layer in im.Layers) | |
{ | |
if (layer is RgbChannelMixerLayer) | |
{ | |
var rgbLayer = (RgbChannelMixerLayer)layer; | |
rgbLayer.RedChannel.Blue = 100; | |
rgbLayer.BlueChannel.Green = -100; | |
rgbLayer.GreenChannel.Constant = 50; | |
} | |
} | |
im.Save(psdPathAfterChange); | |
} | |
// Adding the new layer(Cmyk for this example) | |
sourceFileName = dataDir + "CmykWithAlpha.psd"; | |
psdPathAfterChange = dataDir + "ChannelMixerAdjustmentLayerCmykChanged.psd"; | |
using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
var newlayer = im.AddChannelMixerAdjustmentLayer(); | |
newlayer.GetChannelByIndex(2).Constant = 50; | |
newlayer.GetChannelByIndex(0).Constant = 50; | |
im.Save(psdPathAfterChange); | |
} |
Merge PSD layers into Other layers
This article shows how to merge layers into other layers in a PSD file with Aspose.PSD. In the example below, an existing PSD file is loaded by passing the file path to the Image class static Load method. Now call the Save method of the PsdImage instance. The following code snippet shows you how to merge PSD layers.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
var sourceFile1 = dataDir + "FillOpacitySample.psd"; | |
var sourceFile2 = dataDir + "ThreeRegularLayersSemiTransparent.psd"; | |
var exportPath = dataDir + "MergedLayersFromTwoDifferentPsd.psd"; | |
using (var im1 = (PsdImage)(Image.Load(sourceFile1))) | |
{ | |
var layer1 = im1.Layers[1]; | |
using (var im2 = (PsdImage)(Image.Load(sourceFile2))) | |
{ | |
var layer2 = im2.Layers[0]; | |
layer1.MergeLayerTo(layer2); | |
im2.Save(exportPath); | |
} | |
} |
Support For TextBoundBox
This article shows how to Support text bound box in a PSD file with Aspose.PSD. In the example below, an existing PSD file is loaded by passing the file path to the Image class static Load method. TextBoundBox is the maximum layer size for the Text layer. Now call the Save method of the PsdImage instance. The following code snippet shows you how to merge PSD layers.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string sourceFileName = dataDir + "LayerWithText.psd"; | |
var correctOpticalSize = new Size(127, 45); | |
var correctBoundBox = new Size(172, 62); | |
using (var im = (PsdImage)(Image.Load(sourceFileName))) | |
{ | |
var textLayer = (TextLayer)im.Layers[1]; | |
// Size of the layer is the size of the rendered area | |
var opticalSize = textLayer.Size; | |
// TextBoundBox is the maximum layer size for Text Layer. | |
// In this area PS will try to fit your text | |
var boundBox = textLayer.TextBoundBox; | |
if (opticalSize != correctOpticalSize || | |
boundBox.Size != correctBoundBox) | |
{ | |
throw new Exception("Assertion failed"); | |
} | |
} |
Support For IOPA Resources
This article shows how to Support Iopa resource in a PSD file with Aspose.PSD. The following code snippet shows you how to support Iopa in PSD layers.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string sourceFileName = dataDir + "FillOpacitySample.psd"; | |
string exportPath = dataDir + "FillOpacitySampleChanged.psd"; | |
using (var im = (PsdImage)(Image.Load(sourceFileName))) | |
{ | |
var layer = im.Layers[2]; | |
var resources = layer.Resources; | |
foreach (var resource in resources) | |
{ | |
if (resource is FileFormats.Psd.Layers.LayerResources.IopaResource) | |
{ | |
var iopaResource = (IopaResource)resource; | |
iopaResource.FillOpacity = 200; | |
} | |
} | |
im.Save(exportPath); | |
} |
Manage Opacity of Layers
This article shows how to manage opacity of layers in a PSD file with Aspose.PSD. The code snippet has been provided below.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Change the Fill Opacity property | |
string sourceFileName = dataDir + "FillOpacitySample.psd"; | |
string exportPath = dataDir + "FillOpacitySampleChanged.psd"; | |
using (var im = (PsdImage)(Image.Load(sourceFileName))) | |
{ | |
var layer = im.Layers[2]; | |
layer.FillOpacity = 5; | |
im.Save(exportPath); | |
} |
Rendering of Curves Adjustment Layers
This article shows how to rendered curves adjust of layers in a PSD file and render them into PNG as well with Aspose.PSD. The code snippet has been provided below.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Curves layer editing | |
string sourceFileName = dataDir + "CurvesAdjustmentLayer"; | |
string psdPathAfterChange = "CurvesAdjustmentLayerChanged"; | |
string pngExportPath = "CurvesAdjustmentLayerChanged"; | |
for (int j = 1; j < 2; j++) | |
{ | |
using (var im = (PsdImage)Image.Load(sourceFileName + j.ToString() + ".psd")) | |
{ | |
foreach (var layer in im.Layers) | |
{ | |
if (layer is CurvesLayer) | |
{ | |
var curvesLayer = (CurvesLayer)layer; | |
if (curvesLayer.IsDiscreteManagerUsed) | |
{ | |
var manager = (CurvesDiscreteManager)curvesLayer.GetCurvesManager(); | |
for (int i = 10; i < 50; i++) | |
{ | |
manager.SetValueInPosition(0, (byte)i, (byte)(15 + (i * 2))); | |
} | |
} | |
else | |
{ | |
var manager = (CurvesContinuousManager)curvesLayer.GetCurvesManager(); | |
manager.AddCurvePoint(0, 50, 100); | |
manager.AddCurvePoint(0, 150, 130); | |
} | |
} | |
} | |
// Save PSD | |
im.Save(psdPathAfterChange + j.ToString() + ".psd"); | |
// Save PNG | |
var saveOptions = new PngOptions(); | |
saveOptions.ColorType = PngColorType.TruecolorWithAlpha; | |
im.Save(pngExportPath + j.ToString() + ".png", saveOptions); | |
} | |
} |
Add Curves Adjustment Layers
This article shows how to add curves adjustment layers in a PSD file with Aspose.PSD. The code snippet has been provided below.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Curves layer editing | |
string sourceFileName = dataDir + "CurvesAdjustmentLayer"; | |
string psdPathAfterChange = dataDir + "CurvesAdjustmentLayerChanged"; | |
for (int j = 1; j < 2; j++) | |
{ | |
using (var im = (PsdImage)Image.Load(sourceFileName + j.ToString() + ".psd")) | |
{ | |
foreach (var layer in im.Layers) | |
{ | |
if (layer is CurvesLayer) | |
{ | |
var curvesLayer = (CurvesLayer)layer; | |
if (curvesLayer.IsDiscreteManagerUsed) | |
{ | |
var manager = (CurvesDiscreteManager)curvesLayer.GetCurvesManager(); | |
for (int i = 10; i < 50; i++) | |
{ | |
manager.SetValueInPosition(0, (byte)i, (byte)(15 + (i * 2))); | |
} | |
} | |
else | |
{ | |
var manager = (CurvesContinuousManager)curvesLayer.GetCurvesManager(); | |
manager.AddCurvePoint(0, 50, 100); | |
manager.AddCurvePoint(0, 150, 130); | |
} | |
} | |
} | |
// Save PSD | |
im.Save(psdPathAfterChange + j.ToString() + ".psd"); | |
} | |
} |
Support of Clipping Mask
This article shows how to support clipping mask in a PSD file with Aspose.PSD. The code snippet has been provided below.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Exposure layer editing | |
// Export of the psd with complex clipping mask | |
string sourceFileName = dataDir + "ClippingMaskComplex.psd"; | |
string exportPath = dataDir + "ClippingMaskComplex.png"; | |
using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
// Export to PNG | |
var saveOptions = new PngOptions(); | |
saveOptions.ColorType = PngColorType.TruecolorWithAlpha; | |
im.Save(exportPath, saveOptions); | |
} |
Manager Photo Filter Adjustment Layer
This article shows how to iterate through each photo filter layers and add and edit them and save them as PSD file with Aspose.PSD. The code snippet has been provided below.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string dataDir = RunExamples.GetDataDir_PSD(); | |
// Photo Filter layer editing | |
string sourceFileName = dataDir + "PhotoFilterAdjustmentLayer.psd"; | |
string psdPathAfterChange = dataDir + "PhotoFilterAdjustmentLayerChanged.psd"; | |
using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
foreach (var layer in im.Layers) | |
{ | |
if (layer is PhotoFilterLayer) | |
{ | |
var photoLayer = (PhotoFilterLayer)layer; | |
photoLayer.Color = Color.FromArgb(255, 60, 60); | |
photoLayer.Density = 78; | |
photoLayer.PreserveLuminosity = false; | |
} | |
} | |
im.Save(psdPathAfterChange); | |
} | |
// Photo Filter layer adding | |
sourceFileName = dataDir + "PhotoExample.psd"; | |
psdPathAfterChange = dataDir + "PhotoExampleAddedPhotoFilter.psd"; | |
using (PsdImage im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
var layer = im.AddPhotoFilterLayer(Color.FromArgb(25, 255, 35)); | |
im.Save(psdPathAfterChange); | |
} |
Support Flatten Layers
This article shows how to flatten whole PSD file and merge one layer into another and save them as PSD file with Aspose.PSD. The code snippet has been provided below.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string dataDir = RunExamples.GetDataDir_PSD(); | |
// Photo Filter layer editing | |
string sourceFileName = dataDir + "PhotoFilterAdjustmentLayer.psd"; | |
string psdPathAfterChange = dataDir + "PhotoFilterAdjustmentLayerChanged.psd"; | |
using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
foreach (var layer in im.Layers) | |
{ | |
if (layer is PhotoFilterLayer) | |
{ | |
var photoLayer = (PhotoFilterLayer)layer; | |
photoLayer.Color = Color.FromArgb(255, 60, 60); | |
photoLayer.Density = 78; | |
photoLayer.PreserveLuminosity = false; | |
} | |
} | |
im.Save(psdPathAfterChange); | |
} | |
// Photo Filter layer adding | |
sourceFileName = dataDir + "PhotoExample.psd"; | |
psdPathAfterChange = dataDir + "PhotoExampleAddedPhotoFilter.psd"; | |
using (PsdImage im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
var layer = im.AddPhotoFilterLayer(Color.FromArgb(25, 255, 35)); | |
im.Save(psdPathAfterChange); | |
} |
Adding and Rendering of Level Layers
This article shows how to iterate through each level layers and set different levels than save that as PSD and PNG file with Aspose.PSD. The code snippet has been provided below.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string sourceFileName = dataDir + "LevelsAdjustmentLayer.psd"; | |
string psdPathAfterChange = dataDir + "LevelsAdjustmentLayerChanged.psd"; | |
string pngExportPath = dataDir + "LevelsAdjustmentLayerChanged.png"; | |
using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
foreach (var layer in im.Layers) | |
{ | |
if (layer is LevelsLayer) | |
{ | |
var levelsLayer = (LevelsLayer)layer; | |
var channel = levelsLayer.GetChannel(0); | |
channel.InputMidtoneLevel = 2.0f; | |
channel.InputShadowLevel = 10; | |
channel.InputHighlightLevel = 230; | |
channel.OutputShadowLevel = 20; | |
channel.OutputHighlightLevel = 200; | |
} | |
} | |
// Save PSD | |
im.Save(psdPathAfterChange); | |
// Save PNG | |
var saveOptions = new PngOptions(); | |
saveOptions.ColorType = PngColorType.TruecolorWithAlpha; | |
im.Save(pngExportPath, saveOptions); | |
} |
Add Hue Saturation of Adjustment Layers
This article shows how to iterate through each Hue layers and set different ranges and colors than save that as PSD file with Aspose.PSD. The code snippet has been provided below.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Hue/Saturation layer editing | |
string sourceFileName = dataDir + "HueSaturationAdjustmentLayer.psd"; | |
string psdPathAfterChange = dataDir + "HueSaturationAdjustmentLayerChanged.psd"; | |
using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
foreach (var layer in im.Layers) | |
{ | |
if (layer is HueSaturationLayer) | |
{ | |
var hueLayer = (HueSaturationLayer)layer; | |
hueLayer.Hue = -25; | |
hueLayer.Saturation = -12; | |
hueLayer.Lightness = 67; | |
var colorRange = hueLayer.GetRange(2); | |
colorRange.Hue = -40; | |
colorRange.Saturation = 50; | |
colorRange.Lightness = -20; | |
colorRange.MostLeftBorder = 300; | |
} | |
} | |
im.Save(psdPathAfterChange); | |
} | |
// Hue/Saturation layer adding | |
sourceFileName = dataDir + "PhotoExample.psd"; | |
psdPathAfterChange = dataDir + "PhotoExampleAddedHueSaturation.psd"; | |
using (PsdImage im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
//this.SaveForVisualTest(im, this.OutputPath, prefix + file, "before"); | |
var hueLayer = im.AddHueSaturationAdjustmentLayer(); | |
hueLayer.Hue = -25; | |
hueLayer.Saturation = -12; | |
hueLayer.Lightness = 67; | |
var colorRange = hueLayer.GetRange(2); | |
colorRange.Hue = -160; | |
colorRange.Saturation = 100; | |
colorRange.Lightness = 20; | |
colorRange.MostLeftBorder = 300; | |
im.Save(psdPathAfterChange); | |
} |
Add Level Adjustment Layers
This article shows how to iterate through each level layers and set different levels than save that as PSD with Aspose.PSD. The code snippet has been provided below.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string sourceFileName = dataDir + "LevelsAdjustmentLayer.psd"; | |
string psdPathAfterChange = dataDir + "LevelsAdjustmentLayerChanged.psd"; | |
using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
foreach (var layer in im.Layers) | |
{ | |
if (layer is LevelsLayer) | |
{ | |
var levelsLayer = (LevelsLayer)layer; | |
var channel = levelsLayer.GetChannel(0); | |
channel.InputMidtoneLevel = 2.0f; | |
channel.InputShadowLevel = 10; | |
channel.InputHighlightLevel = 230; | |
channel.OutputShadowLevel = 20; | |
channel.OutputHighlightLevel = 200; | |
} | |
} | |
// Save PSD | |
im.Save(psdPathAfterChange); | |
} |
Support for Interrupt Monitor
This article shows how to create and monitor interrupts in Aspose.PSD. The code snippet has been provided below.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
class InterruptMonitorTest | |
{ | |
public static void Run() | |
{ | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_PSB(); | |
InterruptMonitor(dataDir, dataDir); | |
} | |
public static void InterruptMonitor(string dir, string ouputDir) | |
{ | |
ImageOptionsBase saveOptions = new ImageOptions.PngOptions(); | |
Multithreading.InterruptMonitor monitor = new Multithreading.InterruptMonitor(); | |
SaveImageWorker worker = new SaveImageWorker(dir + "big.psb", dir + "big_out.png", saveOptions, monitor); | |
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(worker.ThreadProc)); | |
try | |
{ | |
thread.Start(); | |
// The timeout should be less than the time required for full image conversion (without interruption). | |
System.Threading.Thread.Sleep(3000); | |
// Interrupt the process | |
monitor.Interrupt(); | |
System.Console.WriteLine("Interrupting the save thread #{0} at {1}", thread.ManagedThreadId, System.DateTime.Now); | |
// Wait for interruption... | |
thread.Join(); | |
} | |
finally | |
{ | |
// If the file to be deleted does not exist, no exception is thrown. | |
System.IO.File.Delete(dir + "big_out.png"); | |
} | |
} | |
} | |
/// <summary> | |
/// Initiates image conversion and waits for its interruption. | |
/// </summary> | |
public class SaveImageWorker | |
{ | |
/// <summary> | |
/// The path to the input image. | |
/// </summary> | |
private readonly string inputPath; | |
/// <summary> | |
/// The path to the output image. | |
/// </summary> | |
private readonly string outputPath; | |
/// <summary> | |
/// The interrupt monitor. | |
/// </summary> | |
private readonly Multithreading.InterruptMonitor monitor; | |
/// <summary> | |
/// The save options. | |
/// </summary> | |
private readonly ImageOptionsBase saveOptions; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="SaveImageWorker" /> class. | |
/// </summary> | |
/// <param name="inputPath">The path to the input image.</param> | |
/// <param name="outputPath">The path to the output image.</param> | |
/// <param name="saveOptions">The save options.</param> | |
/// <param name="monitor">The interrupt monitor.</param> | |
public SaveImageWorker(string inputPath, string outputPath, ImageOptionsBase saveOptions, Multithreading.InterruptMonitor monitor) | |
{ | |
this.inputPath = inputPath; | |
this.outputPath = outputPath; | |
this.saveOptions = saveOptions; | |
this.monitor = monitor; | |
} | |
/// <summary> | |
/// Tries to convert image from one format to another. Handles interruption. | |
/// </summary> | |
public void ThreadProc() | |
{ | |
using (Image image = Image.Load(this.inputPath)) | |
{ | |
Multithreading.InterruptMonitor.ThreadLocalInstance = this.monitor; | |
try | |
{ | |
image.Save(this.outputPath, this.saveOptions); | |
throw new Exception("Expected interruption."); | |
} | |
catch (CoreExceptions.OperationInterruptedException e) | |
{ | |
System.Console.WriteLine("The save thread #{0} finishes at {1}", System.Threading.Thread.CurrentThread.ManagedThreadId, System.DateTime.Now); | |
System.Console.WriteLine(e); | |
} | |
catch (System.Exception e) | |
{ | |
System.Console.WriteLine(e); | |
} | |
finally | |
{ | |
Multithreading.InterruptMonitor.ThreadLocalInstance = null; | |
} | |
} | |
} | |
} |
Support Layer Effects at runtime
This article shows how to add layer effects at runtime in Aspose.PSD. The code snippet has been provided below.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
class InterruptMonitorTest | |
{ | |
public static void Run() | |
{ | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_PSB(); | |
InterruptMonitor(dataDir, dataDir); | |
} | |
public static void InterruptMonitor(string dir, string ouputDir) | |
{ | |
ImageOptionsBase saveOptions = new ImageOptions.PngOptions(); | |
Multithreading.InterruptMonitor monitor = new Multithreading.InterruptMonitor(); | |
SaveImageWorker worker = new SaveImageWorker(dir + "big.psb", dir + "big_out.png", saveOptions, monitor); | |
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(worker.ThreadProc)); | |
try | |
{ | |
thread.Start(); | |
// The timeout should be less than the time required for full image conversion (without interruption). | |
System.Threading.Thread.Sleep(3000); | |
// Interrupt the process | |
monitor.Interrupt(); | |
System.Console.WriteLine("Interrupting the save thread #{0} at {1}", thread.ManagedThreadId, System.DateTime.Now); | |
// Wait for interruption... | |
thread.Join(); | |
} | |
finally | |
{ | |
// If the file to be deleted does not exist, no exception is thrown. | |
System.IO.File.Delete(dir + "big_out.png"); | |
} | |
} | |
} | |
/// <summary> | |
/// Initiates image conversion and waits for its interruption. | |
/// </summary> | |
public class SaveImageWorker | |
{ | |
/// <summary> | |
/// The path to the input image. | |
/// </summary> | |
private readonly string inputPath; | |
/// <summary> | |
/// The path to the output image. | |
/// </summary> | |
private readonly string outputPath; | |
/// <summary> | |
/// The interrupt monitor. | |
/// </summary> | |
private readonly Multithreading.InterruptMonitor monitor; | |
/// <summary> | |
/// The save options. | |
/// </summary> | |
private readonly ImageOptionsBase saveOptions; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="SaveImageWorker" /> class. | |
/// </summary> | |
/// <param name="inputPath">The path to the input image.</param> | |
/// <param name="outputPath">The path to the output image.</param> | |
/// <param name="saveOptions">The save options.</param> | |
/// <param name="monitor">The interrupt monitor.</param> | |
public SaveImageWorker(string inputPath, string outputPath, ImageOptionsBase saveOptions, Multithreading.InterruptMonitor monitor) | |
{ | |
this.inputPath = inputPath; | |
this.outputPath = outputPath; | |
this.saveOptions = saveOptions; | |
this.monitor = monitor; | |
} | |
/// <summary> | |
/// Tries to convert image from one format to another. Handles interruption. | |
/// </summary> | |
public void ThreadProc() | |
{ | |
using (Image image = Image.Load(this.inputPath)) | |
{ | |
Multithreading.InterruptMonitor.ThreadLocalInstance = this.monitor; | |
try | |
{ | |
image.Save(this.outputPath, this.saveOptions); | |
throw new Exception("Expected interruption."); | |
} | |
catch (CoreExceptions.OperationInterruptedException e) | |
{ | |
System.Console.WriteLine("The save thread #{0} finishes at {1}", System.Threading.Thread.CurrentThread.ManagedThreadId, System.DateTime.Now); | |
System.Console.WriteLine(e); | |
} | |
catch (System.Exception e) | |
{ | |
System.Console.WriteLine(e); | |
} | |
finally | |
{ | |
Multithreading.InterruptMonitor.ThreadLocalInstance = null; | |
} | |
} | |
} | |
} |
Add Curve Adjustment layer
This article shows how to add curve adjustment layers in PSD using Aspose.PSD. The code snippet has been provided below.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Curves layer editing | |
string sourceFileName = dataDir + "CurvesAdjustmentLayer"; | |
string psdPathAfterChange = dataDir + "CurvesAdjustmentLayerChanged"; | |
for (int j = 1; j < 2; j++) | |
{ | |
using (var im = (PsdImage)Image.Load(sourceFileName + j.ToString() + ".psd")) | |
{ | |
foreach (var layer in im.Layers) | |
{ | |
if (layer is CurvesLayer) | |
{ | |
var curvesLayer = (CurvesLayer)layer; | |
if (curvesLayer.IsDiscreteManagerUsed) | |
{ | |
var manager = (CurvesDiscreteManager)curvesLayer.GetCurvesManager(); | |
for (int i = 10; i < 50; i++) | |
{ | |
manager.SetValueInPosition(0, (byte)i, (byte)(15 + (i * 2))); | |
} | |
} | |
else | |
{ | |
var manager = (CurvesContinuousManager)curvesLayer.GetCurvesManager(); | |
manager.AddCurvePoint(0, 50, 100); | |
manager.AddCurvePoint(0, 150, 130); | |
} | |
} | |
} | |
// Save PSD | |
im.Save(psdPathAfterChange + j.ToString() + ".psd"); | |
} | |
} |
Add Channel Mixer Adjustment layer
This article shows how to add channel mixer adjustment layers and set different colors and then save as PSD images. The code snippet has been provided below.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string sourceFileName = dataDir + "ChannelMixerAdjustmentLayerRgb.psd"; | |
string psdPathAfterChange = dataDir + "ChannelMixerAdjustmentLayerRgbChanged.psd"; | |
using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
foreach (var layer in im.Layers) | |
{ | |
if (layer is RgbChannelMixerLayer) | |
{ | |
var rgbLayer = (RgbChannelMixerLayer)layer; | |
rgbLayer.RedChannel.Blue = 100; | |
rgbLayer.BlueChannel.Green = -100; | |
rgbLayer.GreenChannel.Constant = 50; | |
} | |
} | |
im.Save(psdPathAfterChange); | |
} | |
// Cmyk Channel Mixer | |
sourceFileName = dataDir + "ChannelMixerAdjustmentLayerCmyk.psd"; | |
psdPathAfterChange = dataDir + "ChannelMixerAdjustmentLayerCmykChanged.psd"; | |
using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
foreach (var layer in im.Layers) | |
{ | |
if (layer is CmykChannelMixerLayer) | |
{ | |
var cmykLayer = (CmykChannelMixerLayer)layer; | |
cmykLayer.CyanChannel.Black = 20; | |
cmykLayer.MagentaChannel.Yellow = 50; | |
cmykLayer.YellowChannel.Cyan = -25; | |
cmykLayer.BlackChannel.Yellow = 25; | |
} | |
} | |
im.Save(psdPathAfterChange); | |
} | |
// Adding the new layer(Cmyk for this example) | |
sourceFileName = dataDir + "CmykWithAlpha.psd"; | |
psdPathAfterChange = dataDir + "ChannelMixerAdjustmentLayerCmykChanged.psd"; | |
using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
var newlayer = im.AddChannelMixerAdjustmentLayer(); | |
newlayer.GetChannelByIndex(2).Constant = 50; | |
newlayer.GetChannelByIndex(0).Constant = 50; | |
im.Save(psdPathAfterChange); | |
} |
Rendering of Channel Mixer Adjustment layer
This article shows how to render channel mixer adjustment layers and set different colors channel and then save as PSD and PNG images. The code snippet has been provided below.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string sourceFileName = dataDir + "ChannelMixerAdjustmentLayerRgb.psd"; | |
string psdPathAfterChange = dataDir + "ChannelMixerAdjustmentLayerRgbChanged.psd"; | |
using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
foreach (var layer in im.Layers) | |
{ | |
if (layer is RgbChannelMixerLayer) | |
{ | |
var rgbLayer = (RgbChannelMixerLayer)layer; | |
rgbLayer.RedChannel.Blue = 100; | |
rgbLayer.BlueChannel.Green = -100; | |
rgbLayer.GreenChannel.Constant = 50; | |
} | |
} | |
im.Save(psdPathAfterChange); | |
} | |
// Cmyk Channel Mixer | |
sourceFileName = dataDir + "ChannelMixerAdjustmentLayerCmyk.psd"; | |
psdPathAfterChange = dataDir + "ChannelMixerAdjustmentLayerCmykChanged.psd"; | |
using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
foreach (var layer in im.Layers) | |
{ | |
if (layer is CmykChannelMixerLayer) | |
{ | |
var cmykLayer = (CmykChannelMixerLayer)layer; | |
cmykLayer.CyanChannel.Black = 20; | |
cmykLayer.MagentaChannel.Yellow = 50; | |
cmykLayer.YellowChannel.Cyan = -25; | |
cmykLayer.BlackChannel.Yellow = 25; | |
} | |
} | |
im.Save(psdPathAfterChange); | |
} | |
// Adding the new layer(Cmyk for this example) | |
sourceFileName = dataDir + "CmykWithAlpha.psd"; | |
psdPathAfterChange = dataDir + "ChannelMixerAdjustmentLayerCmykChanged.psd"; | |
using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
var newlayer = im.AddChannelMixerAdjustmentLayer(); | |
newlayer.GetChannelByIndex(2).Constant = 50; | |
newlayer.GetChannelByIndex(0).Constant = 50; | |
im.Save(psdPathAfterChange); | |
} |
Rendering Exposure Adjustment layer
This article shows how to iterate through each exposure level layers and set different exposure values than save that as PSD as well as PNG with Aspose.PSD. The code snippet has been provided below.
// 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"; | |
string pngExportPath = dataDir + "ExposureAdjustmentLayerChanged.png"; | |
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; | |
} | |
} | |
// Save PSD | |
im.Save(psdPathAfterChange); | |
// Save PNG | |
var saveOptions = new PngOptions(); | |
saveOptions.ColorType = PngColorType.TruecolorWithAlpha; | |
im.Save(pngExportPath, saveOptions); | |
} | |
// Exposure layer adding | |
sourceFileName = dataDir + "PhotoExample.psd"; | |
psdPathAfterChange = dataDir + "PhotoExampleAddedExposure.psd"; | |
pngExportPath = dataDir + "PhotoExampleAddedExposure.png"; | |
using (PsdImage im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
var newlayer = im.AddExposureAdjustmentLayer(); | |
newlayer.Exposure = 2; | |
newlayer.Offset = -0.25f; | |
newlayer.GammaCorrection = 2f; | |
// Save PSD | |
im.Save(psdPathAfterChange); | |
// Save PNG | |
var saveOptions = new PngOptions(); | |
saveOptions.ColorType = PngColorType.TruecolorWithAlpha; | |
im.Save(pngExportPath, saveOptions); | |
} |
Support Blend Modes
Using Aspose.PSD for .NET, developers can use different blend modes. Below is the code demonstration of the said functionality.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
{ | |
"Normal", | |
"Dissolve", | |
"Darken", | |
"Multiply", | |
"ColorBurn", | |
"LinearBurn", | |
"DarkerColor", | |
"Lighten", | |
"Screen", | |
"ColorDodge", | |
"LinearDodgeAdd", | |
"LightenColor", | |
"Overlay", | |
"SoftLight", | |
"HardLight", | |
"VividLight", | |
"LinearLight", | |
"PinLight", | |
"HardMix", | |
"Difference", | |
"Exclusion", | |
"Subtract", | |
"Divide", | |
"Hue", | |
"Saturation", | |
"Color", | |
"Luminosity", | |
}; | |
foreach (var fileName in files) | |
{ | |
using (var im = (PsdImage)Image.Load(dataDir + fileName + ".psd")) | |
{ | |
// Export to PNG | |
var saveOptions = new PngOptions(); | |
saveOptions.ColorType = PngColorType.TruecolorWithAlpha; | |
var pngExportPath100 = "BlendMode" + fileName + "_Test100.png"; | |
im.Save(pngExportPath100, saveOptions); | |
// Set opacity 50% | |
im.Layers[1].Opacity = 127; | |
var pngExportPath50 = "BlendMode" + fileName + "_Test50.png"; | |
im.Save(pngExportPath50, saveOptions); | |
} | |
} | |
} |
Support Color Overlay Effect
Using Aspose.PSD for .NET, developers can set a new layer with a color on top of that image and set it to the blending mode “Color.” To add a more effect, you put the color image on top of these 2 layers and use the “Overlay” blending mode. Below is the code demonstration of the said functionality.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// ColorOverlay effect editing | |
string sourceFileName = dataDir + "ColorOverlay.psd"; | |
string psdPathAfterChange = dataDir + "ColorOverlayChanged.psd"; | |
var loadOptions = new PsdLoadOptions() | |
{ | |
LoadEffectsResource = true | |
}; | |
using (var im = (PsdImage)Image.Load(sourceFileName, loadOptions)) | |
{ | |
var colorOverlay = (ColorOverlayEffect)(im.Layers[1].BlendingOptions.Effects[0]); | |
if (colorOverlay.Color != Color.Red || | |
colorOverlay.Opacity != 153) | |
{ | |
throw new Exception("Color overlay read wrong"); | |
} | |
colorOverlay.Color = Color.Green; | |
colorOverlay.Opacity = 128; | |
im.Save(psdPathAfterChange); | |
} |
Support Drop Shadow Effect
Using Aspose.PSD for .NET, developers can set drop shadow effects to different elements. Below is the code demonstration of the said functionality.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string psdPathAfterChange = dataDir + "ShadowChanged.psd"; | |
var loadOptions = new PsdLoadOptions() | |
{ | |
LoadEffectsResource = true | |
}; | |
using (var im = (PsdImage)Image.Load(sourceFileName, loadOptions)) | |
{ | |
var shadowEffect = (DropShadowEffect)(im.Layers[1].BlendingOptions.Effects[0]); | |
if ((shadowEffect.Color != Color.Black) || | |
(shadowEffect.Opacity != 255) || | |
(shadowEffect.Distance != 3) || | |
(shadowEffect.Size != 7) || | |
(shadowEffect.UseGlobalLight != true) || | |
(shadowEffect.Angle != 90) || | |
(shadowEffect.Spread != 0) || | |
(shadowEffect.Noise != 0)) | |
{ | |
throw new Exception("Shadow Effect was read wrong"); | |
} | |
shadowEffect.Color = Color.Green; | |
shadowEffect.Opacity = 128; | |
shadowEffect.Distance = 11; | |
shadowEffect.UseGlobalLight = false; | |
shadowEffect.Size = 9; | |
shadowEffect.Angle = 45; | |
shadowEffect.Spread = 3; | |
shadowEffect.Noise = 50; | |
im.Save(psdPathAfterChange); | |
} | |
} |
Rendering for export of Color Overlay effect
Using Aspose.PSD for .NET, developers can render documents of color overlay effect. Below is the code demonstration of the said functionality.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string pngExportPath = dataDir + "ColorOverlayresult.png"; | |
var loadOptions = new PsdLoadOptions() | |
{ | |
LoadEffectsResource = true | |
}; | |
using (var im = (PsdImage)Image.Load(sourceFileName, loadOptions)) | |
//using (var im = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
var colorOverlay = (ColorOverlayEffect)(im.Layers[1].BlendingOptions.Effects[0]); | |
if (colorOverlay.Color != Color.Red || | |
colorOverlay.Opacity != 153) | |
{ | |
throw new Exception("Color overlay read wrong"); | |
} | |
// Save PNG | |
var saveOptions = new PngOptions(); | |
saveOptions.ColorType = PngColorType.TruecolorWithAlpha; | |
im.Save(pngExportPath, saveOptions); | |
} | |
} |
Rendering for export of Drop Shadow effect
Using Aspose.PSD for .NET, developers can render documents of the drop shadow effect. Below is the code demonstration of the said functionality.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string pngExportPath = dataDir + "Shadowchanged1.png"; | |
var loadOptions = new PsdLoadOptions() | |
{ | |
LoadEffectsResource = true | |
}; | |
using (var im = (PsdImage)Image.Load(sourceFileName, loadOptions)) | |
{ | |
var shadowEffect = (DropShadowEffect)(im.Layers[1].BlendingOptions.Effects[0]); | |
if ((shadowEffect.Color != Color.Black) || | |
(shadowEffect.Opacity != 255) || | |
(shadowEffect.Distance != 3) || | |
(shadowEffect.Size != 7) || | |
(shadowEffect.UseGlobalLight != true) || | |
(shadowEffect.Angle != 90) || | |
(shadowEffect.Spread != 0) || | |
(shadowEffect.Noise != 0)) | |
{ | |
throw new Exception("Shadow Effect properties were read wrong"); | |
} | |
// Save PNG | |
var saveOptions = new PngOptions(); | |
saveOptions.ColorType = PngColorType.TruecolorWithAlpha; | |
im.Save(pngExportPath, saveOptions); | |
} | |
} |
Support of GdFlResource
This article shows Support of GdFlResource in a PSD file with Aspose.PSD. The following code snippet shows you how Aspose.PSD supports GdFlResource.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Support of GdFlResource | |
string sourceFileName = dataDir + "ComplexGradientFillLayer.psd"; | |
string exportPath = dataDir + "ComplexGradientFillLayer_after.psd"; | |
var im = (FileFormats.Psd.PsdImage)Image.Load(sourceFileName); | |
using (im) | |
{ | |
foreach (var layer in im.Layers) | |
{ | |
if (layer is FillLayer) | |
{ | |
var fillLayer = (FillLayer)layer; | |
var resources = fillLayer.Resources; | |
foreach (var res in resources) | |
{ | |
if (res is GdFlResource) | |
{ | |
// Reading | |
var resource = (GdFlResource)res; | |
if (resource.AlignWithLayer != false || | |
(Math.Abs(resource.Angle - 45.0) > 0.001) || | |
resource.Dither != true || | |
resource.Reverse != false || | |
resource.Color != Color.Empty || | |
Math.Abs(resource.HorizontalOffset - (-39)) > 0.001 || | |
Math.Abs(resource.VerticalOffset - (-5)) > 0.001 || | |
resource.TransparencyPoints.Length != 3 || | |
resource.ColorPoints.Length != 2) | |
{ | |
throw new Exception("Resource Parameters were read wrong"); | |
} | |
var transparencyPoints = resource.TransparencyPoints; | |
if (Math.Abs(100.0 - transparencyPoints[0].Opacity) > 0.25 || | |
transparencyPoints[0].Location != 0 || | |
transparencyPoints[0].MedianPointLocation != 50 || | |
Math.Abs(50.0 - transparencyPoints[1].Opacity) > 0.25 || | |
transparencyPoints[1].Location != 2048 || | |
transparencyPoints[1].MedianPointLocation != 50 || | |
Math.Abs(100.0 - transparencyPoints[2].Opacity) > 0.25 || | |
transparencyPoints[2].Location != 4096 || | |
transparencyPoints[2].MedianPointLocation != 50) | |
{ | |
throw new Exception("Gradient Transparency Points were read Wrong"); | |
} | |
var colorPoints = resource.ColorPoints; | |
if (colorPoints[0].Color != Color.FromArgb(203, 64, 140) || | |
colorPoints[0].Location != 0 || | |
colorPoints[0].MedianPointLocation != 50 || | |
colorPoints[1].Color != Color.FromArgb(203, 0, 0) || | |
colorPoints[1].Location != 4096 || | |
colorPoints[1].MedianPointLocation != 50) | |
{ | |
throw new Exception("Gradient Color Points were read Wrong"); | |
} | |
// Editing | |
resource.Angle = 30.0; | |
resource.Dither = false; | |
resource.AlignWithLayer = true; | |
resource.Reverse = true; | |
resource.HorizontalOffset = 25; | |
resource.VerticalOffset = -15; | |
var newColorPoints = new List<IGradientColorPoint>(resource.ColorPoints); | |
var | |
newTransparencyPoints = new | |
List<IGradientTransparencyPoint>(resource.TransparencyPoints); | |
newColorPoints.Add(new GradientColorPoint() | |
{ | |
Color = Color.Violet, | |
Location = 4096, | |
MedianPointLocation = 75 | |
}); | |
colorPoints[1].Location = 3000; | |
newTransparencyPoints.Add(new GradientTransparencyPoint() | |
{ | |
Opacity = 80.0, | |
Location = 4096, | |
MedianPointLocation = 25 | |
}); | |
transparencyPoints[2].Location = 3000; | |
resource.ColorPoints = newColorPoints.ToArray(); | |
resource.TransparencyPoints = newTransparencyPoints.ToArray(); | |
im.Save(exportPath); | |
} | |
break; | |
} | |
break; | |
} | |
} | |
} |
Support of VmskResource
This article shows Support of VmskResource in a PSD file with Aspose.PSD. The following code snippet shows you how Aspose.PSD supports VmskResource.
// 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(); | |
string sourceFileName = dataDir + "Rectangle.psd"; | |
string exportPath = dataDir + "Rectangle_changed.psd"; | |
var im = (PsdImage)Image.Load(sourceFileName); | |
using (im) | |
{ | |
var resource = GetVmskResource(im); | |
// Reading | |
if (resource.IsDisabled != false || | |
resource.IsInverted != false || | |
resource.IsNotLinked != false || | |
resource.Paths.Length != 7 || | |
resource.Paths[0].Type != VectorPathType.PathFillRuleRecord || | |
resource.Paths[1].Type != VectorPathType.InitialFillRuleRecord || | |
resource.Paths[2].Type != VectorPathType.ClosedSubpathLengthRecord || | |
resource.Paths[3].Type != VectorPathType.ClosedSubpathBezierKnotUnlinked || | |
resource.Paths[4].Type != VectorPathType.ClosedSubpathBezierKnotUnlinked || | |
resource.Paths[5].Type != VectorPathType.ClosedSubpathBezierKnotUnlinked || | |
resource.Paths[6].Type != VectorPathType.ClosedSubpathBezierKnotUnlinked) | |
{ | |
throw new Exception("VmskResource was read wrong"); | |
} | |
var pathFillRule = (PathFillRuleRecord)resource.Paths[0]; | |
var initialFillRule = (InitialFillRuleRecord)resource.Paths[1]; | |
var subpathLength = (LengthRecord)resource.Paths[2]; | |
// Path fill rule doesn't contain any additional information | |
if (pathFillRule.Type != VectorPathType.PathFillRuleRecord || | |
initialFillRule.Type != VectorPathType.InitialFillRuleRecord || | |
initialFillRule.IsFillStartsWithAllPixels != false || | |
subpathLength.Type != VectorPathType.ClosedSubpathLengthRecord || | |
subpathLength.IsClosed != true || | |
subpathLength.IsOpen != false) | |
{ | |
throw new Exception("VmskResource paths were read wrong"); | |
} | |
// Editing | |
resource.IsDisabled = true; | |
resource.IsInverted = true; | |
resource.IsNotLinked = true; | |
var bezierKnot = (BezierKnotRecord)resource.Paths[3]; | |
bezierKnot.Points[0] = new Point(0, 0); | |
bezierKnot = (BezierKnotRecord)resource.Paths[4]; | |
bezierKnot.Points[0] = new Point(8039797, 10905190); | |
initialFillRule.IsFillStartsWithAllPixels = true; | |
subpathLength.IsClosed = false; | |
im.Save(exportPath); | |
} |
Support of RGB Color
This article shows how Aspose.PSD support RGB color mode with 16bit per channels. The code snippet has been provided below.