Aspose.Imaging exception "imaging loading failed" when converting heic to pdf

Getting the following exception when trying to load an heic file into Aspose.Imaging version 25.10 before saving to pdf. I have attached a sample file.

Aspose.Imaging.CoreExceptions.ImageLoadException
  HResult=0x80131500
  Message=Image loading failed.
  Source=Aspose.Imaging
  StackTrace:
   at Aspose.Imaging.Image.#=z5JWA_Z$A$9XR(StreamContainer #=zFY$Jfp$lhZrh, LoadOptions #=z6M65tOh0hoYb, #=zSDPvV3YaRdiB9siH6H2$QAQIsk2$wh9x5qHI7mU= #=zn5$qHPk=)
   at Aspose.Imaging.Image.Load(String filePath)
   at RevealIndexClientService.Application.UseCases.DocumentConversion.NativeView.NativeViewDocumentConverterAsposeHeic.<Convert>d__1.MoveNext() in G:\Reveal\Repositories\rent-18435-aspose-svg-pdf\Development\Reveal Services\RevealIndexClientService\RevealIndexClientService\Application\UseCases\DocumentConversion\NativeView\NativeViewDocumentConverterAsposeHeic.cs:line 35

  This exception was originally thrown at this call stack:
    System.ThrowHelper.ThrowKeyNotFoundException<T>(T) in ThrowHelper.cs
    System.Collections.Generic.Dictionary<TKey, TValue>.this[TKey].get(TKey) in Dictionary.cs
    Openize.Heic.Decoder.HeicImageFrame.LoadProperties(Openize.Heic.Decoder.IO.BitStreamWithNalSupport, System.Collections.Generic.List<Openize.IsoBmff.Box>)
    Openize.Heic.Decoder.HeicImageFrame.HeicImageFrame(Openize.Heic.Decoder.IO.BitStreamWithNalSupport, Openize.Heic.Decoder.HeicImage, uint, System.Collections.Generic.List<Openize.IsoBmff.Box>)
    Openize.Heic.Decoder.HeicImage.ReadFramesMeta(Openize.Heic.Decoder.IO.BitStreamWithNalSupport)
    Openize.Heic.Decoder.HeicImage.HeicImage(Openize.Heic.Decoder.HeicHeader, Openize.Heic.Decoder.IO.BitStreamWithNalSupport)
    Openize.Heic.Decoder.HeicImage.Load(System.IO.Stream)
    Aspose.Imaging.HEIC.Adapter.HEICImage.HEICImage(System.IO.Stream)
    Aspose.Imaging.HEIC.Adapter.Loader.HEICImageLoader.Load(Aspose.Imaging.StreamContainer, Aspose.Imaging.LoadOptions)
    Aspose.Imaging.Image.#=z5JWA_Z$A$9XR(Aspose.Imaging.StreamContainer, Aspose.Imaging.LoadOptions, #=zSDPvV3YaRdiB9siH6H2$QAQIsk2$wh9x5qHI7mU=)

Inner Exception 1:
KeyNotFoundException: The given key '122' was not present in the dictionary.

PROD_0000160.zip (2.4 MB)

@skowald

The “Image loading failed” exception you are encountering when converting HEIC to PDF using Aspose.Imaging may be due to an unsupported HEIC file format or a corrupted file. Here are a few steps you can take to troubleshoot and resolve the issue:

  1. Check File Integrity: Ensure that the HEIC file is not corrupted. Try opening it with a different application to verify its integrity.

  2. Update Aspose.Imaging: Make sure you are using the latest version of Aspose.Imaging, as updates may include fixes for known issues with file formats.

  3. Convert to Supported Format: If the HEIC file continues to cause issues, consider converting it to a more common format (like JPEG or PNG) using a different tool before processing it with Aspose.Imaging.

  4. Sample Code: Here’s a basic example of how to load a HEIC file and save it as a PDF:

    using Aspose.Imaging;
    using Aspose.Imaging.ImageOptions;
    
    // Load the HEIC file
    using (Image image = Image.Load("path/to/your/file.heic"))
    {
        // Save as PDF
        image.Save("output.pdf", new PdfOptions());
    }
    

If the problem persists after trying these steps, please provide additional details about the HEIC file or any error messages you receive for further assistance.

I’m using Aspose.Imaging 25.10 and that example code is exactly what I’m using. Can it be confirmed that this heic is not supported? I’m using Aspose so that I do not have to use another tool to convert to jpeg. Thanks!

Hi @skowald
We appreciate your interest in Aspose.Imaging.
We are sorry that you have faced with such a problem. We are going to check it out ASAP.

This issue was fixed in the Openize library, but the Adapter with this update was not released yet. It is planned for the next week.
However you can update the Openize.HEIC itself (that is used in Aspose.Imaging.HEIC.Adapter) to version 25.8.0 or higher and this will fix the problem.

I was facing the same issue.Initially it worked.Next day when I was testing the code,it didnt work.Even with the Openize.HEIC package version upgrade,it didn’t solve the issue. The below packages are used in my project “Aspose.Imaging” Version=“25.10.0” ,“Aspose.Imaging.Drawing” Version=“25.9.0”,
“Aspose.Imaging.HEIC.Adapter” Version=“25.10.0” ,“Openize.HEIC” Version=“25.9.0” />

@JohnAnish please provide the heic file you have issues with.

File_Heic.zip (287.2 KB)

Code base used:
MemoryStream heicStream = new MemoryStream();
stream.Position = 0;
stream.CopyTo(heicStream); // Copy the data to the stream for Aspose.Imaging
heicStream.Position = 0; // Reset the position before loading
Aspose.Imaging.Image Heicimage = Aspose.Imaging.Image.Load(heicStream);
Aspose.Imaging.ImageOptions.PngOptions pngOptions = new Aspose.Imaging.ImageOptions.PngOptions();
var pngStream = new MemoryStream();
Heicimage.Save(pngStream, pngOptions);
pngStream.Position = 0;
var pdfDocumentHeic = new Aspose.Pdf.Document();
var pageHeic = pdfDocumentHeic.Pages.Add();
var imageHeic = new Aspose.Pdf.Image
{
ImageStream = pngStream
};
pageHeic.Paragraphs.Add(imageHeic);
pdfDocumentHeic.Save(pdfStream);

Hello @JohnAnish ,

Looks like the code that you are trying to compile is not the most effective way of converting HEIC to PDF.
Please use the following code instead:

using Aspose.Imaging.HEIC.Adapter;
using Aspose.Imaging.ImageOptions;

string folder = @"c:\Users\USER\Downloads\folder\";

// register adapter
HEICImage.Register();

// Load the image file in an instance of Heic
using (var image = Aspose.Imaging.Image.Load(Path.Combine(folder, @"JOHNZ_HEIC_2_test.heic")))
{
    // Save image to pdf
    image.Save(Path.Combine(folder, "output.pdf"), new PdfOptions());
}

we are using filestream for document saving.Because of that approach we cannot use by accessing directly from the folder.Even with HEICImage.Register(); it was not working.
And moreover with the same code,heic conversion was happening. But suddenly it stopped working.

If we talk about the code provided by you, I have edited it a bit:

HEICImage.Register();

MemoryStream heicStream = new MemoryStream();
using (FileStream stream = new FileStream(Path.Combine(folder, @"JOHNZ_HEIC_2_test.heic"), FileMode.Open, FileAccess.Read))
    stream.CopyTo(heicStream);

heicStream.Position = 0;
Aspose.Imaging.Image Heicimage = Aspose.Imaging.Image.Load(heicStream);

var pngStream = new MemoryStream();
Heicimage.Save(pngStream, new PngOptions());

var pdfDocumentHeic = new Aspose.Pdf.Document();
var pageHeic = pdfDocumentHeic.Pages.Add();
var imageHeic = new Aspose.Pdf.Image
{
    ImageStream = pngStream
};
pageHeic.Paragraphs.Add(imageHeic);
pdfDocumentHeic.Save(Path.Combine(folder, "output2.pdf"));

This will work, but the image aspect in pdf file would be incorrect.

The part where you copy one heic stream to another may be ommited and the reading of the file will become much shorter:

HEICImage.Register();

Aspose.Imaging.Image Heicimage = Aspose.Imaging.Image.Load(
    new FileStream(Path.Combine(folder, @"JOHNZ_HEIC_2_test.heic"), FileMode.Open, FileAccess.Read));

var pngStream = new MemoryStream();
Heicimage.Save(pngStream, new PngOptions());

var pdfDocumentHeic = new Aspose.Pdf.Document();
var pageHeic = pdfDocumentHeic.Pages.Add();
var imageHeic = new Aspose.Pdf.Image
{
    ImageStream = pngStream
};
pageHeic.Paragraphs.Add(imageHeic);
pdfDocumentHeic.Save(Path.Combine(folder, "output2.pdf"));