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:

RotatedText

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:
Transparent

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:
MultipleLines

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:
MeasureString

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:
MeasureString2lines

  1. February 20, 2013 at 9:39 am

    I personally Think post, “GDI+ Jean-Francois Blier” was great!

    I actuallycould not agree with u even more! At last seems like Istumbled upon a blogging
    site really worth checking out. Thanks a lot, Tobias

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: