2009-08-05 18 views
12

時にはMicrosoftの例外メッセージが怒らないほど役に立ちません。私はテキストをレンダリングするためのすてきなMVCメソッドを作成しました。メソッド本体は以下の通りです。 "DrawString"メソッドに到達すると、 "Parameter is not valid"という例外がスローされます。System.Drawing.Graphics.DrawString - "パラメータが無効です"例外

フォントは、適切に構成されていることを確認してください(私は10ptでArialを使用しています)。rectサイズは正で有効です。ブラシは白色の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)は、それはあまりにも使用してブロックを必要とする、ブラシリソースをリークします。 –

+1

ちょうどヒント:私は同じエラー "パラメータが有効ではありませんでした"とこのスレッドに到着しました。私の場合、それは受け入れられた答えとは何の関係もありませんでした。なぜなら、私はDrawStringにDisposeのフォントまたはブラシのインスタンスを渡していたからです。例外は本当に有用ではありません... – AFract

答えて

14

は、まあ、私は、問題の原因を見つけました。非常にあいまいです。コードは次の行を削除すると動作します:

gfx.CompositingMode = CompositingMode.SourceCopy; 
+0

ライフセーバー!実際には、テキストを描画するためにブレンディングが必要であると確信していますが、エラーメッセージがそれについて何か言及していればいいでしょう:-) – eodabash

4

デバッグ時に役立つものは、メソッドを小さくすることです。たとえば、あなたはそれがあなたのコードが読みやすくなり、それはあなたを助けるために他の人のためにそれがより容易になります

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ただ、ノート

関連する問題