Automatitzant el reemplaçament d’objectes intel·ligents ajuda a crear variacions de la mateixa plantilla de Photoshop amb diferents imatges. amb Aspose.PSD per .NET es pot carregar un PSD, localitzar les cames de objectes inteligents, substituir els seus continguts incorporats amb una nova imatge, i salvar el document actualitzat. Els exemples següents utilitzen codi en línia, executable que se centra en els casos comuns.
Prerequisits
- .NET 6 o més tard
- El nou:
Aspose.PSD
dotnet add package Aspose.PSD
Substituir el primer objecte intel·ligent en un PSD
Aquest exemple carrega un PSD, troba el primer SmartObjectLayer
, reemplaça els seus continguts amb un PNG o JPEG, actualitza la capa, i salva el PSD i un preview PONG.
// 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.");
}
}
Notícies
ReplaceContents
substitueix el contingut incorporat de l’objecte intel·ligent.UpdateModifiedContent
refresca la capa de rendiment després de la substitució.- Saving a PNG preview us ajuda a validar el resultat sense obrir Photoshop.
Substituir un objecte intel·ligent per nom de la capa
Utilitzeu aquest patró quan el vostre estàndard té múltiples objectes intel·ligents i voleu adreçar-vos a un objecte específic per nom de pantalla.
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);
}
Anomenem-ho així com això:
ReplaceByName("template.psd", "Product Image", "new-photo.jpg", "output.psd");
Substituir tots els objectes intel·ligents en un PSD
Aquest exemple passa a través de totes les capes d’objectes intel·ligents i substitueix cadascuna amb la mateixa imatge.
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);
}
Tractament d’objectes intel·ligents enllaçats
Si un objecte intel·ligent està vinculat a un fitxer extern, pot reemplaçar l’objectiu del enllaç indicant-lo a una nova imatge en el disc. 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);
}
Notícies
- Use
IsLinked
Identificar objectes intel·ligents connectats. SetLinkedFile
actualitza el camí extern utilitzat per la capa.- El fitxer enllaçat ha de ser un format de raster recolzat.
Les millors pràctiques
- ** Dimensions d’imatge**: Per a la millor qualitat, correspon la proporció i la resolució de l’aspecte de la imatge de substitució als límits de objectes intel·ligents originals.
- Gestió de colors: Mantenir els perfils de color consistent a través de les plantilles i els actius de reemplaçament.
- Batch automation: reutilitzar una càrrega
RasterImage
Quan reemplaçar moltes cames per reduir el I/O i la memòria. - Validació: Exportar una previsió PNG després de la substitució per confirmar l’alineació i escalar abans de distribuir PSDs.
- Fallbacks: Si un document no té objectes intel·ligents, fracassar ràpidament amb un error clar o trepitjar el fitxer en el mode batx.
Amb aquests patrons es poden construir tubs de modelatge PSD fiables que substitueixen objectes intel·ligents a escala i emmagatzemen lliuraments actualitzats sense obrir Photoshop.