Loading the image throws an error 'Image loading failed.'

I have a tif file which is to be converted to pdf file. To load the image, i tried passing tif file as the documentStream in
=> skbitmap = SKBitmap.Decode(documentStream); //the value of skbitmap becoming Null.
secondly tried loading the image uing Aspose.imaging,
=> image = Aspose.Imaging.Image.Load(documentStream); // this shows image null and i’m getting Image loading failed message.

P.s: I’m using .Net8 and The code should be compatible with Linux machine.

@Ajitha_Selvaraj

It seems you are encountering an issue with loading a TIFF file for conversion to PDF, resulting in an “Image loading failed” error. Here are some steps you can take to troubleshoot and resolve this issue:

  1. Check File Path and Stream: Ensure that the documentStream you are passing to the loading methods is valid and correctly points to the TIFF file. If the stream is null or the file path is incorrect, it will lead to loading failures.

  2. Using Aspose.Imaging: When using Aspose.Imaging to load the TIFF file, make sure you are casting the loaded image to TiffImage. Here’s a sample code snippet that demonstrates how to load a TIFF file and convert it to PDF:

    using Aspose.Imaging;
    using Aspose.Imaging.FileFormats.Tiff;
    using Aspose.Imaging.ImageOptions;
    using System.IO;
    
    public class TiffToPdfConverter
    {
        public static void ConvertTiffToPdf(string tiffFilePath, string outputPdfPath)
        {
            // Load the TIFF image
            using (TiffImage image = (TiffImage)Image.Load(tiffFilePath))
            {
                // Set PDF options with the image's resolution
                PdfOptions pdfOptions = new PdfOptions()
                {
                    ResolutionSettings = new ResolutionSetting(image.HorizontalResolution, image.VerticalResolution)
                };
    
                // Save the image as a PDF
                image.Save(outputPdfPath, pdfOptions);
            }
        }
    }
    
  3. Check TIFF File Validity: Ensure that the TIFF file you are trying to load is not corrupted and is in a supported format. You can test with a different TIFF file to see if the issue persists.

  4. Environment Compatibility: Since you are using .NET 8 on a Linux machine, ensure that all necessary dependencies for Aspose.Imaging are correctly installed and compatible with your environment.

If you continue to face issues, consider checking the Aspose.Imaging documentation or community forums for additional troubleshooting tips or similar cases.

If you have any further questions or need more assistance, feel free to ask!

Hello, @Ajitha_Selvaraj
We appreciate your interest in Aspose.Imaging.
Could you provide us full error stack and a file you try to load? Perhaps, this file is not a tiff or possible it is corrupted.

Hi, I couldn’t share the tif file or the stack due to privacy policies. But I can assure you that the file is not corrupted. I have made sure of it.

Can you possibly check with the below code?

// Create a new Document
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document();
pdfDocument.Pages.Add();

// Save the empty document to the output stream
pdfDocument.Save(outputStream);

// Load the image using SkiaSharp
using (var skBitmap = SKBitmap.Decode(imgFile))
{
if (skBitmap == null)
{
throw new InvalidOperationException(“Unable to decode image stream.”);
}

// Convert SKBitmap to a byte array
using (var skImage = SKImage.FromBitmap(skBitmap))
using (var skData = skImage.Encode(SKEncodedImageFormat.Png, 100))
{
    var imageBytes = skData.ToArray();
    var imageStream = new MemoryStream(imageBytes);

    // Use PdfFileMend to add the image to the PDF
    using (PdfFileMend pdfMend = new PdfFileMend())
    {
        pdfMend.BindPdf(outputStream);
        pdfMend.AddImage(imageStream, 1, 0, 0, skBitmap.Width, skBitmap.Height);
        pdfMend.Save(pdfFile);
    }
}

}

I’m testing this code in windows machine, but it will be deployed later on a Linux machine.
SKBitmap.Decode(imgFile) output is null. Please pour your suggestions to make it work

I tried with Aspose.Imaging too. But getting error at Aspose.Imaging.Image.Load(documentStream)

// Load the image using Aspose.Imaging
using (var image = Aspose.Imaging.Image.Load(documentStream))
{
// Convert the image to a byte array
using (var memoryStream = new MemoryStream())
{
image.Save(memoryStream, new Aspose.Imaging.ImageOptions.PngOptions());
var imageBytes = memoryStream.ToArray();
var imageStream = new MemoryStream(imageBytes);

    // Use PdfFileMend to add the image to the PDF
    using (PdfFileMend pdfMend = new PdfFileMend())
    {
        pdfMend.BindPdf(outputStream);
        pdfMend.AddImage(imageStream, 1, 0, 0, image.Width, image.Height);
        pdfMend.Save(outputStream);
    }
}

}

Unfortunately, we cannot guess what happens without additional data.
Does your code work on Windows without error with this file?
Could you provide detailed exception text and stack trace?