HEIC Conversion to JPEG, image export failed

Hello,

I cannot convert a heic image into jpeg image. What I intended to do is to scale the image and save it in a different format.

However when I want to save it via calling image.Save I get this error:

Image export failed

IndexOutOfRangeException: Index was outside the bounds of the array.

In terms of packages I use Aspose.Imaging (version 24.9.0) and Aspose.Imaging.HEIC.Adapter (version 24.9.0).

This is the code I use:

var resultStream = new MemoryStream();
var heicLoadOptions = HEICLoadOptions.Create();
using (var image = (HEICImage)Aspose.Imaging.Image.Load(sourceFileStream, heicLoadOptions))
{
    var aspectRatio = (double)image.Width / image.Height;
    var newHeight = (int)(size / aspectRatio);

    var options = new Aspose.Imaging.ImageOptions.JpegOptions
    {
        CompressionType = Aspose.Imaging.FileFormats.Jpeg.JpegCompressionMode.Progressive,
        VectorRasterizationOptions = new Aspose.Imaging.ImageOptions.VectorRasterizationOptions()
        {
            PageWidth = size,
            PageHeight = newHeight,
            SmoothingMode = isHighQuality ? Aspose.Imaging.SmoothingMode.HighQuality : Aspose.Imaging.SmoothingMode.Default,
        }
    };
    image.Save(resultStream, options); // this is where the code breaks

And this is the image I tried to convert:
1165.zip (2.8 MB)

Can you please help me to solve this issue?

@Milijanovic00
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): IMAGINGNET-7460

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.

Hi @Milijanovic00
This will be fixed in the next version.

Hi @Nikita_Romanov ,

Thank you for the information

Hi @Nikita_Romanov ,

Can you please give me some expected date on which it’ll be finished?

And one more thing, is this fix going to be provided in the new version of Aspose.Imaging or in Aspose.Imaging.HEIC.Adapter?

The reason I ask is that I noticed there is a newer version of Aspose.Imaging which is 24.11.0 and I switched to it but when I tried to convert the heic image I got an error saying something like “This version of Aspose.Imaging.HEIC.Adapter is too old for this new version of Aspose.Imaging”.

@Milijanovic00
I think that will take a week or two and it will be released as a new version of Aspose.Imaging.HEIC.Adapter.

We’ll also check if we can address this problem.

Hi @Nikita_Romanov,

Thank you for the information.

Hello @Milijanovic00 ,

Aspose.Imaging.HEIC.Adapter was updated to 24.11.0 with the fix.
Also we have applied the new dependency strategy for adapters and now you can use newer Aspose.Imaging versions with an older apapter (the version difference cannot be more then 3 months, but there is no more strict version dependecy).

Hello @Nikita_Romanov,

Thank you for providing the fix.

Have a great day :slight_smile:

Hello,

I encountered the same issue described earlier when trying to convert a HEIC image to JPEG using Aspose.Imaging (version 24.11.0) and Aspose.Imaging.HEIC.Adapter (version 24.11.0). While testing, I observed that the problem persists when uploading multiple HEIC files for conversion and processing in sequence, an IndexOutOfRangeException occurs specifically during the image.Save operation for the second file.

This behavior appears to indicate that the issue is not isolated to specific image files but may also occur in scenarios involving multiple file processing. Could you please investigate this further?

Thank you

@VinayakaP619 Hello!
Do I understand you correctly that you call the above code in a loop, and at some point you get an error?
I will try to reproduce this code.

I would also like to note that this part of the code is redundant, it is used only for rasterization of vector images:

        VectorRasterizationOptions = new Aspose.Imaging.ImageOptions.VectorRasterizationOptions()
        {
            PageWidth = size,
            PageHeight = newHeight,
            SmoothingMode = isHighQuality ? Aspose.Imaging.SmoothingMode.HighQuality : 
            Aspose.Imaging.SmoothingMode.Default,
        }

@VinayakaP619
I checked the work with this example, it works without errors.

var files = Directory.GetFiles(@"D:\samples", "*.heic");
var heicLoadOptions = HEICLoadOptions.Create();
foreach (var file in files)
{
    Console.WriteLine($"file={Path.GetFileName(file)}");
    using (var resultStream = new MemoryStream())
    using (var sourceFileStream = File.Open(file, FileMode.Open))
    using (var image = (HEICImage)Aspose.Imaging.Image.Load(sourceFileStream, heicLoadOptions))
    {
        var options = new Aspose.Imaging.ImageOptions.JpegOptions
        {
            CompressionType = Aspose.Imaging.FileFormats.Jpeg.JpegCompressionMode.Progressive,
        };

        image.Save(resultStream, options);
    }
}

Maybe you can share the files where the error occurs?

Hello @stanislav.popov ,
If multiple HEIC files are processed from an Azure Queue Trigger with parallel processing (e.g., batch size 3), an NullReferenceException: Object reference not set to an instance of an object. occurs during the image.Save operation. This issue does not occur when the batch size is set to 1.
The zip file contains a list of HEIC images that I attempted to convert:
HEIC_FILES.zip (8.4 MB)

Hello @VinayakaP619 ,
We will research the parallel processing and look for the reason of that behavior.
Can you please provide the code that throws this exception for you? Any additional data may help us with searching the root of the problem.

Hello @Nikita_Romanov ,
Here’s a code snippet that reproduces the issue:

private static ConversionResultData ScaleHeicImageViaAspose(Stream sourceFileStream, int size, bool isHighQuality)
        {
            var resultStream = new MemoryStream();

            try
            {
                var heicLoadOptions = HEICLoadOptions.Create();
                using var image = (HEICImage)Aspose.Imaging.Image.Load(sourceFileStream, heicLoadOptions);
                var aspectRatio = (double)image.Width / image.Height;
                var newHeight = (int)(size / aspectRatio);

                var options = new Aspose.Imaging.ImageOptions.JpegOptions
                {
                    CompressionType = Aspose.Imaging.FileFormats.Jpeg.JpegCompressionMode.Progressive,
                    VectorRasterizationOptions = new Aspose.Imaging.ImageOptions.VectorRasterizationOptions()
                    {
                        PageWidth = size,
                        PageHeight = newHeight,
                        SmoothingMode = isHighQuality ? Aspose.Imaging.SmoothingMode.HighQuality : Aspose.Imaging.SmoothingMode.Default,
                    }
                };
                image.Save(resultStream, options); // for parallel processing this is where the code breaks with NullReferenceException: Object reference not set to an instance of an object

                resultStream.Seek(0, SeekOrigin.Begin);

                return new ConversionResultData() { Stream = resultStream, FileWidth = size, FileHeight = newHeight };
            }
            catch (Exception ex)
            {
                resultStream.Dispose();

                return new ConversionResultData() { Exception = ex };
            }
        }

Thank you for the code snippet.
Managed to locate the issue. This will be fixed in the next version.

Also please note that this code is not suitable for resizing images.

Please, use image.resize instead.

Hello @Nikita_Romanov,
Thanks for the information. Could you please share an expected release date for the next version?

@VinayakaP619 ,

there are several other issues with HEIC that need our attention, so it is likely to enter 25.02 release, if there is no release difficulties. Please pay attention that HEIC release is a separate from Aspose.Imaging and may occurr in the second half of the month.

Best regards!

Hello @VinayakaP619 ,

Aspose.Imaging.HEIC.Adapter was updated to 25.2.0 with the fix of the issue you have reported in this topic.