c# – System.Drawing.Graphics.DrawString – “参数无效”异常

前端之家收集整理的这篇文章主要介绍了c# – System.Drawing.Graphics.DrawString – “参数无效”异常前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有时候,微软的例外消息是非常无益的.我创建了一个不错的MVC方法来渲染文本.方法体在下面.当它达到“DrawString”方法时,我收到一个异常,指出“参数无效”.

注意,最好我可以指出的字体是正确构造的(我只是使用10pt的Arial),rect的大小是正的和有效的,画笔是一个白色的SolidBrush,格式标志不影响输出; ie如果我从调用中排除格式标志,我仍然收到错误.

DrawString调用就在底部附近.

public ActionResult RenderText(
    string fontFamily,float pointSize,string foreColor,string backColor,bool isBold,bool isItalic,bool isVertical,string align,string[] allText,int textIndex)
{
    // method renders a horizontal or vertical text image,taking all the text strings that will be rendered in each image
    // and sizing the final bitmap according to which text would take the most space,thereby making it possible to render
    // a selection of text images all at the same size.

    Response.ContentType = "image/png";

    var fmt = StringFormat.GenericTypographic;
    if(isVertical)
        fmt.FormatFlags = StringFormatFlags.DirectionVertical;

    Func<string,StringAlignment> getAlign = (s => {
        switch(s.ToLower())
        {
            case "right": return StringAlignment.Far;
            case "center": return StringAlignment.Center;
            default: return StringAlignment.Near;
        }
    });
    fmt.LineAlignment = isVertical ? StringAlignment.Center : getAlign(align);
    fmt.Alignment = isVertical ? getAlign(align) : StringAlignment.Center;

    var strings = (allText ?? new string[0]).Where(t => t.Length > 0).ToList();
    if(strings.Count == 0)
        strings.Add("[Missing Text]");

    FontStyle style = FontStyle.Regular;
    if(isBold)
        if(isItalic)
            style = FontStyle.Bold | FontStyle.Italic;
        else
            style = FontStyle.Bold;
    else if(isItalic)
        style = FontStyle.Italic;

    Font font = new Font(fontFamily,pointSize,style,GraphicsUnit.Point);
    Color fc = foreColor.IsHexColorString() ? foreColor.ToColorFromHex() : foreColor.ToColor();
    Color bc = backColor.IsHexColorString() ? backColor.ToColorFromHex() : backColor.ToColor();

    var maxSize = new Size(0,0);
    using(var tmp = new Bitmap(100,200))
        using(var gfx = Graphics.FromImage(tmp))
            foreach(var txt in strings)
            {
                var size = gfx.MeasureString(txt,font,1000,fmt);
                maxSize = new Size(
                    Math.Max(Convert.ToInt32(isVertical ? size.Height : size.Width),maxSize.Width),Math.Max(Convert.ToInt32(isVertical ? size.Width : size.Height),maxSize.Width)
                );
            }

    using(var bmp = new Bitmap(maxSize.Width,maxSize.Height))
    {
        using(var gfx = Graphics.FromImage(bmp))
        {
            gfx.CompositingMode = CompositingMode.SourceCopy;
            gfx.CompositingQuality = CompositingQuality.HighQuality;
            gfx.SmoothingMode = SmoothingMode.HighQuality;
            gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;

            var rect = new RectangleF(new PointF(0,0),maxSize);
            gfx.FillRectangle(new SolidBrush(bc),rect);
            gfx.DrawString(strings[textIndex],new SolidBrush(fc),rect,fmt);
        }
        bmp.Save(Response.OutputStream,ImageFormat.Png);
    }
    return new EmptyResult();
}

解决方法

我发现问题的原因.有些东西很模糊当我删除此行时,代码工作:
gfx.CompositingMode = CompositingMode.SourceCopy;
原文链接:https://www.f2er.com/csharp/93373.html

猜你在找的C#相关文章