Use TexturedBrush to draw string


   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class MainForm : Form {

    private Brush texturedTextBrush;
    private Brush texturedBGroundBrush;

    public MainForm() {
        Image bGroundBrushImage = new Bitmap("Clouds.bmp");
        texturedBGroundBrush = new TextureBrush(bGroundBrushImage);
        Image textBrushImage = new Bitmap("Soap Bubbles.bmp");
        texturedTextBrush = new TextureBrush(textBrushImage);
    }

    protected void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        Rectangle r = ClientRectangle;
        g.FillRectangle(texturedBGroundBrush, r);
        g.DrawString("Bitmaps as brushes",
                     new Font("Arial", 30,
                     FontStyle.Bold | FontStyle.Italic),
                     texturedTextBrush,
                     r);

    }
    public static void Main(){
       Application.Run(new MainForm());
    }
}

    


Hotkey Prefix

   
 


using System;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
   
class UnderlinedText: Form
{
     public static void Main()
     {
          Application.Run(new UnderlinedText());
     }
     public UnderlinedText()
     {
          Text = "Underlined Text Using HotkeyPrefix";
          Font = new Font("Times New Roman", 14);
          ResizeRedraw = true; 
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }     
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          string       str    = "This is some &u&n&d&e&r&l&i&n&e&d text!";
          StringFormat strfmt = new StringFormat();
   
          strfmt.HotkeyPrefix = HotkeyPrefix.Show;
   
          grfx.DrawString(str, Font, new SolidBrush(clr), 0, 0, strfmt);
     }
}

    


Draw Rectangle surrounding the Text

   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form {

    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    g.PageUnit = GraphicsUnit.Inch;

    Pen p = new Pen(Color.Black, 1 / 96f);
    Font f = new Font("Times New Roman", 16);
    String s = "Abc";
    SizeF sf = g.MeasureString(s, f);

    g.DrawRectangle(p, 1, 1, sf.Width, sf.Height);
    g.DrawString(s, f, Brushes.Black, 1, 1);

    f.Dispose();
    p.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

    


Rotate the text 45 degrees clockwise and translate the text 150 pixels horizontally

   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form {

    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    g.FillRectangle(Brushes.White, this.ClientRectangle);
    Font f = new Font("Times New Roman", 24);
    g.DrawString("Rotate then Translate", f, Brushes.Black, 0, 0);
    g.RotateTransform(45);
    g.TranslateTransform(150, 0);
    g.DrawString("Rotate  then Translate ", f, Brushes.Black, 0, 0);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

    


With a 200px-width rectangle, and a 12pt font, it requires six lines to display the string in its entirety

   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form {

    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    g.FillRectangle(Brushes.White, this.ClientRectangle);

    String s = "This string is long enough to wrap. ";
    Font f = new Font("Arial", 12);
    Rectangle r = new Rectangle(20, 20, 200, f.Height * 6);

    g.DrawRectangle(Pens.Black, r);
    g.DrawString(s, f, Brushes.Black, r);

    f.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

    


use a 12pt font, and assume the text string must fit into a width of 150 pixels

   
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form {

    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    g.FillRectangle(Brushes.White, this.ClientRectangle);

    String s = "This string is long enough to wrap. ";

    Font f = new Font("Arial", 12);
    SizeF sf = g.MeasureString(s, f, 150);
    RectangleF rf = new RectangleF(20, 20, sf.Width, sf.Height);

    g.DrawRectangle(Pens.Black, rf.Left, rf.Top, rf.Width, rf.Height);
    g.DrawString(s, f, Brushes.Black, rf); 

    f.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

    


Draw the rectangle and draw the string, using the rectangle as a bounding box

   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form {

    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    g.FillRectangle(Brushes.White, this.ClientRectangle);

    String s = "This string is long enough to wrap. ";
    Font f = new Font("Arial", 12);
    Rectangle r = new Rectangle(20, 20, 150, f.Height * 4);

    StringFormat sf = new StringFormat();
    sf.FormatFlags = StringFormatFlags.NoWrap;

    g.DrawRectangle(Pens.Black, r);
    g.DrawString(s, f, Brushes.Black, r, sf);
    f.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}