Hello, I am updating the XMP metadata of a PSB file approximately 2.7GB in size. However, upon saving the file, I encountered the following error:
“System.IndexOutOfRangeException: Index was outside the bounds of the array.”
This issue does not occur when working with PSB files smaller than 1GB. Below, I’ve included the code and the complete stack trace of the error for reference.
Aspose.PSD version 24.11.0
private bool UpdateXmpMetadata(UpdateXmpMetadataMediaAction action)
{
if (!string.IsNullOrEmpty(action.XmpMetadataMapping))
{
PsdImage img = null;
// First attempt to load the PSD/PSB file
try
{
using (var inputStream = File.OpenRead(action.Path))
{
img = (PsdImage)Image.Load(inputStream);
}
}
catch (ImageLoadException e)
{
// Cannot suppport PSD/PSB files with Multichannel or Duotone color mode at this time due to known Aspose bug
if (e.Message.ToLower().Contains("image") && e.InnerException.Message.ToLower().Contains("color mode") && e.InnerException.Message.ToLower().Contains("compression"))
{
throw new MediaEngineException("PSD/PSB files with Multichannel or Duotone color mode are not supported.", false);
}
}
catch (Exception)
{
// Something unexpected went wrong while loading
throw;
}
// Since we loaded successfully, process it
using (img)
{
// Cannot suppport PSD/PSB files with Lab color mode which have 16 bits per channel at this time due to known Aspose bug
if (img.ColorMode == ColorModes.Lab && img.BitsPerChannel == 16)
{
throw new MediaEngineException("PSD/PSB files with Lab color mode which have 16 bits per channel are not supported.", false);
}
// Cannot suppport PSD/PSB files with CMYK color mode which have less than 4 or more than 5 channels at this time due to known Aspose bug
if (img.ColorMode == ColorModes.Cmyk && (img.ChannelsCount < 4 || img.ChannelsCount > 5))
{
throw new MediaEngineException("PSD/PSB files with CMYK color mode must have 4 or 5 channels.", false);
}
// Cannot suppport PSD/PSB files with RGB/Lab color mode which have less than 3 or more than 4 channels at this time due to known Aspose bug
if ((img.ColorMode == ColorModes.Rgb || img.ColorMode == ColorModes.Lab) && (img.ChannelsCount < 3 || img.ChannelsCount > 4))
{
throw new MediaEngineException("PSD/PSB files with RGB/Lab color must have 3 or 4 channels.", false);
}
XDocument xmpMapping = XDocument.Parse(action.XmpMetadataMapping);
// Getting the xmp metadata
XmpPacketWrapper xmpData = img.XmpData;
if (xmpData == null)
{
// Create xmp metadata if the image doesn't contains any xmp data.
XmpHeaderPi xmpHeader = new XmpHeaderPi(Guid.NewGuid().ToString());
XmpTrailerPi xmpTrailer = new XmpTrailerPi(true);
XmpMeta xmpMeta = new XmpMeta();
xmpData = new XmpPacketWrapper(xmpHeader, xmpTrailer, xmpMeta);
img.XmpData = xmpData;
}
foreach (var mapping in xmpMapping.Descendants("xmpMapping").Elements("add"))
{
var xmpNamespace = mapping.Attribute("namespace").Value;
var xmpName = mapping.Attribute("name").Value;
var xmpPrefix = mapping.Attribute("prefix").Value;
var xmpValue = mapping.Value;
if (xmpData.ContainsPackage(xmpNamespace))
{
// Update or add XMP property value in existing namespace
foreach (var xmpPackage in xmpData.Packages)
{
if (xmpPackage.NamespaceUri.Trim().ToLower() == xmpNamespace.Trim())
{
if (xmpPackage.ContainsKey(xmpPackage.Prefix + ":" + xmpName))
{
xmpPackage[(xmpPackage.Prefix + ":" + xmpName)] = xmpValue;
}
else
{
xmpPackage.AddValue(xmpPackage.Prefix + ":" + xmpName, xmpValue);
}
}
}
}
else
{
// Create XMP namespace, since it does not yet exist
XmpBasicPackage xmp = new XmpBasicPackage(xmpPrefix, xmpNamespace);
xmp.AddValue(xmpPrefix + ":" + xmpName, xmpValue);
xmpData.AddPackage(xmp);
}
}
img.Save(action.OutputPath);
}
return true;
}
return false;
}
Adam.Core.MediaEngines.MediaEngineException: Action UpdateXmpMetadata failed to execute using all engines. The last engine (AsposePsd) failed with error "Index was outside the bounds of the array.". ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Aspose.PSD.FileFormats.Psd.Layers.ChannelInformation.(Byte[] , Size , Rectangle )
at ..(Dictionary`2 , Size , Rectangle )
at ..Process(Rectangle , Int32[] , Point , Point )
at .. (Rectangle )
at .( , IList`1 , )
at .(Rectangle , , , Int32 , Int32 , )
at .(Rectangle , , )
at .(Object )
at .(MethodBase , Boolean )
at .()
at .(Boolean )
at .(Object )
at .()
at .(Object , UInt32 )
at .(Boolean )
at .(Object[] , Type[] , Type[] , Object[] )
at .LoadPartialArgb32Pixels(Rectangle , IPartialArgb32PixelLoader )
at . (Rectangle )
at .( , IList`1 , )
at .(Rectangle , , , Int32 , Int32 , )
at . (Rectangle , IPartialArgb32PixelLoader )
at .(Int32 , IColorPalette , , Int32 )
at .(StreamContainer , Int32 , IColorPalette , , Int32 )
at Aspose.PSD.FileFormats.Psd.PsdImage.SaveData(Stream stream)
at Aspose.PSD.DataStreamSupporter.Save(Stream stream)
at Aspose.PSD.DataStreamSupporter.Save(String filePath)
at Adam.Core.MediaEngines.AsposePsdMediaEngine.UpdateXmpMetadata(UpdateXmpMetadataMediaAction action)
at Adam.Core.MediaEngines.MediaManager.RunAction(MediaAction action, IMediaEngine engine, Exception& exception)
--- End of inner exception stack trace ---
at Adam.Core.MediaEngines.MediaManager.LogActionFailure(MediaAction action, IMediaEngine lastFailedEngine, Exception lastException)
at Adam.Core.MediaEngines.MediaManager.RunInternal(IEnumerable`1 engines, IEnumerable`1 actions)
at Adam.Core.Orders.UpdateXmpMetadataOrderTargetAction.OnExecute()
at Adam.Core.Orders.OrderRunner.ExecuteRegisteredActions()
Here is the test file
https://drive.google.com/file/d/10Jbf4nEFWd6zRCbiPbYghBUyC0sP3JY7/view?usp=sharing