2009-08-05 16 views
12

कभी-कभी माइक्रोसॉफ्ट के अपवाद संदेश क्रूरता से अनुपयोगी होते हैं। मैंने पाठ प्रस्तुत करने के लिए एक अच्छी छोटी एमवीसी विधि बनाई है। विधि शरीर नीचे है। जब यह "ड्रॉस्ट्रिंग" विधि तक पहुंच जाता है, तो मुझे यह कहते हुए एक अपवाद मिलता है कि "पैरामीटर वैध नहीं है"।सिस्टम। ड्रॉइंग। ग्राफिक्स। ड्रास्ट्रिंग - "पैरामीटर मान्य नहीं है" अपवाद

ध्यान दें कि जितना संभव हो उतना फ़ॉन्ट ठीक से बनाया जा सकता है (मैं केवल 10pt पर एरियल का उपयोग कर रहा हूं), रेक्ट आकार सकारात्मक और मान्य दिख रहा है, ब्रश एक सफेद सॉलिडब्रश है और प्रारूप झंडे प्रभावित नहीं होते हैं आउटपुट; यानी अगर मैं कॉल से प्रारूप झंडे बाहर करते हैं, मैं अभी भी कोई त्रुटि मिलती है

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

बस एक ध्यान दें: नए SolidBrush (एफसी) एक ब्रश संसाधन रिसाव हो जाएगा, यह एक का उपयोग कर ब्लॉक भी जरूरत है। –

+1

बस एक टिप: मुझे वही त्रुटि थी "पैरामीटर वैध नहीं है" और इस धागे पर पहुंचा। मेरे मामले में इसका स्वीकार्य उत्तर से कोई लेना देना नहीं था, ऐसा इसलिए था क्योंकि मैं ड्रॉस्ट्रिंग को फ़ॉन्ट या ब्रश के एक विस्थापित उदाहरण के लिए पास कर रहा था। अपवाद वास्तव में सहायक नहीं है ... – 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

संबंधित मुद्दे