编辑大尺寸图像时出现System.OverflowException(A0,A1,A2)

时间:2016-01-27 07:00:42

标签: c# asp.net image-processing

我必须画出Lozenge& Lozenge中的文本,图像中具有用户给出的特定坐标。我已成功通过使用Graphics类来完成此操作 -

        var memoryStream = new MemoryStream();
        using (var bmp = new Bitmap(filePath, false))
        {
            using (var grp = Graphics.FromImage(bmp))
            {
                // Measure text size
                //SizeF textSize = grp.MeasureString(inputText1, font);
                var count = 0;
                var maxCount = (printPositions.Count < inputTexts.Count)
                    ? printPositions.Count
                    : inputTexts.Count;
                for (var i = 0; i < maxCount; i++)
                {
                    var printPosition = printPositions.ElementAt(i);

                    // Calculating font size as per DPI.
                    printPosition.FontSize = (printPosition.FontSize * 300) / 74;
                    var cornerRadius = CommonHelper.GetCornerRadius(size);
                    //Convert.ToInt32(printPosition.FontSize / 0.95);

                    if (printPosition.IsLozenge)
                    {
                        var recPoint = ImageServices.GetCoordinates(printPosition.PrintTextPosition);
                        var converter = new ColorConverter();
                        var color = Color.SlateGray;
                        if(!string.IsNullOrEmpty(printPosition.LozengeColor))
                        {
                            var colorString = converter.ConvertFromString(printPosition.LozengeColor) ??
                                               Color.SlateGray;
                        color = (Color)colorString;
                        }
                        using (var sbrush = new SolidBrush(color))
                        {
                            using (var gPath = new GraphicsPath())
                            {
                                gPath.AddLine(recPoint.X + cornerRadius, recPoint.Y,
                                    recPoint.X + recPoint.Width - (cornerRadius * 2), recPoint.Y);
                                gPath.AddArc(recPoint.X + recPoint.Width - (cornerRadius * 2), recPoint.Y,
                                    cornerRadius * 2, cornerRadius * 2, 270, 90);
                                gPath.AddLine(recPoint.X + recPoint.Width, recPoint.Y + cornerRadius,
                                    recPoint.X + recPoint.Width, recPoint.Y + recPoint.Height - (cornerRadius * 2));
                                gPath.AddArc(recPoint.X + recPoint.Width - (cornerRadius * 2),
                                    recPoint.Y + recPoint.Height - (cornerRadius * 2), cornerRadius * 2,
                                    cornerRadius * 2, 0, 90);
                                gPath.AddLine(recPoint.X + recPoint.Width - (cornerRadius * 2),
                                    recPoint.Y + recPoint.Height, recPoint.X + cornerRadius,
                                    recPoint.Y + recPoint.Height);
                                gPath.AddArc(recPoint.X, recPoint.Y + recPoint.Height - (cornerRadius * 2),
                                    cornerRadius * 2, cornerRadius * 2, 90, 90);
                                gPath.AddLine(recPoint.X, recPoint.Y + recPoint.Height - (cornerRadius * 2),
                                    recPoint.X, recPoint.Y + cornerRadius);
                                gPath.AddArc(recPoint.X, recPoint.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);

                                gPath.CloseFigure();
                                grp.FillPath(sbrush, gPath);
                            }
                        }
                    }

                    // Adding Padding from top & left
                    var positions = printPosition.PrintTextPosition.Split(',');
                    var newPosition = string.Empty;
                    for (var n = 0; n < positions.Count(); n++)
                    {
                        var position = positions.ElementAt(n);
                        if (n == 0)
                        {
                            var topPadding = CommonHelper.GetTopPadding(size, cornerRadius);
                            newPosition = (int.Parse(position) + topPadding).ToString(CultureInfo.InvariantCulture);
                            continue;
                        }
                        if (n == 1)
                        {
                            var leftPadding = CommonHelper.GetLeftPadding(size, cornerRadius);
                            newPosition += "," + (int.Parse(position) + leftPadding);
                            continue;
                        }
                        newPosition += "," + position;
                    }

                    Brush brush = new SolidBrush(Color.White);
                    var fontStyle = FontStyle.Regular;
                    if (printPosition.Font.Equals("Proxima Nova Rg") ||
                        printPosition.Font.Equals("HelveticaNeueLT Std"))
                        fontStyle = FontStyle.Bold;
                    var font = new Font(printPosition.Font, printPosition.FontSize, fontStyle,
                        GraphicsUnit.Pixel);
                    var ps = ImageServices.GetCoordinates(newPosition);
                    var point = new Point(ps.X, ps.Y);
                    var printText = (inputTexts.ElementAt(count).Length > printPosition.TextLimit)
                        ? inputTexts.ElementAt(count).Substring(0, printPosition.TextLimit)
                        : inputTexts.ElementAt(count);
                    grp.DrawString(printText, font, brush, point);
                    count++;
                }
                //Save the Watermarked image to the MemoryStream.
                bmp.Save(memoryStream, ImageFormat.Png);

但这仅适用于尺寸小于10MB的A3,A4,A5纸张尺寸。对于其他类似尺寸A0,A1,A2&amp;文件大小大于16MB,其抛出异常System.OverflowException。我的代码或C#中的任何其他方式是否有任何错误来实现这一目标?如果没有那么任何可以做到这一点的免费Nuget套餐? 我在64位机器上使用.Net framework 4.5。

0 个答案:

没有答案