System.Drawing.Graphics.DrawString - “参数无效”异常

问题描述:

有时候,微软的异常消息真是无益。我创建了一个很好的小MVC方法来呈现文本。方法体在下面。当它到达“DrawString”方法时,我得到一个抛出的异常,说“参数无效”。System.Drawing.Graphics.DrawString - “参数无效”异常

请注意,最好的我可以告诉的字体是正确构造的(我只是在10pt使用Arial),矩形大小是正面和有效的看,刷子是一个白色的SolidBrush和格式标志不影响输出;也就是说,如果我排除呼叫的格式标志,我还得到一个错误

拉绳调用正好在底部附近

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], font, new SolidBrush(fc), rect, fmt); 
     } 
     bmp.Save(Response.OutputStream, ImageFormat.Png); 
    } 
    return new EmptyResult(); 
} 
+1

刚一说明:新SolidBrush(FC)会泄漏一个刷机资源,它需要使用块了。 – 2009-08-05 09:47:11

+1

只是一个提示:我有同样的错误“参数无效”,并在此线程抵达。在我的情况下,它与接受的答案无关,这是因为我传递给DrawString一个Disposed字体或画笔的实例。这个例外是不是真的有用... – AFract 2016-04-06 15:29:54

好吧,我发现了问题的原因。有些东西很晦涩,当我删除这行代码时,代码起作用:

gfx.CompositingMode = CompositingMode.SourceCopy; 

什么可以帮助调试,使方法更小。例如,您可以通过

FontStyle style = GetFontStyle(isBold, isItalic); 

public FontStyle GetFontStyle(bool isBold, bool isItalic) 
{ 
    if(isBold) 
     if(isItalic) 
      return FontStyle.Bold | FontStyle.Italic; 
     else 
      return FontStyle.Bold; 
    else if(isItalic) 
     return FontStyle.Italic; 
    else 
     return FontStyle.Regular; 
} 

它使你的代码更易读取代

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

,并使得它更容易让别人来帮助你。

真的没有冒犯的意思!

亲切的问候, 答Vlug