Export (0) Print
Expand All

Drawing Text with GDI+

Visual Studio .NET 2003

You can use any graphics object as a surface to render text. Rendering text requires a Brush object, which indicates what pattern will fill the text, and a Font object, which describes the pattern to be filled. The font can be any named font that is installed on the system, and the brush can be any kind of brush. Thus, it is possible to paint text using a solid color, a pattern, or even an image. For more information on brushes, see Pens, Brushes, and Colors.

To render a string with GDI+

  1. Obtain a reference to the graphics object you will use for drawing. For more information, see Creating Graphical Images with GDI+.
    ' Visual Basic
    ' Obtains a reference to the graphics object for Button1
    Dim g as Graphics = Button1.CreateGraphics
    
    // C#
    // Obtains a reference to the Graphics object for Button1
    Graphics g = Button1.CreateGraphics();
    
    // C++
    // Obtains a reference to the Graphics object for button1
    Graphics* g = button1->CreateGraphics();
    
  2. Create an instance of the Brush you want to use to paint the text. For example:
    ' Visual Basic
    Dim mybrush As New Drawing2D.LinearGradientBrush(ClientRectangle, _
       Color.Red, Color.Yellow, Drawing2D.LinearGradientMode.Horizontal)
    
    // C#
    System.Drawing.Drawing2D.LinearGradientBrush myBrush = new 
       System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle,
       Color.Red, Color.Yellow, System.Drawing.Drawing2D.
       LinearGradientMode.Horizontal);
    
    // C++
    System::Drawing::Drawing2D::LinearGradientBrush* myBrush = new 
       System::Drawing::Drawing2D::LinearGradientBrush(ClientRectangle,
       Color::Red, Color::Yellow, System::Drawing::Drawing2D::
       LinearGradientMode::Horizontal);
    
  3. Create the font you want your text to be displayed in. For example:
    ' Visual Basic
    Dim myFont as New Font("Times New Roman", 24)
    
    // C#
    Font myFont = new Font("Times New Roman", 24);
    
    // C++
    System::Drawing::Font* myFont = 
       new System::Drawing::Font(S"Times New Roman", 24);
    
  4. Call the Graphics.DrawString Method method of the Graphics object to render your text.
    • If you supply a RectangleF object, the text will wrap within the rectangle.
    • Otherwise, it will begin at the starting coordinates you provide.
      ' Visual Basic
      g.DrawString("Look at this text!", myFont, myBrush, New _
         RectangleF(10, 10, 100, 200))
      g.DrawString("Look at this text!", myFont, myBrush, 10, 10)
      
      // C#
      g.DrawString("Look at this text!", myFont, myBrush, new 
         RectangleF(10, 10, 100, 200));
      g.DrawString("Look at this text!", myFont, myBrush, 10, 10);
      
      // C++
      g->DrawString(S"Look at this text!", myFont, myBrush, 
         RectangleF(10, 10, 100, 200));
      g->DrawString(S"Look at this text!", myFont, myBrush, 10, 10);
      

See Also

GDI+ Example Topics | Creating Graphical Images with GDI+ | Pens, Brushes, and Colors | Drawing Lines and Shapes with GDI+ | Rendering Images With GDI+ | GDI+ Graphics | Font Class

Show:
© 2014 Microsoft