Performance issue adding image to document resources

We are trying to upgrade from Aspose.Pdf 8.3.1.0 to the latest version 19.1.0.0 due to restrictions/bugs in the older version that affect functionality we now need to implement. We are currently using a trial license for the new version while we convert our code to make sure it will work and have completed most of our development work but have run into a couple of issues. I will post separate threads for each so that they can be responded to individually.

This post is in reference to the AddImage functionality and is our top priority before we can even consider moving forward with Aspose! In the older version our code looked like this:

            ImageFileType BitmapType = GetAsposeImageType(bitmapType);

            Image mImage = new Image();
            mImage.Margin.Left = 0.0F;
            mImage.Margin.Top = 0.0F;
            mImage.Margin.Right = 0.0F;
            mImage.Margin.Bottom = 0.0F;
            mImage.ImageInfo.ImageFileType = BitmapType;
            mImage.Top = YPosition;
            mImage.Left = XPosition;
            mImage.ImageInfo.FixWidth = (bitmapWidth / bitmapHorizontalResolution) * Miscellaneous.POINTS_PER_INCH;
            mImage.ImageInfo.FixHeight = (bitmapHeight / bitmapVerticalResolution) * Miscellaneous.POINTS_PER_INCH;
            //mImage.PositioningType = PositioningType.PageRelative;
            mImage.ImageInfo.Alignment = AlignmentType.Center;
            mImage.ImageInfo.ImageStream = bitmapStream;


            FloatingBox fBox = new FloatingBox();
            fPdfSection.Paragraphs.Add(fBox);
            fBox.PositioningType = PositioningType.PageRelative;
            fBox.BoxHorizontalPositioning = BoxHorizontalPositioningType.Page;
            fBox.BoxVerticalPositioning = BoxVerticalPositioningType.Page;
            fBox.Top = mImage.Top;
            fBox.Left = mImage.Left;
            fBox.BoxWidth = mImage.ImageInfo.FixWidth;
            fBox.BoxHeight = mImage.ImageInfo.FixHeight;
            fBox.Paragraphs.Add(mImage);

In the new version we have tried several attempts such as:

double llx = fPdfPage.PageInfo.Margin.Left + XPosition;
double urx = llx + imageWidth;
double ury = GetPrintableTopPosition() - YPosition;
double lly = ury - imageHeight; ;
fPdfPage.AddImage(bitmapStream, new Aspose.Pdf.Rectangle(llx, lly, urx, ury));

And even:

            fPdfPage.Resources.Images.Add(bitmapStream); //THIS SPECIFIC LINE IS THE SLOWDOWN!
            // Using GSave operator: this operator saves current graphics state
            fPdfPage.Contents.Add(new Aspose.Pdf.Operators.GSave());
            // Create Rectangle and Matrix objects
            Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(llx, lly, urx, ury);
            Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });
            // Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
            fPdfPage.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
            XImage ximage = fPdfPage.Resources.Images[fPdfPage.Resources.Images.Count];
            // Using Do operator: this operator draws image
            fPdfPage.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));
            // Using GRestore operator: this operator restores graphics state
            fPdfPage.Contents.Add(new Aspose.Pdf.Operators.GRestore());

Both of the new versions are significantly slower. In the old version we were able to call our add image piece in under 29 milliseconds, and the new version takes 4.7 full seconds!

I have broken out the code to only Aspose code in a repeatable chunk for you here:

        Aspose.Pdf.Document myPdf = new Aspose.Pdf.Document();
        myPdf.PageInfo.Width = Aspose.Pdf.PageSize.PageLetter.Width;
        myPdf.PageInfo.Height = Aspose.Pdf.PageSize.PageLetter.Height;
        myPdf.PageInfo.Margin.Top = 36;
        myPdf.PageInfo.Margin.Bottom = 36;
        myPdf.PageInfo.Margin.Left = 24;
        myPdf.PageInfo.Margin.Right = 24;
        Aspose.Pdf.Page myPage = myPdf.Pages.Add();
        myPage.SetPageSize(Aspose.Pdf.PageSize.PageLetter.Width, Aspose.Pdf.PageSize.PageLetter.Height);
        myPage.PageInfo.Margin = myPage.PageInfo.Margin;
        byte[] myImageBytes = System.IO.File.ReadAllBytes(@"C:\Temp\Overlay2.png"); //prove that it isn't the disk read
        Stream myImageStream = new MemoryStream(myImageBytes); //convert to a stream
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();
        myPage.AddImage(myImageStream, new Aspose.Pdf.Rectangle(0, 0, Aspose.Pdf.PageSize.PageLetter.Width, Aspose.Pdf.PageSize.PageLetter.Height));
        sw.Stop();
        Console.WriteLine("Time for AddImage method: " + sw.Elapsed);
        myPdf.Save(@"C:\Temp\Overlay.pdf");

Please help us figure out how to add an image to an existing PDF in a timely matter, thank you. Image used: Overlay.png (299.2 KB)

@TheMitasGroup

Thank you for contacting support.

We have been able to notice slow performance while adding images. A ticket with ID PDFNET-46033 has been logged in our issue management system for further investigations. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.

It does not appear to be slow for all image types. We have found BMP, JPEG, GIF to be reasonably fast, but PNGs are what are quite slow.

@TheMitasGroup

Thank you for the information.

We have recorded your comments and will update you as soon as any significant update will be available in this regard.

The issues you have found earlier (filed as PDFNET-46033) have been fixed in Aspose.PDF for .NET 22.11.