自动化智能对象更换有助于您创建不同图像的相同 Photoshop 模板的变量. 使用 Aspose.PSD for .NET 您可以加载一个 PSD,查找智能物体层,将其内置内容替换为新图形,并保存更新文档。
原則
- .NET 6 或更晚
- 诺基亚:
Aspose.PSD
dotnet add package Aspose.PSD
在PSD中取代第一个智能对象
这个例子加载了一个PSD,发现第一 SmartObjectLayer
,将其内容替换为 PNG 或 JPEG,更新层,并保存 PSD 和预览 PPG。
// File: Program.cs
using System;
using System.IO;
using System.Linq;
using Aspose.PSD;
using Aspose.PSD.FileFormats.Psd;
using Aspose.PSD.FileFormats.Psd.Layers.SmartObjects;
using Aspose.PSD.ImageOptions;
class Program
{
static void Main()
{
var inputPsd = "template.psd";
var replacement = "replacement.png"; // any raster format supported by Aspose.PSD
var outputPsd = "output.psd";
var outputPreview = "output-preview.png";
if (!File.Exists(inputPsd) || !File.Exists(replacement))
{
Console.WriteLine("Missing input files.");
return;
}
using (var psd = (PsdImage)Image.Load(inputPsd))
{
// Find the first smart object layer
var smart = psd.Layers.OfType<SmartObjectLayer>().FirstOrDefault();
if (smart == null)
{
Console.WriteLine("No smart object layer found in PSD.");
}
else
{
// Load the replacement raster and replace contents
using var newContent = (RasterImage)Image.Load(replacement);
// Replace embedded contents and update the smart object
smart.ReplaceContents(newContent);
smart.UpdateModifiedContent();
}
// Save updated PSD
psd.Save(outputPsd);
// Optional preview export
psd.Save(outputPreview, new PngOptions());
}
Console.WriteLine("Done.");
}
}
注意事項
ReplaceContents
取代智能对象的内置内容。UpdateModifiedContent
更换后,清理放置的层。- 保存 PNG 预览可以帮助您在不打开 Photoshop 的情况下验证结果。
用层名替换智能对象
使用此模式,当您的模板有多个智能对象,并且您希望通过显示名称针对一个特定的。
using System.Linq;
using Aspose.PSD;
using Aspose.PSD.FileFormats.Psd;
using Aspose.PSD.FileFormats.Psd.Layers.SmartObjects;
static void ReplaceByName(string psdPath, string layerName, string replacementPath, string outputPsd)
{
using var psd = (PsdImage)Image.Load(psdPath);
var smart = psd.Layers
.OfType<SmartObjectLayer>()
.FirstOrDefault(l => string.Equals(l.DisplayName, layerName, StringComparison.OrdinalIgnoreCase));
if (smart == null)
throw new InvalidOperationException($"Smart object layer '{layerName}' not found.");
using var newContent = (RasterImage)Image.Load(replacementPath);
smart.ReplaceContents(newContent);
smart.UpdateModifiedContent();
psd.Save(outputPsd);
}
把它叫这样:
ReplaceByName("template.psd", "Product Image", "new-photo.jpg", "output.psd");
在PSD中取代所有智能对象
这个例子通过所有智能对象层,并用相同的图像取代它们。
using System.Linq;
using Aspose.PSD;
using Aspose.PSD.FileFormats.Psd;
using Aspose.PSD.FileFormats.Psd.Layers.SmartObjects;
static void ReplaceAll(string psdPath, string replacementPath, string outputPsd)
{
using var psd = (PsdImage)Image.Load(psdPath);
using var newContent = (RasterImage)Image.Load(replacementPath);
var smartLayers = psd.Layers.OfType<SmartObjectLayer>().ToList();
if (smartLayers.Count == 0)
throw new InvalidOperationException("No smart object layers found.");
foreach (var smart in smartLayers)
{
smart.ReplaceContents(newContent);
smart.UpdateModifiedContent();
}
psd.Save(outputPsd);
}
处理连接的智能对象
如果智能对象连接到外部文件,您可以通过指向磁盘上的新图像来取代链接目标。 UpdateModifiedContent
.
using System.Linq;
using Aspose.PSD;
using Aspose.PSD.FileFormats.Psd;
using Aspose.PSD.FileFormats.Psd.Layers.SmartObjects;
static void ReplaceLinked(string psdPath, string newLinkedFilePath, string outputPsd)
{
using var psd = (PsdImage)Image.Load(psdPath);
var linked = psd.Layers
.OfType<SmartObjectLayer>()
.FirstOrDefault(l => l.IsLinked);
if (linked == null)
throw new InvalidOperationException("No linked smart object layer found.");
// Point the layer to a new external file
linked.SetLinkedFile(newLinkedFilePath);
// Refresh layer rendering
linked.UpdateModifiedContent();
psd.Save(outputPsd);
}
注意事項
- 使用
IsLinked
识别连接的智能物体。 SetLinkedFile
更新使用层的外部路径。- 链接文件必须是支持的拉斯特格式。
最佳实践
- ** 图像尺寸**:以获得最佳品质,相匹配的替代图片方面比例和分辨率与原始智能对象界限。
- 彩色管理:在模板和替代资产中保持颜色档案一致。
- Batch 自动化:重新使用负载
RasterImage
当更换多个层以减少 I/O 和内存切割时。 - ** 验证**:在更换后出口 PNG 预览,以确认调整和扩展,然后分发 PSD。
- Fallbacks:如果文档没有智能对象,则以明显的错误快速失败或将文件转换为集合模式。
使用这些模式,您可以建立可靠的 PSD 模拟管道,以更换规模上的智能对象,并在不打开 Photoshop 的情况下保存更新。