How to Apply Gradient or Pattern Fill to PSD Layers Using Aspose.PSD for .NET
Gradient and pattern fills make PSD artwork dynamic and brand-compliant. Aspose.PSD for .NET lets you set them on fill or shape layers in code—perfect for batch design or generative graphics.
Real-World Problem
Manually updating gradients or patterns in hundreds of PSD templates is slow and error-prone. Automation is vital for large-scale design or on-the-fly graphics.
Solution Overview
Use Aspose.PSD for .NET to configure gradient or pattern fill settings, then apply them directly to FillLayer
or ShapeLayer
objects programmatically.
Prerequisites
- Visual Studio 2019 or later
- .NET 6.0 or later (or .NET Framework 4.6.2+)
- Aspose.PSD for .NET from NuGet
- PSD file with a fill or shape layer
PM> Install-Package Aspose.PSD
Step-by-Step Implementation
Step 1: Load the PSD File
using Aspose.PSD;
using Aspose.PSD.FileFormats.Psd;
using Aspose.PSD.FileFormats.Psd.Layers;
using Aspose.PSD.FileFormats.Psd.Layers.FillLayers;
string inputFile = "./input/sample_with_filllayer.psd";
string outputFile = "./output/sample_gradient_fill.psd";
var loadOptions = new PsdLoadOptions() { LoadEffectsResource = true };
PsdImage psdImage = (PsdImage)Image.Load(inputFile, loadOptions);
Step 2: Locate the Fill or Shape Layer
FillLayer fillLayer = null;
foreach (var layer in psdImage.Layers)
{
if (layer is FillLayer f)
{
fillLayer = f;
break;
}
}
if (fillLayer == null)
{
throw new InvalidOperationException("No fill layer found in PSD.");
}
Step 3: Set Up Gradient Fill Settings
var gradientSettings = new GradientFillSettings();
gradientSettings.ColorPoints = new IGradientColorPoint[] {
new GradientColorPoint() { Color = Color.Red },
new GradientColorPoint() { Color = Color.Blue }
};
fillLayer.FillSettings = gradientSettings;
Step 4: (Optional) Set Up Pattern Fill Settings
// Pattern fill is also supported if needed
// var patternSettings = new PatternFillSettings();
// ... configure patternSettings as needed
// fillLayer.FillSettings = patternSettings;
Step 5: Save the Updated PSD
psdImage.Save(outputFile);
psdImage.Dispose();
Use Cases and Applications
- Mass-update brand gradients across templates
- Generate pattern-filled graphics programmatically
- Creative automation for marketing assets
Common Challenges and Solutions
No visual effect: Make sure you’re targeting a fill or shape layer, not a rasterized one.
Gradient direction: Configure additional properties for angle/direction as needed.
Best Practices
- Validate fills in Photoshop for exact results
- Script fill changes for consistency across assets
- Keep original files for rollbacks
FAQ
Q: Can I combine gradients and patterns? A: Only one fill type at a time per FillLayer—use multiple layers for composites.
Q: Are all Photoshop gradient types supported? A: Most common types are; see API docs for advanced cases.
Conclusion
With Aspose.PSD for .NET, design teams can automate beautiful fills for PSD assets. For more features, see the Aspose.PSD for .NET API Reference .