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;

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...