Mirrored text

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
I am trying to draw on the screen, with Y being positive from the bottom up - like a normal graph.

VB.NET:
Private mYFlip As New Matrix(1, 0, 0, -1, 0, 0)

Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    With e.Graphics
        .Transform = mYFlip
        .ScaleTransform(0.43, 0.43)
        .TranslateTransform(300, -1550)

        'etc etc
    End With
End Sub

I can now draw what I need to. However, as expected, when I try and draw text at, for example (100, 100), the text is up the wrong way. How do I apply another transformation to temporarily flip the matrix back the correct way?
 
I added a little something extra (VerticalMirror is a RadioButton):
VB.NET:
Private Sub VerticalMirror_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VerticalMirror.CheckedChanged
    pbMirroredText.Invalidate()
  End Sub
Private Sub pbMirroredText_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pbMirroredText.Paint
    Dim drawingArea As Rectangle
    Dim drawingArea2 As Rectangle
    Dim saveState As GraphicsState
    Dim mirrorMatrix As Drawing2D.Matrix
    Dim TEXT As String = tb.Text
    Dim font As New Font("Garamond", 24, FontStyle.Bold)
    e.Graphics.Clear(Color.White)
    If (VerticalMirror.Checked = True) Then
      drawingArea = New Rectangle(0, 0, pbMirroredText.Width, pbMirroredText.Height / 2 + 2)
      drawingArea2 = New Rectangle(0, pbMirroredText.Height / 2, pbMirroredText.Width, pbMirroredText.Height - 1)
      Using lb1 As New LinearGradientBrush(drawingArea, Color.Navy, Color.DarkGray, LinearGradientMode.Vertical)
        Using lb2 As New LinearGradientBrush(drawingArea2, Color.DarkGray, Color.Navy, LinearGradientMode.Vertical)
          e.Graphics.FillRectangle(lb1, drawingArea)
          e.Graphics.FillRectangle(lb2, drawingArea2)
        End Using
      End Using
    Else
      drawingArea = New Rectangle(0, 0, pbMirroredText.Width / 2, pbMirroredText.Height)
      drawingArea2 = New Rectangle(pbMirroredText.Width / 2, 0, pbMirroredText.Width, pbMirroredText.Height)
      Using lb1 As New LinearGradientBrush(drawingArea, Color.Navy, Color.DarkGray, LinearGradientMode.Horizontal)
        Using lb2 As New LinearGradientBrush(drawingArea2, Color.DarkGray, Color.Navy, LinearGradientMode.Horizontal)
          e.Graphics.FillRectangle(lb1, drawingArea)
          e.Graphics.FillRectangle(lb2, drawingArea2)
        End Using
      End Using
    End If
    e.Graphics.DrawString(TEXT, font, _
             Brushes.White, drawingArea)
    saveState = e.Graphics.Save()
    If (VerticalMirror.Checked = True) Then
      mirrorMatrix = New Drawing2D.Matrix(1, 0, 0, -1, 0, pbMirroredText.ClientRectangle.Height)
    Else
      mirrorMatrix = New Drawing2D.Matrix(-1, 0, 0, 1, pbMirroredText.ClientRectangle.Width, 0)
    End If
    e.Graphics.Transform = mirrorMatrix
       e.Graphics.DrawString(TEXT, font, _
             Brushes.Black, drawingArea)
    e.Graphics.Restore(saveState)
  End Sub
 
Last edited:
Not the perfect solution by a long way, but a solution at least :-

VB.NET:
Private mYFlip As New Matrix(1, 0, 0, -1, 0, 0)

Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    With e.Graphics
        .Transform = mYFlip
        .ScaleTransform(0.43, 0.43)
        .TranslateTransform(300, -1550)

        'mark origin
        .DrawLine(Pens.Black, 0, 0, 150, 0)
        DrawCorrectedString(e.Graphics, "X+", New Font("Arial", 36, FontStyle.Bold), Brushes.Black, 175, 0, sfMiddleCenter)
        .DrawLine(Pens.Black, 0, 0, 0, 150)
        DrawCorrectedString(e.Graphics, "Y+", New Font("Arial", 36, FontStyle.Bold), Brushes.Black, 0, 175, sfMiddleCenter)

        'etc etc
    End With
End Sub

Private gsSavedState As GraphicsState
Private PointsToTransform(0) As PointF
Private FontScalingElement As Single
Private Sub DrawCorrectedString(ByVal eg As Graphics, ByVal whatString As String, ByVal whatFont As Font, ByVal whatBrush As Brush, ByVal whatX As Integer, ByVal whatY As Integer, ByVal whatStringFormat As StringFormat)
    With eg
        PointsToTransform(0) = New Point(whatX, whatY)
        .TransformPoints(CoordinateSpace.Page, CoordinateSpace.World, PointsToTransform)
        FontScalingElement = .Transform.Elements(0)
        gsSavedState = .Save
        .ResetTransform()
        Using ResizedFont As New Font(whatFont.FontFamily, whatFont.Size * FontScalingElement, whatFont.Style, whatFont.Unit)
            .DrawString(whatString, ResizedFont, whatBrush, PointsToTransform(0).X, PointsToTransform(0).Y, whatStringFormat)
        End Using
        .Restore(gsSavedState)
    End With
End Sub
 

Latest posts

Back
Top