Skip to content

Instantly share code, notes, and snippets.

@SajjadArifGul
Last active October 8, 2015 18:07
Show Gist options
  • Save SajjadArifGul/1d0a4c6915a2c574f972 to your computer and use it in GitHub Desktop.
Save SajjadArifGul/1d0a4c6915a2c574f972 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
/* We added following Assemblies in our Project. */
using System.Drawing.Drawing2D;
using System.Drawing.Text;
namespace howto_rotated_text
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
We are now using Font class here. & You can change the Font here as you like. I am using Comic Sans MS font here.
using (Font the_font = new Font("Comic Sans MS", 20))
{
const int dx = 50;
int x = 40, y = 50;
DrawRotatedTextAt(e.Graphics, 0, "Google", x, y, the_font, Brushes.Green);
x += dx;
DrawRotatedTextAt(e.Graphics, 10, "Youtube", x, y, the_font, Brushes.Blue);
x += dx;
DrawRotatedTextAt(e.Graphics, 30, "Yahoo", x, y, the_font, Brushes.CornflowerBlue);
x += dx;
DrawRotatedTextAt(e.Graphics, 40, "Microsoft", x, y, the_font, Brushes.DarkGoldenrod);
x += dx;
DrawRotatedTextAt(e.Graphics, 50, "Bing", x, y, the_font, Brushes.Aquamarine);
x += dx;
DrawRotatedTextAt(e.Graphics, 60, "Csharpens", x, y, the_font, Brushes.DarkMagenta);
x += dx;
DrawRotatedTextAt(e.Graphics, 70, "WhatsApp", x, y, the_font, Brushes.Black);
x += dx;
DrawRotatedTextAt(e.Graphics, 80, "Pakistanii", x, y, the_font, Brushes.BlueViolet);
x += dx;
DrawRotatedTextAt(e.Graphics, 90, "Gmail", x, y, the_font, Brushes.Brown);
x += dx;
DrawRotatedTextAt(e.Graphics, 100, "Outlook", x, y, the_font, Brushes.BurlyWood);
x += dx;
DrawRotatedTextAt(e.Graphics, 110, "Facebook", x, y, the_font, Brushes.Chartreuse);
x += dx;
DrawRotatedTextAt(e.Graphics, 120, "Twitter", x, y, the_font, Brushes.Coral);
}
}
// Draw a rotated string at a particular position.
private void DrawRotatedTextAt(Graphics gr, float angle, string txt, int x, int y, Font the_font, Brush the_brush)
{
// Save the graphics state.
GraphicsState state = gr.Save();
gr.ResetTransform();
// Rotate.
gr.RotateTransform(angle);
// Translate to desired position. Be sure to append
// the rotation so it occurs after the rotation.
gr.TranslateTransform(x, y, MatrixOrder.Append);
// Draw the text at the origin.
gr.DrawString(txt, the_font, the_brush, 0, 0);
// Restore the graphics state.
gr.Restore(state);
}
private void Form1_Load(object sender, EventArgs e)
{
//doing nothing here. :p
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment