2010-01-15 15 views
47

कोई भी इनपुट टेक्स्ट से छवि उत्पन्न करने का मार्गदर्शन कर सकता है। छवि में कोई विस्तार नहीं हो सकता है।रनटाइम पर फ्लाई पर टेक्स्ट से छवि कैसे उत्पन्न करें

+0

क्या आपका मतलब है आप की तरह एक छवि एक स्क्रीन पर कब्जा से प्राप्त होता है के लिए उपयोग कर उपयोग कैसे करें? निश्चित रूप से, * कुछ * प्रारूप/एक्सटेंशन दूसरों की तुलना में बेहतर होंगे। – pavium

+0

आपका किस प्रकार का टेक्स्ट इनपुट मतलब है? –

+0

नहीं, यह स्क्रीन कैप्चर नहीं है हमारे पास इनपुट टेक्स्टबॉक्स है और हम सी # – Ravia

उत्तर

123

ठीक है, यह सोचते हैं आप सी # में एक छवि पर एक स्ट्रिंग आकर्षित करने के लिए चाहते हैं, आप यहाँ System.Drawing नाम स्थान उपयोग करने की आवश्यकता करने जा रहे हैं:

private Image DrawText(String text, Font font, Color textColor, Color backColor) 
{ 
    //first, create a dummy bitmap just to get a graphics object 
    Image img = new Bitmap(1, 1); 
    Graphics drawing = Graphics.FromImage(img); 

    //measure the string to see how big the image needs to be 
    SizeF textSize = drawing.MeasureString(text, font); 

    //free up the dummy image and old graphics object 
    img.Dispose(); 
    drawing.Dispose(); 

    //create a new image of the right size 
    img = new Bitmap((int) textSize.Width, (int)textSize.Height); 

    drawing = Graphics.FromImage(img); 

    //paint the background 
    drawing.Clear(backColor); 

    //create a brush for the text 
    Brush textBrush = new SolidBrush(textColor); 

    drawing.DrawString(text, font, textBrush, 0, 0); 

    drawing.Save(); 

    textBrush.Dispose(); 
    drawing.Dispose(); 

    return img; 

} 

इस कोड को पहली स्ट्रिंग को मापने, एक और फिर सही आकार की एक छवि बनाएँ।

यदि आप इस फ़ंक्शन की वापसी को सहेजना चाहते हैं, तो बस लौटाई गई छवि की सहेजें विधि को कॉल करें।

+6

लाइन का उपयोग कर रहे हैं "छवि img = new बिटमैप (0, 0);", काम नहीं करता है: आप 0-आकार वाली छवि नहीं बना सकते हैं। इसे "नया बिटमैप (1, 1)" में बदलें, यह काम करता है। – neminem

+3

यदि आप 'ड्राइंग .extRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;' 'ड्राइंग से पहले 'जोड़ें। ड्रॉस्ट्रिंग (टेक्स्ट, फ़ॉन्ट, टेक्स्टब्रश, 0, 0);' लाइन, आपको चिकनी एंटी-एलाइज्ड टेक्स्ट मिलेगा । – LoneBunny

3

चित्र पर पाठ प्रतिपादन के लिए उपयोग imagemagick (सर्वर पर)

जब से तुम सी # में हैं तो आप भी (जैसे वर्गों के साथ: System.Drawing.Bitmap और System.Drawing.Graphics) सीधे बिटमैप और फ़ॉन्ट हेरफेर के लिए नेट वर्गों इस्तेमाल कर सकते हैं

3

मैंने इस answer में उल्लिखित इस विधि का अनुवाद VB.NET विधि में किया है। शायद यह किसी की मदद करता है।

Public Function DrawText(ByVal text As String, ByRef font As Font, ByRef textColor As Color, ByRef backColor As Color) As Image 
    ' first, create a dummy bitmap just to get a graphics object 
    Dim img As Image = New Bitmap(1, 1) 
    Dim drawing As Graphics = Graphics.FromImage(img) 

    ' measure the string to see how big the image needs to be 
    Dim textSize As SizeF = drawing.MeasureString(Text, Font) 

    ' free up the dummy image and old graphics object 
    img.Dispose() 
    drawing.Dispose() 

    ' create a new image of the right size 
    img = New Bitmap(CType(textSize.Width, Integer), CType(textSize.Height, Integer)) 

    drawing = Graphics.FromImage(img) 

    ' paint the background 
    drawing.Clear(BackColor) 

    ' create a brush for the text 
    Dim textBrush As Brush = New SolidBrush(textColor) 

    drawing.DrawString(text, font, textBrush, 0, 0) 

    drawing.Save() 

    textBrush.Dispose() 
    drawing.Dispose() 

    Return img 

End Function 

संपादित: फिक्स्ड टाइपो।

+0

धन्यवाद फ्रेडी, आपने मुझे बहुत सारी ऊर्जा बचाई। – BedfordNYGuy

1

F# संस्करण:


open System.Drawing 

let drawText text font textColor backColor = 
    let size = 
     use dummyImg = new Bitmap(1, 1) 
     use drawing = Graphics.FromImage(dummyImg) 
     drawing.MeasureString(text, font) 
    let img = new Bitmap((int size.Width), (int size.Height)) 
    use drawing = Graphics.FromImage(img) 
    use textBrush = new SolidBrush(textColor) 
    do 
     drawing.Clear(backColor) 
     drawing.DrawString(text, font, textBrush, PointF()) 
     drawing.Save() |> ignore 
    img 
3

धन्यवाद Kazar। पिछले जवाब का एक मामूली सुधार छवि/ग्राफिक वस्तुओं के बाद इस्तेमाल किया की समाप्ती और मिनट आकार परम की शुरूआत

private Image DrawTextImage(String currencyCode, Font font, Color textColor, Color backColor) { 
     return DrawTextImage(currencyCode, font, textColor, backColor, Size.Empty); 
    } 
    private Image DrawTextImage(String currencyCode, Font font, Color textColor, Color backColor, Size minSize) { 
     //first, create a dummy bitmap just to get a graphics object 
     SizeF textSize; 
     using (Image img = new Bitmap(1, 1)) { 
      using (Graphics drawing = Graphics.FromImage(img)) { 
       //measure the string to see how big the image needs to be 
       textSize = drawing.MeasureString(currencyCode, font); 
       if (!minSize.IsEmpty) { 
        textSize.Width = textSize.Width > minSize.Width ? textSize.Width : minSize.Width; 
        textSize.Height = textSize.Height > minSize.Height ? textSize.Height : minSize.Height; 
       } 
      } 
     } 

     //create a new image of the right size 
     Image retImg = new Bitmap((int)textSize.Width, (int)textSize.Height); 
     using (var drawing = Graphics.FromImage(retImg)) { 
      //paint the background 
      drawing.Clear(backColor); 

      //create a brush for the text 
      using (Brush textBrush = new SolidBrush(textColor)) { 
       drawing.DrawString(currencyCode, font, textBrush, 0, 0); 
       drawing.Save(); 
      } 
     } 
     return retImg; 
    } 
संबंधित मुद्दे