DrawText随机给出错误:参数无效

时间:2017-05-05 11:55:46

标签: c# system.drawing

我有一个错误:

  

参数无效。

此错误大约发生在5次中。

此行发生错误:

TextRenderer.DrawText(drawing, "Code12", font, fullWidthRectangle,
                                        textColor,
                                        flags);

小代码示例(非实际代码):

public Image CreateTripDetailPreview(Image image)
{
    using (var fontCollection = new PrivateFontCollection())
    using (var fontCollectionBold = new PrivateFontCollection())
    {
        fontCollection.AddFontFile("Assets\\SourceSansPro-Regular.ttf");
        fontCollectionBold.AddFontFile("Assets\\SourceSansPro-Bold.ttf");

        //This will be used to define heigt of text and allign text
        Rectangle fullWidthRectangle;

        var widthInDip = 360;
        var imgHeigtInDip = 168;
        var canvasWidth = 1080;
        var canvasHeight = 1200;
        var dip = canvasWidth / widthInDip;
        var leftRightMargin = 15 * dip;
        var resolutionScale = 5;

        using (Image img = new Bitmap(canvasWidth * resolutionScale, canvasHeight * resolutionScale))
         using (Graphics drawing = Graphics.FromImage(img))
         {
            //Clear 'background' and make it white
            drawing.Clear(Color.White);

            var imageHeight = (int)167.5 * dip;
            var height = imageHeight * resolutionScale;

            using (var cityImageBitmap = new Bitmap(image))
            using (var resizedCityImage = new Bitmap(cityImageBitmap, new Size(canvasWidth, imageHeight)))
            {
                canvasWidth *= resolutionScale;
                canvasHeight *= resolutionScale;
                dip *= resolutionScale;
                leftRightMargin *= resolutionScale;

                TextFormatFlags flags;
                using (var regularFont = new Font(fontCollection.Families[0], 1, FontStyle.Regular))
                using (var boldFont = new Font(fontCollectionBold.Families[0], 1, FontStyle.Regular)
                        ) //1 as default fontsize, fontsize will be calculated for each property
                {
                    Color textColor = Color.FromArgb(102, 102, 102);

                    //FlightCode
                    height += 4 * dip;
                    fullWidthRectangle = new Rectangle(leftRightMargin, height,
                    canvasWidth - leftRightMargin * 2,
                                    (int)22.5 * dip);
                    using (Font font = GetFontSizeByBox(drawing, "Code12",
                                    fullWidthRectangle.Size,
                                    regularFont))
                    {
                        flags = TextFormatFlags.NoPadding | TextFormatFlags.HorizontalCenter;
                        TextRenderer.DrawText(drawing, "Code12", font, fullWidthRectangle,
                                        textColor,
                                        flags);
                    }

                    using (var result = new Bitmap(img, canvasWidth / resolutionScale, canvasHeight / resolutionScale))
                    using (Graphics drawing2 = Graphics.FromImage(result))
                    {
                        drawing2.DrawImage(resizedCityImage, new Point(0, 0));
                        return new Bitmap(result);
                    }
                }
            }
        }
    }
}

GetFontSizeByBox方法:

    private static Font GetFontSizeByBox(Graphics g, string longString, Size room, Font preferedFont, int extraSize = 0)
    {
        SizeF realSize = g.MeasureString(longString, preferedFont);
        var heightScaleRatio = room.Height / realSize.Height;
        var widthScaleRatio = room.Width / realSize.Width;
        var scaleRatio = heightScaleRatio < widthScaleRatio ? heightScaleRatio : widthScaleRatio;
        var scaleFontSize = preferedFont.Size * scaleRatio;
        return new Font(preferedFont.FontFamily, scaleFontSize + extraSize, preferedFont.Style);
    }

备注

    此方法顶部的
  1. GC.Collect()修复了问题,我不希望使用此“修复”来防止它。
  2. 一次性使用陈述
  3. 该方法大部分时间都有效,是失败的1/5
  4. 发生错误时DrawText的值:

    • 图形对象(可访问的属性)
    • “Code12”常规字符串
    • font Name = "Source Sans Pro" Size=179.088287(可访问的属性)
    • 矩形X = 225 Y = 2565 Width = 4950 Height = 330
    • 颜色Name=ff666666, ARGB=(255, 102, 102, 102)
    • 标志TextFormatFlags.HorizontalCenter | TextFormatFlags.NoPadding

    如果有人知道我为什么会收到此错误或如何解决此问题,我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案,正如Hans Passant所说,GC.Collect();在方法的顶部将解决问题。这确实有效,但我想找出原因。

代码构造的方式所有一次性对象将在方法完成后处理(因为所有using语句),32bit应用程序中有足够的内存来运行此方法一次或两次。但是一旦调用该方法并且仍然存在一些垃圾,它就会在制作位图时崩溃。

<强>解决方案:
如果您遇到此问题并且每次运行方法时都不想强制进行垃圾回收,则可以设置如下阈值:

if( Process.GetCurrentProcess().PrivateMemorySize64 > someNumber )
{
    GC.Collect();
}

事实证明,这对我来说不起作用,因为每次都会达到阈值,所以我每次只能使用GC.Collect();。如果有人有更好的解决方案(不增加记忆力),请不要犹豫。