Trying to write text at an angle with RotateTransform

  • Thread starter Thread starter C
  • Start date Start date
C

C

This thing does not rotate the text. I have tried a dozen things now,
but can't get this to rotate the text.

Public Class Form1
Dim frmImage As Bitmap = New Bitmap(1000, 800)
Dim longBmp As Bitmap = New Bitmap(250, 250)

Sub WriteAngledText(ByVal gr As Graphics, ByVal text As String,
ByVal angle As Single)
gr.RotateTransform(angle, Drawing2D.MatrixOrder.Append) ' what
is this append for?
gr.DrawString(text, Label3.Font, Brushes.BlueViolet, 200, 200)
End Sub

Private Sub Label3_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Label3.Click

Dim g As Graphics = Graphics.FromImage(frmImage)
Dim glong As Graphics = Graphics.FromImage(longBmp)

Call WriteAngledText(glong, "Temperatures", 45)
' glong.DrawString("Temperature", Label1.Font,
Brushes.Blue, 10, 2)
longBmp.MakeTransparent(longBmp.GetPixel(1, 1))
' glong.RotateTransform(45,
Drawing2D.MatrixOrder.Append)

Me.Invalidate()

End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawImage(frmImage, 0, 0)
e.Graphics.DrawImage(longBmp, 80, 80)
End Sub

This does not show anything at all. Earlier with glong.DrawString I
could see the text "Temperature".
 
Am 21.09.2010 11:24, schrieb C:
This thing does not rotate the text. I have tried a dozen things now,
but can't get this to rotate the text.

Public Class Form1
Dim frmImage As Bitmap = New Bitmap(1000, 800)
Dim longBmp As Bitmap = New Bitmap(250, 250)

Sub WriteAngledText(ByVal gr As Graphics, ByVal text As String,
ByVal angle As Single)
gr.RotateTransform(angle, Drawing2D.MatrixOrder.Append) ' what
is this append for?

The rotation matrix is an additional matrix.
The final matrix is the product of the current transformation matrix
multiplied by the rotation matrix. With matrix multiplications,
in opposite to usual multiplications, the order does matter. For example,
if you first rotate, then translate an object, the result is different
from what you get if you first translate, then rotate it. Therefore,
you can specify if the rotation has to be applied before or after the
current tranformation. That's why you can append/prepand the new
transformation.

To your problem: It rotates around the origin, so it's probably outside
the Form. If you want to rotate _at_ 200|200, you must first rotate,
then translate:

gr.TranslateTransform(200, 200)
gr.RotateTransform(angle) 'Default is prepend = rotate first
gr.DrawString(text, Label3.Font, Brushes.BlueViolet, 0, 0)
gr.DrawString(text, Label3.Font, Brushes.BlueViolet, 200, 200)
End Sub


This does not show anything at all. Earlier with glong.DrawString I
could see the text "Temperature".
 
Am 21.09.2010 11:24, schrieb C:
This thing does not rotate the text. I have tried a dozen things now,
but can't get this to rotate the text.
Public Class Form1
    Dim frmImage As Bitmap = New Bitmap(1000, 800)
    Dim longBmp As Bitmap = New Bitmap(250, 250)
    Sub WriteAngledText(ByVal gr As Graphics, ByVal text As String,
ByVal angle As Single)
        gr.RotateTransform(angle, Drawing2D.MatrixOrder.Append)' what
is this append for?

The rotation matrix is an additional matrix.
The final matrix is the product of the current transformation matrix
multiplied by the rotation matrix. With matrix multiplications,
in opposite to usual multiplications, the order does matter. For example,
if you first rotate, then translate an object, the result is different
from what you get if you first translate, then rotate it. Therefore,
you can specify if the rotation has to be applied before or after the
current tranformation. That's why you can append/prepand the new
transformation.

To your problem: It rotates around the origin, so it's probably outside
the Form. If you want to rotate _at_ 200|200, you must first rotate,
then translate:

         gr.TranslateTransform(200, 200)
         gr.RotateTransform(angle)      'Default is prepend = rotate first
         gr.DrawString(text, Label3.Font, Brushes.BlueViolet, 0, 0)
        gr.DrawString(text, Label3.Font, Brushes.BlueViolet, 200, 200)
    End Sub
[...]
This does not show anything at all. Earlier with glong.DrawString I
could see the text "Temperature".

Thanks for this explanation. I still did not manage it. I have now
made it a little better. Tried various angles and displacements for
the text, but don't get the desired result (writing vertical text for
the y axis).

The subroutine should be more useful in general:

Sub WriteAngledText(ByVal gr As Graphics, ByVal text As String,
Optional ByVal angle As Single = -90, Optional ByVal x As Single = 0,
Optional ByVal y As Single = 0)

gr.TranslateTransform(-x, -y, Drawing2D.MatrixOrder.Append)
gr.RotateTransform(angle, Drawing2D.MatrixOrder.Append)
gr.DrawString(text, Label1.Font, Brushes.Red, x, y)
gr.RotateTransform(-angle, Drawing2D.MatrixOrder.Append)
gr.TranslateTransform(x, y, Drawing2D.MatrixOrder.Append)

End Sub

Everything still goes to the upper left corner at different angles, so
the -90 degree angle text is not visible.

Dim g As Graphics = Graphics.FromImage(frmImage)
Dim glong As Graphics = Graphics.FromImage(longBmp)

Call WriteAngledText(glong, "Temperatures", -0, 0, 80)
Call WriteAngledText(glong, "Temperatures", -10, 0, 80)
Call WriteAngledText(glong, "Temperatures", -30, 0, 80)
Call WriteAngledText(glong, "Temperatures", -60, 0, 80)
Call WriteAngledText(glong, "Temperatures", -90, 0, 80)
longBmp.MakeTransparent(longBmp.GetPixel(1, 1))
PictureBox1.Image = longBmp : PictureBox1.Size = longBmp.Size

Me.Invalidate()

In the Form_Paint process, I can still do one translate, but if there
is nothing in the image longBmp, then nothing appears.
 
Am 21.09.2010 21:58, schrieb C:
Am 21.09.2010 11:24, schrieb C:
This thing does not rotate the text. I have tried a dozen things now,
but can't get this to rotate the text.
Public Class Form1
Dim frmImage As Bitmap = New Bitmap(1000, 800)
Dim longBmp As Bitmap = New Bitmap(250, 250)
Sub WriteAngledText(ByVal gr As Graphics, ByVal text As String,
ByVal angle As Single)
gr.RotateTransform(angle, Drawing2D.MatrixOrder.Append) ' what
is this append for?

The rotation matrix is an additional matrix.
The final matrix is the product of the current transformation matrix
multiplied by the rotation matrix. With matrix multiplications,
in opposite to usual multiplications, the order does matter. For example,
if you first rotate, then translate an object, the result is different
from what you get if you first translate, then rotate it. Therefore,
you can specify if the rotation has to be applied before or after the
current tranformation. That's why you can append/prepand the new
transformation.

To your problem: It rotates around the origin, so it's probably outside
the Form. If you want to rotate _at_ 200|200, you must first rotate,
then translate:

gr.TranslateTransform(200, 200)
gr.RotateTransform(angle) 'Default is prepend = rotate first
gr.DrawString(text, Label3.Font, Brushes.BlueViolet, 0, 0)
gr.DrawString(text, Label3.Font, Brushes.BlueViolet, 200, 200)
End Sub
[...]
This does not show anything at all. Earlier with glong.DrawString I
could see the text "Temperature".

Thanks for this explanation. I still did not manage it. I have now
made it a little better. Tried various angles and displacements for
the text, but don't get the desired result (writing vertical text for
the y axis).

The subroutine should be more useful in general:

Sub WriteAngledText(ByVal gr As Graphics, ByVal text As String,
Optional ByVal angle As Single = -90, Optional ByVal x As Single = 0,
Optional ByVal y As Single = 0)

gr.TranslateTransform(-x, -y, Drawing2D.MatrixOrder.Append)
gr.RotateTransform(angle, Drawing2D.MatrixOrder.Append)
gr.DrawString(text, Label1.Font, Brushes.Red, x, y)
gr.RotateTransform(-angle, Drawing2D.MatrixOrder.Append)
gr.TranslateTransform(x, y, Drawing2D.MatrixOrder.Append)

End Sub

Everything still goes to the upper left corner at different angles, so
the -90 degree angle text is not visible.

Dim g As Graphics = Graphics.FromImage(frmImage)
Dim glong As Graphics = Graphics.FromImage(longBmp)

Call WriteAngledText(glong, "Temperatures", -0, 0, 80)
Call WriteAngledText(glong, "Temperatures", -10, 0, 80)
Call WriteAngledText(glong, "Temperatures", -30, 0, 80)
Call WriteAngledText(glong, "Temperatures", -60, 0, 80)
Call WriteAngledText(glong, "Temperatures", -90, 0, 80)
longBmp.MakeTransparent(longBmp.GetPixel(1, 1))
PictureBox1.Image = longBmp : PictureBox1.Size = longBmp.Size

Me.Invalidate()

In the Form_Paint process, I can still do one translate, but if there
is nothing in the image longBmp, then nothing appears.


I'm not sure which result you expect exactly. Maybe...:

Sub WriteAngledText( _
ByVal gr As Graphics, ByVal text As String, _
Optional ByVal angle As Single = -90, Optional ByVal x As Single = 0, _
Optional ByVal y As Single = 0)

gr.TranslateTransform(x, y)
gr.RotateTransform(angle)
gr.DrawString(text, Label1.Font, Brushes.Red, 0, 0)
gr.ResetTransform()

End Sub
 
Am 21.09.2010 21:58, schrieb C:




Am 21.09.2010 11:24, schrieb C:
This thing does not rotate the text. I have tried a dozen things now,
but can't get this to rotate the text.
Public Class Form1
    Dim frmImage As Bitmap = New Bitmap(1000, 800)
    Dim longBmp As Bitmap = New Bitmap(250, 250)
    Sub WriteAngledText(ByVal gr As Graphics, ByVal text As String,
ByVal angle As Single)
        gr.RotateTransform(angle, Drawing2D.MatrixOrder.Append) ' what
is this append for?
The rotation matrix is an additional matrix.
The final matrix is the product of the current transformation matrix
multiplied by the rotation matrix. With matrix multiplications,
in opposite to usual multiplications, the order does matter. For example,
if you first rotate, then translate an object, the result is different
from what you get if you first translate, then rotate it. Therefore,
you can specify if the rotation has to be applied before or after the
current tranformation. That's why you can append/prepand the new
transformation.
To your problem: It rotates around the origin, so it's probably outside
the Form. If you want to rotate _at_ 200|200, you must first rotate,
then translate:
         gr.TranslateTransform(200, 200)
         gr.RotateTransform(angle)      'Default is prepend = rotate first
         gr.DrawString(text, Label3.Font, Brushes.BlueViolet, 0, 0)
        gr.DrawString(text, Label3.Font, Brushes.BlueViolet, 200, 200)
    End Sub
[...]
This does not show anything at all. Earlier with glong.DrawString I
could see the text "Temperature".
Thanks for this explanation. I still did not manage it. I have now
made it a little better. Tried various angles and displacements for
the text, but don't get the desired result (writing vertical text for
the y axis).
The subroutine should be more useful in general:
    Sub WriteAngledText(ByVal gr As Graphics, ByVal text As String,
Optional ByVal angle As Single = -90, Optional ByVal x As Single = 0,
Optional ByVal y As Single = 0)
        gr.TranslateTransform(-x, -y, Drawing2D.MatrixOrder.Append)
        gr.RotateTransform(angle, Drawing2D.MatrixOrder.Append)
        gr.DrawString(text, Label1.Font, Brushes.Red, x, y)
        gr.RotateTransform(-angle, Drawing2D.MatrixOrder.Append)
        gr.TranslateTransform(x, y, Drawing2D.MatrixOrder.Append)
    End Sub
Everything still goes to the upper left corner at different angles, so
the -90 degree angle text is not visible.
        Dim g As Graphics = Graphics.FromImage(frmImage)
        Dim glong As Graphics = Graphics.FromImage(longBmp)
        Call WriteAngledText(glong, "Temperatures", -0, 0, 80)
        Call WriteAngledText(glong, "Temperatures", -10, 0, 80)
        Call WriteAngledText(glong, "Temperatures", -30, 0, 80)
        Call WriteAngledText(glong, "Temperatures", -60, 0, 80)
        Call WriteAngledText(glong, "Temperatures", -90, 0, 80)
        longBmp.MakeTransparent(longBmp.GetPixel(1, 1))
        PictureBox1.Image = longBmp : PictureBox1.Size = longBmp.Size
        Me.Invalidate()
In the Form_Paint process, I can still do one translate, but if there
is nothing in the image longBmp, then nothing appears.

I'm not sure which result you expect exactly. Maybe...:

 Sub WriteAngledText( _
      ByVal gr As Graphics, ByVal text As String, _
      Optional ByVal angle As Single = -90, Optional ByVal x As Single = 0, _
      Optional ByVal y As Single = 0)

      gr.TranslateTransform(x, y)
      gr.RotateTransform(angle)
      gr.DrawString(text, Label1.Font, Brushes.Red, 0, 0)
      gr.ResetTransform()

   End Sub

Thanks. Your code works fine.
 
Back
Top