Skip to content

Instantly share code, notes, and snippets.

@AlleSchonWeg
Last active May 30, 2017 13:30
Show Gist options
  • Save AlleSchonWeg/36ffe0bb7b5678d707d36782e2addc15 to your computer and use it in GitHub Desktop.
Save AlleSchonWeg/36ffe0bb7b5678d707d36782e2addc15 to your computer and use it in GitHub Desktop.
Text Transformation
private void DrawText(Graphics graphics)
{
using(Font font = new Font("Arial", 20, FontStyle.Regular, GraphicsUnit.Pixel))
{
// draw string as text without transformation
using(Brush brush = new SolidBrush(Color.Blue))
graphics.DrawString("Draw as text without transformation", font, brush, new PointF(10,10));
// draw string as text with transformation
GraphicsContainer container = graphics.BeginContainer();
graphics.TranslateTransform(10,40);
graphics.RotateTransform(15);
using(Brush brush = new SolidBrush(Color.Green))
graphics.DrawString("Draw as text with transformation", font, brush, PointF.Empty);
graphics.EndContainer(container);
// draw string as path
using (GraphicsPath path = new GraphicsPath())
using (Brush brush = new SolidBrush(Color.Red))
{
path.AddString("Draw as path without transformation", font.FontFamily, (int) font.Style,
20, new PointF(10, 70), StringFormat.GenericTypographic);
graphics.FillPath(brush, path);
}
// draw string as path with transformation
container = graphics.BeginContainer();
graphics.TranslateTransform(10, 100);
graphics.RotateTransform(15);
using (GraphicsPath path = new GraphicsPath())
using (Brush brush = new SolidBrush(Color.Fuchsia))
{
path.AddString("Draw as path with transformation", font.FontFamily, (int) font.Style,
20, PointF.Empty, StringFormat.GenericTypographic);
graphics.FillPath(brush, path);
}
graphics.EndContainer(container);
}
}
@AlleSchonWeg
Copy link
Author

Sample image:
sample

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment