using System.Web.Mvc;
using Aspose.PSD;
using Aspose.PSD.FileFormats.Psd;
using Aspose.PSD.FileFormats.Psd.Layers;
using Aspose.PSD.FileFormats.Psd.Layers.SmartObjects;
using Aspose.PSD.FileFormats.Psd.Layers.Warp;
using Aspose.PSD.ImageLoadOptions;
using Aspose.PSD.ImageOptions;
namespace WebApplication1.Controllers
{
public class AsposeController : Controller
{
// GET: Aspose
public ActionResult Index()
{
EditPsd();
return Content("hello aspose");
//return View();
}
public ActionResult EditPsd()
{
// Define file paths
string psdPath = @"C:\inetpub\wwwroot\Images\test\sample.psd";
string newImagePath = @"C:\inetpub\wwwroot\Images\test\sample.jpg";
string outputPath = @"C:\inetpub\wwwroot\Images\test\1.png";
var opt = new PsdLoadOptions()
{
LoadEffectsResource = true,
AllowWarpRepaint = true
};
using (PsdImage psd = (PsdImage)Image.Load(psdPath,opt))
{
// Find the "Design" smart object layer
Layer designLayer = System.Array.Find(psd.Layers, layer => layer.Name == "Design" && layer is SmartObjectLayer);
SmartObjectLayer smartObject = designLayer as SmartObjectLayer;
WarpSettings warpSettings = smartObject.WarpSettings;
//AffineTransformMatrix warpTransformMatrix = smartObject.WarpTransformMatrix;
smartObject.*ReplaceContents*(newImagePath);
smartObject.WarpSettings = warpSettings;
smartObject.UpdateModifiedContent();
//smartObject.WarpTransformMatrix = warpTransformMatrix;
// Create an instance of PngOptions
var options = new Aspose.PSD.ImageOptions.PngOptions();
options.ColorType = Aspose.PSD.FileFormats.Png.PngColorType.TruecolorWithAlpha;
// Save the document as output.png with transparency
psd.Save(outputPath, options);
return Content("PSD editing complete");
}
}
}
}
It seems you are experiencing issues with the warp transformation not functioning correctly after using the ReplaceContents
method on a SmartObject layer named “Design”.
To address this, ensure that you are using the PsdLoadOptions
with the AllowWarpRepaint
property set to true
, which is essential for processing warp effects through the public API. This option allows the warp settings to be correctly applied after replacing the contents of the SmartObject.
Here’s a refined version of your code that incorporates this suggestion:
public ActionResult EditPsd()
{
// Define file paths
string psdPath = @"C:\inetpub\wwwroot\Images\test\sample.psd";
string newImagePath = @"C:\inetpub\wwwroot\Images\test\sample.jpg";
string outputPath = @"C:\inetpub\wwwroot\Images\test\1.png";
var opt = new PsdLoadOptions()
{
LoadEffectsResource = true,
AllowWarpRepaint = true // Ensure this is set to true
};
using (PsdImage psd = (PsdImage)Image.Load(psdPath, opt))
{
// Find the "Design" smart object layer
Layer designLayer = System.Array.Find(psd.Layers, layer => layer.Name == "Design" && layer is SmartObjectLayer);
SmartObjectLayer smartObject = designLayer as SmartObjectLayer;
// Replace contents of the smart object
smartObject.ReplaceContents(newImagePath);
// Update the modified content to apply the warp settings
smartObject.UpdateModifiedContent();
// Create an instance of PngOptions
var options = new Aspose.PSD.ImageOptions.PngOptions();
options.ColorType = Aspose.PSD.FileFormats.Png.PngColorType.TruecolorWithAlpha;
// Save the document as output.png with transparency
psd.Save(outputPath, options);
return Content("PSD editing complete");
}
}
If you continue to experience issues, it may be worth checking if you are using the latest version of the Aspose.PSD library, as there have been updates that address similar problems with SmartObject content replacement and warp transformations.
For further assistance, you might want to refer to the release notes or updates related to warp effects and SmartObject handling in the Aspose documentation.
If the problem persists, consider reaching out to the support team with specific details about your issue for more targeted help.
Sources:
[1]: SmartObject ReplaceContents have problem with reRender
I have the latest version 24.9.0 and warp transformation not working without errors. I have use many PSD sample files and Input type of images.
@nikmauro
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): PSDNET-2193
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
@nikmauro we prepared workaround for this task. Please check this:
When the content is replaced, it creates the new resource (LiFeDataSource
) with file path to new content, and we currently not support warp with this resource. To store new content as bytes data in another(supported by warp) resource(LiFdDataSource
), try add this:
// code smartObject.EmbedLinked()
Example your code:
2024-10-03_16-15-20.png (78.5 KB)
This workaround should help you, until this task wouldn’t be done.
Here the new code, now the warp working but the result is cracked, see the attachement
cdl_mac_ 2024-10-08 at 15.27.28.jpg (122.6 KB)
here the PSD
mug.psd.zip (5.4 MB)
public ActionResult EditPsdNew()
{
// Define file paths
string sourceFile = @"C:\inetpub\wwwroot\Images\test\mug.psd";
string changeFile = @"C:\inetpub\wwwroot\Images\test\l1.png";
string exportFile = @"C:\inetpub\wwwroot\Images\test\export.psd";
string exportImgBefore = @"C:\inetpub\wwwroot\Images\test\export_before.png";
string exportImgAfter = @"C:\inetpub\wwwroot\Images\test\export_after.png";
using (var psdImage = (PsdImage)Image.Load(sourceFile))
{
foreach (Layer layer in psdImage.Layers)
{
if (layer is SmartObjectLayer && layer.Name == "sucai1")
{
SmartObjectLayer smartObjectLayer = (SmartObjectLayer)layer;
smartObjectLayer.ReplaceContents(changeFile);
smartObjectLayer.EmbedLinked();
break;
}
}
psdImage.Save(exportFile, new PsdOptions());
psdImage.Save(exportImgBefore, new PngOptions());
}
using (var psdImage = (PsdImage)Image.Load(exportFile))
{
psdImage.Save(exportImgAfter, new PngOptions());
}
return Content("PSD editing complete");
}
@nikmauro
Aspose.PSD team actively works on improvement of warp rendering, but it still has issues. We can not make pixel-perfect rendering of Warp, but we are working on the couple of tasks about improvement of quality.
We have opened the following new ticket(s) in our internal issue tracking system for your example and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): PSDNET-2215
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
I’ve added PSDNET-1600 issue additionaly. It should fix the “Cracking” of result.