GDI+
Rotate text
The code to rotate a text in GDI+ is pretty simple:
private void Form1_Paint(object sender, PaintEventArgs e)
{
int x = this.ClientRectangle.Width / 2;
int y = this.ClientRectangle.Height / 2;
e.Graphics.TranslateTransform(x, y);
e.Graphics.RotateTransform(270);
e.Graphics.DrawString("Hello", new Font("Arial", 14), Brushes.Black, 0, 0);
e.Graphics.ResetTransform();
e.Graphics.DrawLine(Pens.Black, x, 0, x, this.Size.Height);
e.Graphics.DrawLine(Pens.Black, 0, y, this.Size.Width, y);
}
Here is a screenshot of what it’s gave:

Predefined brushes and pens
When using GDI+, to save the creation of standard pens and brushes, you can use the predefined in the classes Pens and Brushes.
For example, the black brush is Brushes.Black, and the white pen is Pens.White
Here is a example:
e.Graphics.DrawLine(Pens.Black, x, 0, x, this.Size.Height);
Transparent drawing
In GDI+, you can easilly draw transparent shapes.
One of the Color class constructor take a Alpha value. 0 is completly transparent and 255 is opaque.
//Fill the background
Brush brush = new HatchBrush(HatchStyle.DiagonalCross, Color.Black, Color.White);
e.Graphics.FillRectangle(brush, 10,10, this.ClientRectangle.Width-20, this.ClientRectangle.Height-20);
//Transparent
Color transparentBlue = Color.FromArgb(0, Color.LightBlue);
e.Graphics.FillRectangle(new SolidBrush(transparentBlue), 20, 20, 50, 50);
//Opaque
e.Graphics.FillRectangle(Brushes.LightBlue, 120, 120, 50, 50);
Here’s the screenshot of the example:
Render multiple lines of text
The Graphics.DrawString method process the \r\n to produce multiples lines.
string text = "Measure string hello world!\r\nA second line."; e.Graphics.DrawString(text, this.Font, Brushes.Black,10,10);
Here’s the result:

Measuring the text to draw
You can easilly measure a text to know the size of drawing.
string text = "Measure string hello world!"; SizeF size = e.Graphics.MeasureString(text, this.Font); e.Graphics.DrawRectangle(Pens.Green, 10,10, size.Width, size.Height); e.Graphics.DrawString(text, this.Font, Brushes.Black,10,10);
Here’s the result:

Multiple lines
The measure works with multiple lines of text if the text is delimited with \r\n.
string text = "Measure string hello world!\r\nA second line."; SizeF size = e.Graphics.MeasureString(text, this.Font); e.Graphics.DrawRectangle(Pens.Green, 10,10, size.Width, size.Height); e.Graphics.DrawString(text, this.Font, Brushes.Black,10,10);
Here’s the result:
