GDI+Font Font

 Fonts are not unfamiliar to most people. Fonts are essential in text editing software (such as Word). Similarly, in GDI+, fonts are also required to draw strings. Before introducing the use of the font Font class, first introduce some related classes or enumerations:

 

      (1) Font family FontFamily:

            GDI+ will have the same style as a font family, such as our common "Song style", "Imitation Song", "Microsoft Yahei", "Arial" and so on.

 

     (2)  Font style enumeration FontSytle:

            The FontStyle enumeration lists common font styles, which are defined as follows:

[csharp]  view plain copy  
  1. enum FontStyle  
  2.   
  3. {  
  4.     Regular,         //regular  
  5.      Bold,            // bold  
  6.      Italic,          //tilt  
  7.      Underline,       //underscore  
  8.      Strikout         // Emphasis line  
  9. }  

    

      (3) The font size unit GraphicsUnit:

            Font description in GDI+, when outputting text, it is inevitable to use the unit to describe the width or height (such as pixels, points). The GraphicsUnit enumeration lists the units commonly used in GDI+, which are defined as follows:

[csharp]  view plain copy  
  1. enum GraphicsUnit  
  2.   
  3. {  
  4.      Display,        //Specify the unit of measure of the display device. Typically, video displays use pixels; printers use 1/100 of an inch.   
  5.       Document,       //Specify the document unit (1/300 of an inch) as the unit of measure.  
  6.       Inch,           //Specify inches as the unit of measure.   
  7.       Millimeter,     // Specify millimeters as the unit of measure.  
  8.       Pixel,          // Specifies the device pixel as the unit of measurement.   
  9.       Point,          //Specifies the printer point (1/72 inch) as the unit of measure.  
  10.       World           //Specifies the world coordinate system unit as the unit of measure.  
  11. }  


     Build the font Font object:

             Let's take a look at a constructor of the Font class. Combining the above information, you should know that this constructor should be written as follows:

[csharp]  view plain copy  
  1. public Font (FontFamily family,float emSize,FontStyle style,GraphicsUnit unit)  

 

The following Demo demonstrates the basic use of font:

[csharp]  view plain copy  
  1. privatevoid Form1_Paint(object sender, PaintEventArgs e)   
  2.        {  
  3.            Graphics g = e.Graphics;  
  4.            g.Clear(Color.White);  
  5.   
  6.            //anti-alias  
  7.            g.SmoothingMode = SmoothingMode.AntiAlias;  
  8.   
  9.            FontFamily fontFamily = new FontFamily("宋体");  
  10.            Font myFont1 = new Font(fontFamily,15,FontStyle.Regular,GraphicsUnit.Pixel);  
  11.            g.DrawString( "The font family is Arial, the size is 15, and the font style is regular black font" ,myFont1,Brushes.Black, new  PointF(10,50));  
  12.   
  13.            Font myFont2 = new Font("Arial",25.5f,FontStyle.Italic,GraphicsUnit.Pixel);  
  14.            g.DrawString( "The font family is Arial, the size is 25.5, and the font style is italic red font" , myFont2, Brushes.Red,  new  PointF(10, 100));  
  15.   
  16.            Font myFont3 =  new  Font( "Microsoft Yahei" , 20, FontStyle.Underline, GraphicsUnit.Pixel);  
  17.            TextRenderer.DrawText(g, "Use the TextRenderer class to draw strings" ,myFont3, new  Point(10,150),Color.Green);  
  18.   
  19.   
  20.            // release resources  
  21.            fontFamily.Dispose();  
  22.            myFont1.Dispose();  
  23.            myFont2.Dispose();  
  24.            myFont3.Dispose();  
  25.   
  26.        }  



 Effect picture:

 

 

 

Note: If there are a lot of text output operations in the program, it is strongly recommended to use the DrawText method of TextRenderer instead of the DrawString method, because DrawText is much more efficient than DrawStrng in terms of text output. Output text with antialiasing if necessary.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325967023&siteId=291194637