2010-11-11 19 views
12

मैं ग्राफिक्स ऑब्जेक्ट पर एक स्ट्रिंग खींचने के लिए जीडीआई + का उपयोग कर रहा हूं।आयत के अंदर पाठ भरें

मैं स्ट्रिंग (किसी भी लाइनों को तोड़ने के बिना) एक पूर्व निर्धारित आयत के अंदर फिट करने के लिए

वहाँ के अलावा यह कर एक पाश में TextRenderer.MeasureString() का उपयोग कर जब तक वांछनीय आकार दिया जाता है की वैसे भी है करना चाहते हैं?

कुछ की तरह:

DrawScaledString(Graphics g, string myString, Rectangle rect) 
+0

आप एक मैट्रिक्स ट्रांसफॉर्म लागू कर सकते हैं, लेकिन पिछले कुछ वर्षों से मैंने इसे छुआ है। – leppie

+0

क्या यह ओवरहेड का थोड़ा सा नहीं है? – Nissim

उत्तर

8

आप ScaleTransform

string testString = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Suspendisse et nisl adipiscing nisl adipiscing ultricies in ac lacus. 
Vivamus malesuada eros at est egestas varius tincidunt libero porttitor. 
Pellentesque sollicitudin egestas augue, ac commodo felis ultricies sit amet."; 

Bitmap bmp = new Bitmap(300, 300); 
using (var graphics = Graphics.FromImage(bmp)) 
{ 
    graphics.FillRectangle(Brushes.White, graphics.ClipBounds); 
    var stringSize = graphics.MeasureString(testString, this.Font); 
    var scale = bmp.Width/stringSize.Width; 
    if (scale < 1) 
    { 
     graphics.ScaleTransform(scale, scale); 
    } 
    graphics.DrawString(testString, this.Font, Brushes.Black, new PointF()); 
} 
bmp.Save("lorem.png", System.Drawing.Imaging.ImageFormat.Png); 

उपयोग कर सकते हैं लेकिन आप कुछ उर्फ ​​प्रभाव मिल सकता है।

alt text

संपादित करें:

लेकिन मुझे लगता है कि आप कोड में बजाय ऊपर पैमाने को बदलने का उपयोग करने का scale साथ फ़ॉन्ट आकार को बदल सकते हैं आप के बजाय फ़ॉन्ट आकार बदलना चाहते हैं। दोनों की कोशिश करें और परिणाम की गुणवत्ता की तुलना करें।

+0

धन्यवाद आदमी ... मैंने स्केल के अनुसार फ़ॉन्ट आकार बदल दिया, थोड़ा समायोजन किया और यह बहुत अच्छा काम किया ... – Nissim

5

यहाँ समस्या का एक समाधान है, यह एक छोटे से गहन है के रूप में यह फॉन्ट निर्माण और विनाश का एक उचित बिट की आवश्यकता है, लेकिन अपने परिस्थितियों के आधार पर बेहतर कार्य कर सकता है, और की जरूरत है:

public class RenderInBox 
{ 
    Rectangle box; 
    Form root; 
    Font font; 
    string text; 

    StringFormat format; 

    public RenderInBox(Rectangle box, Form root, string text, string fontFamily, int startFontSize = 150) 
    { 
     this.root = root; 
     this.box = box; 
     this.text = text; 

     Graphics graphics = root.CreateGraphics(); 

     bool fits = false; 
     int size = startFontSize; 
     do 
     { 
      if (font != null) 
       font.Dispose(); 

      font = new Font(fontFamily, size, FontStyle.Regular, GraphicsUnit.Pixel); 

      SizeF stringSize = graphics.MeasureString(text, font, box.Width, format); 

      fits = (stringSize.Height < box.Height); 
      size -= 2; 
     } while (!fits); 

     graphics.Dispose(); 

     format = new StringFormat() 
     { 
      Alignment = StringAlignment.Center, 
      LineAlignment = StringAlignment.Center 
     }; 

    } 

    public void Render(Graphics graphics, Brush brush) 
    { 
     graphics.DrawString(text, font, brush, box, format); 
    } 
} 

यह बस का उपयोग करने के एक नई कक्षा बनाएं और रेंडर कॉल करें()। ध्यान दें कि यह विशेष रूप से किसी फ़ॉर्म को प्रस्तुत करने के लिए लिखा गया है।

var titleBox = new RenderInBox(new Rectangle(10, 10, 400, 100), thisForm, "This is my text it may be quite long", "Tahoma", 200); 
titleBox.Render(myGraphics, Brushes.White); 

आप RenderInBox वस्तु अग्रिम की वजह से यह गहन निर्माण स्वभाव है बनाना चाहिए। इसलिए यह हर जरूरत के लिए उपयुक्त नहीं है।

+0

उत्कृष्ट, बिल्कुल मुझे क्या चाहिए! – Gromer

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