2011-07-19 14 views
5

मैं ग्राफिक्स ड्रास्ट्रिंग विधि का उपयोग कर एक छवि पर टेक्स्ट लिख रहा हूं, एक आयत के साथ अपने पाठ को बाध्यकारी। यहाँ मेरी कोड है:सी # ग्राफिक्स। ड्रॉस्ट्रिंग आयताकार ऑटो-ऊंचाई: उस ऊंचाई को कैसे ढूंढें?

//Write header2 
RectangleF header2Rect = new RectangleF(); 
header2Rect.Width = 600; 
header2Rect.Location = new Point(30, 105); 
graphicImage.DrawString(header2, new Font("Gotham Medium", 28, FontStyle.Bold),brush, header2Rect); 
//Write Description 
RectangleF descrRect = new RectangleF(); 
descrRect.Width = 600; 
int measurement = ((int)graphicImage.MeasureString(header2, new Font("Gotham Medium", 28, FontStyle.Bold)).Height); 
var yindex = int.Parse(105 + header2Rect.Height.ToString()); 
descrRect.Location = new Point(30, 105+measurement); 
graphicImage.DrawString(description.ToLower(), new Font("Gotham", 24, FontStyle.Italic), SystemBrushes.WindowText, descrRect); 

यह कुछ मामलों के लिए काम करता है (यानी जब header2 केवल 1 लाइन लंबी है।), लेकिन मेरे measurement चर केवल फ़ॉन्ट की ऊंचाई, न कि पूरी DrawString आयत को मापता है। मैं एक स्थिर header2Rect ऊंचाई सेट नहीं करना चाहता क्योंकि ऊंचाई उस पाठ के आधार पर बदलती है।

yindex काम नहीं करता है क्योंकि header2Rect.Height = 0। क्या यह देखने का कोई तरीका है कि मेरे header2 कितनी लाइनें हैं?

क्या मुझे बस MeasureString चौड़ाई करने की आवश्यकता है और मेरी सीमा आयताकार चौड़ाई से विभाजित करें, फिर MeasureString ऊंचाई से गुणा करें? मुझे लगता है कि एक बेहतर तरीका है।

धन्यवाद

[संपादित करें] ऐसा लगता है ऊंचाई वास्तव में 0 है, लेकिन पाठ के ठीक बाहर overflows, लेकिन चौड़ाई अभी भी पाठ-रैपिंग constricts। मैंने ऊंचाई खोजने के लिए कुछ गणित की गणना की है, लेकिन मेरी इच्छा है कि एक बेहतर तरीका था।

उत्तर

6

आपको अपना आयत ऊंचाइयों सेट कर रहे हैं:

private void panel1_Paint(object sender, PaintEventArgs e) 
{ 
    string header2 = "This is a much, much longer Header"; 
    string description = "This is a description of the header."; 

    RectangleF header2Rect = new RectangleF(); 
    using (Font useFont = new Font("Gotham Medium", 28, FontStyle.Bold)) 
    { 
    header2Rect.Location = new Point(30, 105); 
    header2Rect.Size = new Size(600, ((int)e.Graphics.MeasureString(header2, useFont, 600, StringFormat.GenericTypographic).Height)); 
    e.Graphics.DrawString(header2, useFont, Brushes.Black, header2Rect); 
    } 

    RectangleF descrRect = new RectangleF(); 
    using (Font useFont = new Font("Gotham Medium", 28, FontStyle.Italic)) 
    { 
    descrRect.Location = new Point(30, (int)header2Rect.Bottom); 
    descrRect.Size = new Size(600, ((int)e.Graphics.MeasureString(description, useFont, 600, StringFormat.GenericTypographic).Height)); 
    e.Graphics.DrawString(description.ToLower(), useFont, SystemBrushes.WindowText, descrRect); 
    } 

} 
+0

आप अद्भुत हैं! यह ठीक वही है जिसकी तलाश मुझे थी! धन्यवाद। –

+0

मेरी सी ++ प्रोजेक्ट में, केवल 'स्ट्रिंगफॉर्मैट :: जेनेरिकडिफॉल्ट() 'स्ट्रिंगफॉर्मैट :: जेनेरिक टाइपोग्राफिक()' के बजाय सही तरीके से काम करता है। मुझे यकीन नहीं है कि क्या कारण है। –

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