The following code fails with:
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object. at Aspose.PSD.FileFormats.Psd.PsdImage.(SmartObjectLayer ) at Aspose.PSD.FileFormats.Psd.PsdImage.(Layer , Int32 ) at Aspose.PSD.FileFormats.Psd.PsdImage.AddLayer(Layer layer) at Program.<Main>$(String[] args)
At first, I thought it could be the MemoryStream
being messed up when retrieving the image, so I’ve hardcoded a PNG file (hence the getImage()
method). Still same result.
I also tried with Layer
instead and that worked like a charm… but I need it as a SmartObjectLayer
so I can later swap images using Photoshop directly.
…so, what’s the proper way to add a SmartObject layer to a PSD?
Btw, psd.SmartObjectProvider
is null, so converting a Layer
to a SmartObjectLayer
also fails.
(using Aspose 24.11.0)
using Aspose.PSD;
using Aspose.PSD.FileFormats.Psd;
using Aspose.PSD.FileFormats.Psd.Layers.SmartObjects;
using Aspose.PSD.ImageOptions;
new License().SetLicense("XXXXX");
using (var psd = new PsdImage(300, 100))
{
var sol = new SmartObjectLayer(getImage());
psd.AddLayer(sol);
psd.Save("mySmartTest.psd", new PsdOptions());
}
/* - */
static MemoryStream getImage()
{
var png = "iVBORw0KGgoAAAANSUhEUgAAAQ4AAAAyCAMAAACaoMX1AAACmlBMVEVHcEz////////////////////////a18n////////////S4M7///////////////////////////////////86lDn////////////////////////2+v3////////////////Alyr///////////////////////////////////////+5tT/////////////////////////////////////////////byiz///////////////9aoznrnk7///////////8smc5Ens3y1lB/sN7///9Bmsp2rUyyoRD3k0j////3fS0tl8/020DzaypNnTz///8tl81DmTyynw8rltH2di08lTr1cSr4jT4sl9S0nxA+mdN5r+W2sDr1eyu3pRRZpM1rqd/0dSr8gzj54kM5ksz2hS7byDL51lc9ljaTv0iQu+amvWG1ohL3hDMsmc46lDo5lDn5hzj+yWz3cSbzbSp7sEU3kzqxohCMu1H4fCSFs+D+tlSLrc84kzr8jT87lTqwsJ7ezDeXwUyxnQ/9jD2wnQ6ZwkWs0DxFmT9epDFBmDlUodJvqUgwltbp1i3/0HZvq0Sxn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