The Displace filter is one of Photoshop’s most useful distortion tools. It warps a layer’s pixels according to the brightness values of a separate displacement map, which is how designers wrap artwork around fabric folds, add ripple effects, or fit logos onto textured surfaces. When that filter is applied as a smart filter, its settings live inside the PSD file and remain fully editable. This article shows how to read and write displace smart filter data in C#. You will add a Displace smart filter to a Smart Object layer, configure its scale and edge-handling options, embed the displacement map, save the PSD, and then read the filter settings back to verify the round trip — all without Adobe Photoshop installed.
Key Takeaways
- The
DisplaceSmartFilterclass, available in Aspose.PSD for .NET 26.8 and later, exposes the Photoshop Displace filter as a strongly typed .NET object. - Horizontal and vertical scale, displacement method (
TileorStretch), and undefined-area handling (WrapAroundorRepeatEdgePixels) are all simple read/write properties. - The displacement map can be embedded in the output PSD by passing
trueto the constructor, which keeps the file self-contained and portable. SmartFilters.UpdateResourceValues()must be called after changing the filter collection, otherwise the changes are not saved.- The same API reads filter data back from existing PSD files, which makes it suitable for validation, auditing, and batch processing.
Why the Displace Smart Filter Matters
Design automation pipelines often need to reproduce Photoshop effects at scale. Typical examples include generating product mockups where a print design follows the folds of a T-shirt, producing template variations for marketing teams, or auditing a library of PSD templates to confirm that every file uses the same filter settings.
Doing this by hand in Photoshop does not scale, and flattening the effect into pixels loses the ability to edit it later. By reading and writing Displace smart filter data in C#, you keep the filter non-destructive and editable in Photoshop, while controlling it from server-side code, background jobs, or desktop tools.
Using Aspose.PSD for Reading and Writing Displace Smart Filter Data
Aspose.PSD for .NET enables C# developers to read and write Displace smart filter data directly through the DisplaceSmartFilter class.
Getting Started
Install the Aspose.PSD NuGet package (version 26.8 or later) from the Package Manager Console:
Install-Package Aspose.PSD
Or use the .NET CLI:
dotnet add package Aspose.PSD
The examples below use the following namespaces:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Aspose.PSD;
using Aspose.PSD.FileFormats.Psd;
using Aspose.PSD.FileFormats.Psd.Layers.SmartFilters;
using Aspose.PSD.FileFormats.Psd.Layers.SmartObjects;
You will also need two input files: a PSD that contains at least one Smart Object layer, and a PSD to use as the displacement map. For API details, see the Aspose.PSD for .NET documentation and the smart filters guide.
DisplaceSmartFilter Properties at a Glance
| Property | Type | Purpose |
|---|---|---|
HorizontalScale | double | Horizontal displacement scale, as in Photoshop’s Displace dialog |
VerticalScale | double | Vertical displacement scale |
DisplacementMethod | DisplacementMethod | Tile repeats the map; Stretch resizes the map to fit the layer |
UndefinedAreas | UndefinedAreas | WrapAround or RepeatEdgePixels for pixels shifted outside the layer |
IsDisplacementMapEmbedded | bool | Indicates whether the map is embedded in the PSD |
DisplaceMapData | — | The embedded displacement map data |
DisplacementMapPath | string | The path of the displacement map |
How to Add a Displace Smart Filter in C#
The first step is to load a PSD that contains a Smart Object layer. You then create a DisplaceSmartFilter, configure its properties, append it to the layer’s smart filter collection, and save the result.
The following C# example adds and configures a Displace smart filter on a Smart Object layer.
string baseFolder = "Data";
string outputFolder = "Output";
Directory.CreateDirectory(outputFolder);
string sourceFile = Path.Combine(baseFolder, "no_displace_filter.psd");
string displaceMapPath = Path.Combine(baseFolder, "displace_map.psd");
string outputFile = Path.Combine(outputFolder, "output_displace_filter.psd");
// Load the source PSD file.
using (PsdImage image = (PsdImage)Image.Load(sourceFile))
{
// Find the first Smart Object layer instead of relying on a hard-coded index.
SmartObjectLayer smartObj = image.Layers.OfType<SmartObjectLayer>().FirstOrDefault()
?? throw new InvalidOperationException("The source PSD has no Smart Object layer.");
// Create the filter. The second argument embeds the displacement map in the output PSD.
DisplaceSmartFilter displace = new DisplaceSmartFilter(displaceMapPath, true)
{
HorizontalScale = 12.5,
VerticalScale = 15.0,
DisplacementMethod = DisplacementMethod.Tile,
UndefinedAreas = UndefinedAreas.WrapAround
};
// Filters is exposed as an array, so copy it to a list, append the new filter,
// and assign the updated array back to the collection.
List<SmartFilter> filters = new List<SmartFilter>(smartObj.SmartFilters.Filters);
filters.Add(displace);
smartObj.SmartFilters.Filters = filters.ToArray();
// Write the updated filter collection into the Smart Object layer's resources.
smartObj.SmartFilters.UpdateResourceValues();
// Save the modified PSD.
image.Save(outputFile);
}
Explanation
- Loading the PSD –
Image.Loadreturns a genericImage, which is cast toPsdImageto access PSD-specific features such as layers and smart filters. - Finding the Smart Object layer –
OfType<SmartObjectLayer>()selects the first Smart Object layer and fails with a clear message if none exists. This is safer than indexing intoLayersdirectly, because layer order varies between files. - Creating the filter – The
DisplaceSmartFilter(string, bool)constructor takes the displacement map path and a flag that controls whether the map is embedded. Passingtruemakes the output PSD self-contained. - Configuring the filter –
HorizontalScaleandVerticalScaleset the displacement strength.DisplacementMethod.Tilerepeats the map across the layer, whileStretchresizes it to fit.UndefinedAreas.WrapAroundfills pixels shifted off the canvas with content from the opposite edge;RepeatEdgePixelsextends the edge colors instead. - Updating the collection –
SmartFilters.Filtersis exposed as an array. Copying it to aList<SmartFilter>, adding the new filter, and assigningToArray()back preserves any smart filters that were already on the layer. - Persisting the changes –
UpdateResourceValues()writes the filter collection into the layer’s PSD resources. Skip this call and the new filter is lost on save. - Saving –
image.Save(outputFile)writes the updated PSD to disk.
How to Read Displace Smart Filter Data in C#
After saving the PSD, you can reload it and inspect the filter properties to confirm that every value was persisted correctly. The same approach works for PSD files created in Photoshop.
The following C# example reads the Displace smart filter back and validates its settings.
using (PsdImage image = (PsdImage)Image.Load(outputFile))
{
SmartObjectLayer smartObj = image.Layers.OfType<SmartObjectLayer>().FirstOrDefault()
?? throw new InvalidOperationException("The PSD has no Smart Object layer.");
// Search by type instead of position, so other smart filters on the layer are ignored.
DisplaceSmartFilter displace = smartObj.SmartFilters.Filters
.OfType<DisplaceSmartFilter>()
.LastOrDefault()
?? throw new InvalidOperationException("No Displace smart filter found on the layer.");
Console.WriteLine($"Horizontal scale: {displace.HorizontalScale}");
Console.WriteLine($"Vertical scale: {displace.VerticalScale}");
Console.WriteLine($"Method: {displace.DisplacementMethod}");
Console.WriteLine($"Undefined areas: {displace.UndefinedAreas}");
Console.WriteLine($"Map embedded: {displace.IsDisplacementMapEmbedded}");
// Verify that the saved values match the values written earlier.
AssertAreEqual(12.5, displace.HorizontalScale);
AssertAreEqual(15.0, displace.VerticalScale);
AssertAreEqual(DisplacementMethod.Tile, displace.DisplacementMethod);
AssertAreEqual(UndefinedAreas.WrapAround, displace.UndefinedAreas);
AssertAreEqual(true, displace.IsDisplacementMapEmbedded);
AssertAreEqual(true, displace.DisplaceMapData != null);
}
static void AssertAreEqual(object expected, object actual, string message = null)
{
if (!object.Equals(expected, actual))
{
throw new Exception(message ?? $"Expected {expected}, but got {actual}.");
}
}
Explanation
- Reloading the file –
Image.Loadopens the PSD saved in the previous step. - Locating the Smart Object layer – The same type-based lookup keeps the read and write steps consistent.
- Finding the filter by type –
OfType<DisplaceSmartFilter>().LastOrDefault()returns the most recently added Displace filter and never produces an invalid cast. This is more reliable than reading the last array element and casting withas, which silently returnsnullif a different filter sits in that position. - Reading properties – All filter settings are available as typed properties, so they can be logged, compared, or exported to a report.
- Validation – The
AssertAreEqualhelper compares persisted values with the original values and throws with a descriptive message on any mismatch.
Conclusion
With the DisplaceSmartFilter class in Aspose.PSD for .NET, C# developers can add, configure, save, and read back Photoshop Displace smart filters entirely in code. The filter stays non-destructive and editable in Photoshop, the displacement map can travel inside the PSD, and the same API supports round-trip validation. This makes it straightforward to build mockup generators, template pipelines, and PSD auditing tools on top of .NET.
FAQs
Which version of Aspose.PSD for .NET supports the Displace smart filter? Reading and writing Displace smart filter data was introduced in Aspose.PSD for .NET 26.8. Use version 26.8 or later to access the
DisplaceSmartFilterclass.Can I add a DisplaceSmartFilter to a regular raster layer? Smart filter data is stored with Smart Object layers, so the filter must be added to the
SmartFilterscollection of aSmartObjectLayer. If your content is on a regular layer, convert it to a Smart Object first.How do I embed the displacement map in the PSD? Pass the displacement map path and
trueto theDisplaceSmartFilterconstructor. After saving,IsDisplacementMapEmbeddedreturnstrueandDisplaceMapDataholds the embedded map data.Which values can the UndefinedAreas property take?
UndefinedAreassupportsWrapAround, which fills undefined pixels with content from the opposite edge of the image, andRepeatEdgePixels, which extends the colors of the edge pixels.What is the difference between DisplacementMethod.Tile and DisplacementMethod.Stretch?
Tilerepeats the displacement map across the layer at its original size, whileStretchresizes the map to fit the layer.Do I have to call UpdateResourceValues after modifying smart filters? Yes.
UpdateResourceValueswrites the modified filter collection into the Smart Object layer’s resources. Without it, the new filter is not persisted when you save.
Get a Free License and Explore More
Evaluate Aspose.PSD for .NET without limitations by requesting a temporary license:
Additional resources for working with PSD files in .NET:
- Aspose.PSD for .NET Documentation
- Aspose.PSD for .NET API Reference
- Smart Filter Manipulation in Aspose.PSD for C#
- Free Online PSD Tools
