2010-12-12 8 views
10

मैं एक विशिष्ट कोण में एक दिए गए स्ट्रिंग को प्रदर्शित करना चाहता हूं। मैंने System.Drawing.Font कक्षा के साथ ऐसा करने की कोशिश की।जीडीआई + में पाठ को घुमाने के लिए कैसे?

Font boldFont = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold, GraphicsUnit.Pixel, 1, true);
graphics.DrawString("test", boldFont, textBrush, 0, 0);

किसी को भी मेरी मदद कर सकते: यहाँ मेरी कोड है?

उत्तर

16
String theString = "45 Degree Rotated Text"; 
SizeF sz = e.Graphics.VisibleClipBounds.Size; 
//Offset the coordinate system so that point (0, 0) is at the 
center of the desired area. 
e.Graphics.TranslateTransform(sz.Width/2, sz.Height/2); 
//Rotate the Graphics object. 
e.Graphics.RotateTransform(45); 
sz = e.Graphics.MeasureString(theString, this.Font); 
//Offset the Drawstring method so that the center of the string matches the center. 
e.Graphics.DrawString(theString, this.Font, Brushes.Black, -(sz.Width/2), -(sz.Height/2)); 
//Reset the graphics object Transformations. 
e.Graphics.ResetTransform(); 

here से लिया: कि सभी परिवर्तनों है कि आप आवेदन किया है जो नहीं हो सकता है कि आप क्या चाहते) साफ हो जाएगा।

10

आप सभी पर Graphics ड्राइंग (सहित पाठ DrawString का उपयोग कर तैयार की गई) के लिए रोटेशन निर्दिष्ट करने के लिए RotateTransform विधि (see MSDN) का उपयोग कर सकते हैं। angle डिग्री में है:

graphics.RotateTransform(angle) 

आप बस एक ही घुमाया आपरेशन क्या करना चाहते हैं, तो आप मूल अवस्था में परिणत नकारात्मक कोण के साथ फिर से RotateTransform बुला (वैकल्पिक रूप से, आप ResetTransform उपयोग कर सकते हैं लेकिन रीसेट कर सकते हैं,

graphics.RotateTransform(-angle) 
+0

मैं पहले से ही इस कोशिश की, लेकिन तो मेरे सभी तैयार ग्राफिक्स घूर्णन कर रहे हैं। Thats बहुत ज्यादा मदद नहीं करते हैं। – eagle999

+2

@ eagle999 रीसेट ट्रान्सफॉर्म() का उपयोग करने के बाद घुमाए गए पाठ को चित्रित करने के बाद –

7

आप एक विधि तार केंद्र के स्थान पर एक घुमाया स्ट्रिंग आकर्षित करने के लिए चाहते हैं, तो निम्नलिखित विधि का प्रयास:

public void drawRotatedText(Bitmap bmp, int x, int y, float angle, string text, Font font, Brush brush) 
{ 
    Graphics g = Graphics.FromImage(bmp); 
    g.TranslateTransform(x, y); // Set rotation point 
    g.RotateTransform(angle); // Rotate text 
    g.TranslateTransform(-x, -y); // Reset translate transform 
    SizeF size = g.MeasureString(text, font); // Get size of rotated text (bounding box) 
    g.DrawString(text, font, brush, new PointF(x - size.Width/2.0f, y - size.Height/2.0f)); // Draw string centered in x, y 
    g.ResetTransform(); // Only needed if you reuse the Graphics object for multiple calls to DrawString 
    g.Dispose(); 
} 

सादर हंस मिलिंग ...

+0

आप नायक हैं !! धन्यवाद। –

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