Issue with the PSDImage.Save() method

I hope this message finds you well.

While using Aspose.PSD for .NET, I encountered an issue with the PSDImage.Save() method. Please see the details below:

Failed to save PSD: Exception calling "Save" with "1" argument(s):
"Source array was not long enough. Check the source index, length, and the array's lower bounds. (Parameter 'sourceArray')"

I am working with very large, high-resolution PSD files. During the internal execution of the Save() method, it seems that an obfuscated internal method:

   at .[T](IList`1 , Int64, IList`1 , Int64 , Int64 )

is eventually calling:

   System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)

From the logs, I can see:

Private Memory: 3162.7 MB 

Since this exceeds Int32.MaxValue (=0x7FFFFFC7 (2,147,483,591)), I suspect that an Int64 parameter is being converted to Int32 internally, which may result in a negative value being passed and ultimately causing the following error:“Source array was not long enough. Check the source index, length, and the array’s lower bounds. (Parameter ‘sourceArray’)”

If my assumption is correct, it seems this issue may be a fundamental limitation of the .NET/Aspose.PSD architecture. Could you please confirm whether this analysis is correct?

Also, is there any recommended workaround to avoid this problem?

For reference, here are the relevant debug logs:

    DEBUG: Final memory cleanup before saving...
    DEBUG: Saving PSD with 12 converted layers
    DEBUG: Current layer count: 94
    DEBUG: PSD dimensions: 3508x4967
WARNING:     Failed to save PSD: Exception calling "Save" with "1" argument(s): "Source array was not long enough. Check the source index, length, and the array's lower bounds. (Parameter 'sourceArray')"
    DEBUG: Save exception details: MethodInvocationException
    DEBUG: Inner exception: System.ArgumentException: Source array was not long enough. Check the source index, length, and the array's lower bounds. (Parameter 'sourceArray')
   at System.Array.CopyImpl(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
   at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)
   at .[T](IList`1 , Int64, IList`1 , Int64 , Int64 )
   ...
    DEBUG: Current memory usage before error:
    DEBUG: Working Set: 3005.62 MB
    DEBUG: Private Memory: 3162.7 MB
    DEBUG: PSD instance disposed
    DEBUG: Memory after cleanup - Working Set: 2742.32 MB, Private: 2867.75 MB
WARNING:   ✗ Failed to process 004074e3-7c95-470e-9777-ad8955de45d7

I would greatly appreciate your guidance on whether this is indeed a limitation of the framework and whether there are any recommended best practices to work around it.

Inside the PSDImage.Save() method in Aspose.PSD for .NET, before calling System.Array.Copy() , if the parameters could be checked against Int32.MaxValue , and the size divided so that System.Array.Copy() is executed multiple times, I believe this might provide a way to avoid the error.

Thank you for your support.

Best regards,

@tkim

It sounds like you’re encountering a limitation with the PSDImage.Save() method when working with large, high-resolution PSD files. The error message you’re seeing indicates that there may be an issue with the internal handling of array sizes, particularly when the data exceeds the maximum value for an Int32.

Your analysis about the potential conversion from Int64 to Int32 leading to a negative value is plausible, especially given the memory usage you reported. This could indeed be a limitation of the framework when dealing with very large images.

Cause:

The issue arises from the handling of large image data, where the size exceeds the maximum value for an Int32, potentially leading to negative values during processing.

Solution:

  1. Check Image Size: Ensure that the dimensions and the number of layers in your PSD file are within reasonable limits. If possible, try reducing the resolution or the number of layers to see if that resolves the issue.

  2. Split the Image: If the image is too large, consider splitting it into smaller sections and saving them individually. This can help avoid exceeding the array size limits.

  3. Update Aspose.PSD: Ensure you are using the latest version of Aspose.PSD, as updates may include fixes for such issues.

  4. Contact Support: If the problem persists, consider reaching out to Aspose support with your specific use case and the error details. They may provide a more tailored solution or workaround.

Next Steps:

Here’s a minimal code snippet to illustrate how you might handle saving in smaller chunks:

using (PsdImage psdImage = (PsdImage)Image.Load("largeImage.psd"))
{
    // Example of saving in chunks
    for (int i = 0; i < psdImage.Layers.Count; i++)
    {
        // Save each layer separately or in smaller groups
        psdImage.Layers[i].Save($"layer_{i}.png", new PngOptions());
    }
}

Please try these steps and let me know if this resolves your issue or if you have any further questions!

I am not having an issue with the Save method of Layer, but rather with the Save method of PsdImage.

PsdImage Image.Save | Aspose.PSD for .NET API Reference

@tkim could you please provide input file for testing. Please upload it to the some cloud storage if the file is large for the forum.