Click here to Skip to main content
15,887,683 members
Articles / Programming Languages / C#
Tip/Trick

Rotated Text

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
8 Apr 2015CPOL2 min read 37.5K   1.1K   14   10
Understanding the nuances of TranslateTransform and RotateTransform

Introduction

This tip will attempt to assist you in understanding and using the PictureBox TranslateTransform and RotateTransform functions in C#. Imagine that this also applies to other .NET languages. I also imagine that it applies to other containers with graphic interfaces, but I've not tried.

Background

I've read a few articles on this, and I'd read that it's better to perform the Translate first and the rotate second, but nothing which really helped me to understand what is going on. In the end, I wrote a simple program with just this functionality so that I could see (and hopefully understand) what it all means.

My purpose for performing the text rotation was to elegantly label the Y axis on a graph. Initially, I tried doing this and nothing came out - I realise now that it was just because the text was being rendered outside the bounds of the PictureBox.

TranslateTransform

This function simply repositions the origin (where is 0,0 ?) in the graphics object for any subsequent rendering. You could take the view that this is unnecessary because there's no problem with using a position with negative coordinates, but more on this below...

RotateTransform

This function causes any subsequent rendering to be rotated about the origin.

Main Bit of Code

I'm not a fan of over-commenting, but here goes...

C++
// Our picturebox is an empty container, so we must create the bitmap to go in it.
Bitmap bmp1 = new Bitmap(pictureBox1.Width, pictureBox1.Height);

//Now make a GDI+ graphics connection to the bitmap
Graphics gr1 = Graphics.FromImage(bmp1);

//We want our text to look good, so we'll tell GDI+ to apply AntiAliasing to anything
//we add
gr1.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;

//Now we're ready to perform our first function - TranslateTransform
//We're not actually moving anything graphical here, we're just telling GDI+
//that we want to move the origin of the graphics object for subsequent rendering
gr1.TranslateTransform(trX, trY);

//Next comes the RotateTransform
//Again we're not actually moving anything graphical, we're just telling GDI+
//that anything we draw from now on is to be rotated.
gr1.RotateTransform(R);

//Now add the text.
gr1.DrawString("Some text", drawFont, Brushes.Black, DSX, DSY);

//Finally, put the bitmap (which has been updated via GDI+) into the picturebox.
pictureBox1.Image = bmp1;

In my app, I've also included the StringFormat argument to the DrawString call because it has a useful and associated function. The app front-end looks like this:

Now, in my particular case, I want the text rotate counter-clockwise by 90 degrees to be centred in the tall/thin box vertically and be on the leftmost extreme, so let's try rotating the text. If we just put a rotation in, then it disappears, but with a "Translate" it works:

It's not in the centre yet though, so I'll add 150 to the X position and tell StringFormat to Centre align it there.

So that works. Interestingly though, I could instead take the view that I'm repositioning my origin at the halfway point and then positioning my text at 0,0 like this:

Um, ok. That's exactly the same. So let's try NOT moving my origin, and using a NEGATIVE X position:

Sure enough, that works too.

I think each set is equally as valid, but each for different reasons:

The first says "I want to refer to my coordinates as if the box were horizontal instead of vertical."
The second says "I want to rotate my text around a certain point"
The third says "I appreciate that my origin is now unusual, but I'm OK with that."

C# Project

The project can be found here.

History

  • V1 20150408 164000
  • V2 Tidied-up and released by Deeksha Shenoy
  • V3 Image links fixed by author (probably incorrectly changed "Article" back to "cs")

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Noble Software Systems Ltd
United Kingdom United Kingdom
I've been programming in various languages since 1980. I started in Basic at college and moved swiftly to Z80 assembler. After that I was an operator on a Burroughs mini for a time, then after a little Burroughs COBOL I moved to UNIVAC COBOL for many years. (During which time I became an IT contractor). During that time I touched UNIVAC assembler a bit, as well as the usual SSG etc. I did a bit of RPG, though I never really liked that. After COBOL I went to Gupta SQLWindows for a few years (which I still maintain was a good OO language). Then a move to Oracle PL/SQL and a few years as senior programmer for a large American company, also some years in the DBA team at then end of my time there. Somewhere in the middle of that I spent a year working in PIC assembler and C on a Palm PDA. After Oracle I moved to the other end of things in an IT sense, and discovered FPGAs and VHDL programming. Oh my, that's a brain-bender coming from computer programming. Anyhow, I am also interfacing FPGAs to a PC, and that's where C# comes in.

Comments and Discussions

 
QuestionRotated Text Pin
Member 1276495828-Sep-16 6:54
Member 1276495828-Sep-16 6:54 
AnswerRe: Rotated Text Pin
CharlieFoolsTheComputer28-Sep-16 23:37
professionalCharlieFoolsTheComputer28-Sep-16 23:37 
GeneralRe: Rotated Text Pin
CharlieFoolsTheComputer28-Sep-16 23:41
professionalCharlieFoolsTheComputer28-Sep-16 23:41 
Questiontry Translate -> Rotate -> Translate Pin
sx20089-Apr-15 8:37
sx20089-Apr-15 8:37 
AnswerRe: try Translate -> Rotate -> Translate Pin
CharlieFoolsTheComputer10-Apr-15 2:15
professionalCharlieFoolsTheComputer10-Apr-15 2:15 
QuestionPictures links are dead Pin
tbayart9-Apr-15 4:08
professionaltbayart9-Apr-15 4:08 
AnswerRe: Pictures links are dead Pin
CharlieFoolsTheComputer10-Apr-15 2:03
professionalCharlieFoolsTheComputer10-Apr-15 2:03 
GeneralRe: Pictures links are dead Pin
tbayart10-Apr-15 5:13
professionaltbayart10-Apr-15 5:13 
QuestionPlease fix the images Pin
Shao Voon Wong8-Apr-15 20:10
mvaShao Voon Wong8-Apr-15 20:10 
AnswerRe: Please fix the images Pin
CharlieFoolsTheComputer10-Apr-15 2:03
professionalCharlieFoolsTheComputer10-Apr-15 2:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.