1 Replies - 8492 Views - Last Post: 28 July 2008 - 07:50 AM Rate Topic: -----

#1 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Rotate text

Posted 26 July 2008 - 05:15 PM

On the site I'm currently working on I have a custom HttpHandler taking care of thumbnailing and displaying images. If you look here you will see a very faint watermark on the image when it is displayed (thanks Martyr2 for helping me finish that part) and I was wondering if anyone knew of a way I can rotate the text diagonally?

I know, I know, I havent showed any code, but you can see the code I wrote for this on my blog, sorry, don't feel like re-posting the code.

Is This A Good Question/Topic? 0
  • +

#3 orcasquall   User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 158
  • Joined: 14-September 07

Re: Rotate text

Posted 28 July 2008 - 07:50 AM

Here's a code sample that rotates text:
Bitmap bm = new Bitmap(200, 200);
Graphics g = Graphics.FromImage(bm);
g.FillRectangle(Brushes.White, 0, 0, 200, 200);

g.TranslateTransform(100, 100);
g.DrawString("Hello World!", new Font("Verdana", 12), Brushes.Black, 0, 0);


It creates a bitmap of 200 pixels in width, 200 pixels in height, and fills it with white colour.

Then we use matrix transformations. Matrix operations are, when read procedurally, done in reverse order.

So we draw the string "Hello World!" using the Verdana font at 12 points, at the origin (0,0). The origin is the top left corner (raster coordinates).

Then we rotate the text 30 degrees anti-clockwise. Positive values are for the clockwise direction. (Left-hand rule?).

Then we translate (or move) the result to the centre of the bitmap, 100 pixels right, 100 pixels down.

If you do a bit of 3D (or even 2D) geometry, possibly in OpenGL or DirectX, then the matrix operations should be familiar.

Note: It's easier to rotate about the origin first, then translate. Translation then rotation gives different results.

I hope that helps.
Was This Post Helpful? 1

Page 1 of 1