'System.ArgumentException: 'Parameter is not valid.' on drawing a rectangle with g.DrawRectangle()

I am designing a bill layout that is flexible with the length of the text so I am calculating the length of the text asynchronously and using its result to draw a rectangle. but when I execute the code I got an InvalidArgument exception.

here the code:

        private async void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            // Get Graphics
            var g = e.Graphics;

            // Constants
            const int FontSize = 16;
            const int PassegerFontSize = 12;

            // Brushes
            SolidBrush blackBrush = new SolidBrush(Color.Black);
            SolidBrush redBrush = new SolidBrush(Color.Red);

            // Pens
            Pen blackPen = new Pen(blackBrush, 2);

            // Constants
            const int MarginY = 3;

            // Fonts
            Font senderBoxFont = new Font(FontFamily.GenericSansSerif, FontSize);
            Font passengerBoxTitleFont = new Font(FontFamily.GenericSansSerif, PassegerFontSize, FontStyle.Bold);
            Font passengerBoxFont = new Font(FontFamily.GenericSansSerif, PassegerFontSize);
            FontFamily algerianfamily = FontFamily.GenericSansSerif;
            try
            {
                algerianfamily = new FontFamily("Algerian");
            }
            catch (Exception ex)
            {
                algerianfamily = FontFamily.GenericSansSerif;
            }
            Font algerianFont = new Font(algerianfamily, PassegerFontSize);

            // Calculating Header X
            int imgX = (e.PageBounds.Width - header.Width) / 2;
            int imgY = 15;

            // SenderInfo Box Details Calculation
            string invoiceString = $"INVOICE No :- {recordData.InvoiceID}";
            string dateString = $"Date :- {DateToSting(recordData.BillDate)}";
            SizeF invoiceStringSize = g.MeasureString(invoiceString, senderBoxFont);
            SizeF dateStringSize = g.MeasureString(dateString, senderBoxFont);
            int senderInfoY = imgY + header.Height;
            float invoiceX = e.PageBounds.Width - imgX - invoiceStringSize.Width - 10;
            float dateX = e.PageBounds.Width - imgX - dateStringSize.Width - 10;
            int senderInfoBoxHeight = (int)(5 + invoiceStringSize.Height + dateStringSize.Height);
            Rectangle rect = new Rectangle(imgX, senderInfoY, header.Width, senderInfoBoxHeight);

            // Main Header
            g.DrawImage(header, imgX, 15, header.Width, header.Height);

            // SenderInfo Box
            g.DrawRectangle(blackPen, rect);
            g.DrawString($"To, {recordData.CompanyName}", senderBoxFont, blackBrush, imgX + 10, senderInfoY + 5);
            g.DrawString($"Ref :- {recordData.ReferenceName}", senderBoxFont, blackBrush, imgX + 10, senderInfoY + FontSize + 5 + 10);
            g.DrawString(invoiceString, senderBoxFont, blackBrush, invoiceX, senderInfoY + 5);
            g.DrawString(dateString, senderBoxFont, blackBrush, dateX, senderInfoY + FontSize + 5 + 10);



            string title = "Sr.\nNo";
            SizeF titleSize = g.MeasureString(title, passengerBoxTitleFont);
            int titleBoxX = imgX;
            int titleBoxY = (header.Height + senderInfoBoxHeight + imgY + MarginY);
            int titleWidth = (int)(5 + titleSize.Width + 5);
            int titleHeight = (int)(5 + titleSize.Height + 5);
            Rectangle titleRect = new Rectangle(titleBoxX, titleBoxY, titleWidth, titleHeight);
            Rectangle titleBounds = new Rectangle(titleBoxX + 5, titleBoxY + 5, titleWidth, titleHeight);
            g.DrawRectangle(blackPen, titleRect);
            g.DrawString(title, passengerBoxTitleFont, blackBrush, titleBounds);

            string nop = "Name tOf Passenger";
            SizeF nopSize = g.MeasureString(nop, passengerBoxTitleFont);
            var task = CalculatePassengerNameWidth(passengerBoxTitleFont);
            int nopWidth = 0;

            Trace.WriteLine(nopWidth.ToString());
            int nopBoxX = imgX + titleWidth;
            int nopBoxY = (header.Height + senderInfoBoxHeight + imgY + MarginY);
            int nopHeight = (int)(5 + titleSize.Height + 5);



            foreach (var rec in recordData.Travellers)
            {

                var size = TextRenderer.MeasureText(rec.Name, passengerBoxTitleFont);
                if (nopWidth < size.Width)
                {
                    nopWidth = size.Width;
                }
            }

            int paddingX = (int)((nopWidth - nopSize.Width) / 2);
            int paddingY = (int)((nopHeight - nopSize.Height) / 2);

            Rectangle nopRect = new Rectangle(nopBoxX, nopBoxY, nopWidth, nopHeight);
            Rectangle nopBoudns = new Rectangle(nopBoxX + paddingX, nopBoxY, nopWidth, nopHeight);

            Trace.WriteLine($"{nopWidth}, {nopHeight}, {nopBoxX}, {nopBoxY}");
            Trace.WriteLine(nopRect.ToString());
            g.DrawRectangle(blackPen, nopRect);
            g.DrawString(nop, passengerBoxTitleFont, blackBrush, nopBoudns);


        }

here is the function that I am running for calculation:

        private async Task<float> CalculatePassengerNameWidth(Font font)
        {
            float maxWidth = 0;
            await Task.Run(() =>
            {

                foreach (var rec in recordData.Travellers)
                {

                    var size = TextRenderer.MeasureText(rec.Name, font);
                    if (maxWidth < size.Width)
                    {
                        maxWidth = size.Width;
                    }
                }

            });
            return maxWidth;
        }

Thanks In advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source