Exception when drawing PdfBitmap

Using code below to drap various bitmaps inside pdf cell.

public void AddImage(Stream stream, float? width, float? height)

        {

            var bitmap = new PdfBitmap(stream);

            float h = height ?? bitmap.Height;

            h += this.cell.Style.CellPadding.Top + this.cell.Style.CellPadding.Bottom;

            this.row.Height = Math.Max(this.row.Height, h);

            float w = width ?? bitmap.Width;

            w += this.cell.Style.CellPadding.Left + this.cell.Style.CellPadding.Right;


            void BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args)

            {

                if (args.CellIndex == this.parentColumn.Index && args.Style.BackgroundImage != null)

                {

                    PdfImage backgroundOriginalImage = args.Style.BackgroundImage;

                    // Calculate the new image dimensions

                    float newWidth, newHeight;


                    //Calculate Aspect ratio of image.

                    float radiusX = (float)(args.Bounds.Width / backgroundOriginalImage.Width);

                    float radiusY = (float)(args.Bounds.Height / backgroundOriginalImage.Height);

                    float r = Math.Min(radiusX, radiusY);

                    newWidth = backgroundOriginalImage.Width * r;

                    newHeight = backgroundOriginalImage.Height * r;


                    //Draw image in center of the cell.

                    float centerX = args.Bounds.X + (args.Bounds.Width - newWidth) / 2;

                    float centerY = args.Bounds.Y + (args.Bounds.Height - newHeight) / 2;


                    args.Graphics.DrawImage(backgroundOriginalImage, new RectangleF(centerX, centerY, w, h));

                    args.Style.BackgroundImage = null;

                }


            this.grid.BeginCellLayout += BeginCellLayout;

            this.cell.Style.BackgroundImage = bitmap;

        }

Fields row, grid and cell are stored within class:

        private readonly PdfGridRow row;

        private readonly PdfGrid grid;

        private readonly PdfGridCell cell;


When drawing main grid with

          var layoutFormat = new PdfLayoutFormat { Break = PdfLayoutBreakType.FitPage, Layout =                    PdfLayoutType.Paginate };

            this.rootGrid.Draw(this.page, new PointF(0,0), layoutFormat);

            this.document.Save(stream);

            this.document.Close(true);

, exception is thrown:


-----<<Exception Message>>-----

Collection was modified; enumeration operation may not execute.


-----<<Exception Source>>-----

mscorlib


-----<<Exception Stack Trace>>-----

   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)

   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()

   at Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutOnPage(PdfLayoutParams param)

   at Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutInternal(PdfLayoutParams param)

   at Syncfusion.Pdf.Grid.PdfGridCell.Draw(PdfGraphics graphics, RectangleF bounds, Boolean cancelSubsequentSpans)

   at Syncfusion.Pdf.Grid.PdfGridLayouter.DrawRow(RowLayoutResult& result, PdfGridRow row, Single height)

   at Syncfusion.Pdf.Grid.PdfGridLayouter.DrawRow(PdfGridRow row)

   at Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutOnPage(PdfLayoutParams param)

   at Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutInternal(PdfLayoutParams param)

   at Syncfusion.Pdf.Grid.PdfGridCell.Draw(PdfGraphics graphics, RectangleF bounds, Boolean cancelSubsequentSpans)

   at Syncfusion.Pdf.Grid.PdfGridLayouter.DrawRow(RowLayoutResult& result, PdfGridRow row, Single height)

   at Syncfusion.Pdf.Grid.PdfGridLayouter.DrawRow(PdfGridRow row)

   at Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutOnPage(PdfLayoutParams param)

   at Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutInternal(PdfLayoutParams param)

   at Syncfusion.Pdf.Grid.PdfGridCell.Draw(PdfGraphics graphics, RectangleF bounds, Boolean cancelSubsequentSpans)

   at Syncfusion.Pdf.Grid.PdfGridLayouter.DrawRowWithBreak(RowLayoutResult& result, PdfGridRow row, Single height)

   at Syncfusion.Pdf.Grid.PdfGridLayouter.DrawRow(PdfGridRow row)

   at Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutOnPage(PdfLayoutParams param)

   at Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutInternal(PdfLayoutParams param)

   at Syncfusion.Pdf.Grid.PdfGrid.Layout(PdfLayoutParams param)

   at Syncfusion.Pdf.Graphics.PdfLayoutElement.Draw(PdfPage page, RectangleF layoutRectangle, PdfLayoutFormat format)

   at Syncfusion.Pdf.Graphics.PdfLayoutElement.Draw(PdfPage page, PointF location, PdfLayoutFormat format)



3 Replies

GK Gowthamraj Kumar Syncfusion Team December 27, 2021 03:52 PM UTC

Hi umlprog, 
 
We have tried to reproduce the reported exception with provided details on our end, but it is working properly. We have attached the sample and output document for your reference, please try the sample on your end and let us know the result. 
Please refer to the below link for more information, 
 
We request you to share the details such as modified sample or complete code snippet, input images, exception details to replicate the exception on our end. So that it will be helpful for us to analyze and provide a better solution. 
 
Regards, 
Gowthamraj K 



UM umlprog December 28, 2021 10:32 AM UTC

Thank you for your input.

Actually problem went away after implementing text wrapping, but not sure whether it's in other cells of a PdfGrid or cells that also contain images:

cell.Value = text;

UpdateRowHeight((string)this.cell.Value, this.cell.Style.Font);


        private void UpdateRowHeight(string text, PdfFont font)

        {

            font = font ?? new PdfStandardFont(PdfFontFamily.Helvetica, 8f); // Latter is the default pdf font.

            //measure the string height

            SizeF size = font.MeasureString(text);


            // find width

            float columnWidth = 0;

            for (int i = this.parentColumn.Index; i < this.parentColumn.Index + this.ColSpan; i++)

            {

                columnWidth += this.grid.Columns[i].Width;

            }


            //divide the text width and column width

            float lineCount = (int)Math.Ceiling(size.Width / columnWidth + 1);


            float heightNeeded = (size.Height + 1) * lineCount;


            if (this.row.Height < heightNeeded)

            {

                this.row.Height = heightNeeded;

            }

        }



GK Gowthamraj Kumar Syncfusion Team December 29, 2021 02:41 PM UTC


Hi umlprog, 

When checking the provided code snippet, we found that the some of grid values are using this values. We tried with static values, but we were not able to reproduce the exception. As we requested earlier, we request you to share the simplified sample with all resources to analyze the exception with your implementation on our end, so that it will be helpful for us to provide solution. 

Regards, 
Gowthamraj K 


Loader.
Up arrow icon