0

I have a problem in flipping text in VB.NET It is flipped but with no line brake

See the Link: http://www.spider-news.net/Flip_Text_question.JPG

Imports System.Drawing.Drawing2D
Imports System.Drawing


Public Class Form1

  Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint


    ' Draw the text and the surrounding rectangle START.
    Dim text1 As String = RichTextBox1.Text
    Dim font1 As New Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Point)
    Try
        Dim rect1 As New Rectangle(10, 10, 1000, 140)

        ' Create a StringFormat object with the each line of text, and the block 
        ' of text centered on the page. 
        Dim stringFormat As New StringFormat()
        stringFormat.Alignment = StringAlignment.Center
        stringFormat.LineAlignment = StringAlignment.Center


        ' Draw the text and the surrounding rectangle.
        e.Graphics.DrawString(text1, font1, Brushes.Blue, rect1, stringFormat)
        e.Graphics.DrawRectangle(Pens.Black, rect1)


    Finally
        font1.Dispose()
    End Try
    ' Draw the text and the surrounding rectangle END.


    '' FLIP TEXT ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Draw Flipped Text the text surrounding rectangle START.

    Using the_font As New Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Point)

        DrawFlippedText(e.Graphics, the_font, Brushes.Black, 10, 10, RichTextBox1.Text, True, False)

        Dim txt_size As SizeF
        txt_size = e.Graphics.MeasureString(RichTextBox1.Text, the_font)
        e.Graphics.DrawRectangle(Pens.Red, 10, 10, txt_size.Width, txt_size.Height)

    End Using

    ' Draw Flipped Text the text surrounding rectangle END.
    '' FLIP TEXT ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End Sub

Public Sub DrawFlippedText(ByVal gr As Graphics, ByVal the_font As Font, ByVal the_brush As Brush, ByVal x As Integer, ByVal y As Integer, ByVal txt As String, ByVal flip_x As Boolean, ByVal flip_y As Boolean)

    ' Save the current graphics state.
    Dim state As GraphicsState = gr.Save()

    ' Set up the transformation.
    Dim scale_x As Integer = IIf(flip_x, -1, 1)
    Dim scale_y As Integer = IIf(flip_y, -1, 1)
    gr.ResetTransform()
    gr.ScaleTransform(scale_x, scale_y)


    ' Figure out where to draw.
    Dim txt_size As SizeF = gr.MeasureString(txt, the_font)

    If flip_x Then x = -x - RichTextBox1.Size.Width
    If flip_y Then y = -y - RichTextBox1.Size.Height


    Dim rect1 As New Rectangle(10, 10, 1000, 140)
    Dim stringFormat As New StringFormat()
    stringFormat.Alignment = StringAlignment.Center
    stringFormat.LineAlignment = StringAlignment.Center



    ' Draw.
    gr.DrawString(txt, the_font, the_brush, x, y)

    ' Restore the original graphics state.
    gr.Restore(state)

End Sub


End Class

Please HELP

SysDragon
  • 9,692
  • 15
  • 60
  • 89
Nazim Iqbal
  • 21
  • 1
  • 4

2 Answers2

0

My guess is that if the linebreaks are not there you have to split the string into single words.

Then concatenate the words one by one and measure the lenght. if it exceeds your line width draw this string and continue with the next words.

The next draw should be on y-coordinate + your line-height.

I did this in a pdf where i place a text to an absolute position which could be more than 1 line:

 Dim splitted As String() = text.Split()
        Dim tempchunk As Chunk = New Chunk("", pdfFont)
        Dim count As Integer = 0
        For Each s As String In splitted
            Dim chunk2 As Chunk
            chunk2 = New Chunk(tempchunk.Content, pdfFont)
            chunk2.Append(" " & s)
            If chunk2.GetWidthPoint() > 155 Then
                cb.SaveState()
                cb.BeginText()
                cb.MoveText(x, y - (13 * count))
                cb.SetFontAndSize(bfont, 11)
                cb.ShowText(tempchunk.Content.Trim())
                cb.EndText()
                cb.RestoreState()
                tempchunk = New Chunk(s, pdfFont)
                count += 1
            Else
                tempchunk.Append(" " & s)
            End If
        Next
        If tempchunk.Content <> "" Then
            cb.SaveState()
            cb.BeginText()
            cb.MoveText(x, y - (13 * count))
            cb.SetFontAndSize(bfont, 11)
            cb.ShowText(tempchunk.Content.Trim())
            cb.EndText()
            cb.RestoreState()
        End If

Its the code for the pdf but maybe it helps

Nikolaj Zander
  • 1,270
  • 9
  • 13
0

Try this.

I created a bitmap, draw the string and rectangle there, flipped it, then draw the bitmap (with flipped text) on the Form.

    Public Class Form1
      Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        Dim b As New Bitmap(300, 100)
        Dim g As Graphics = Graphics.FromImage(b)
        Dim d As Graphics = Me.CreateGraphics

        Dim r As New Rectangle(0, 0, b.Width - 1, b.Height - 1)
        Dim f As New StringFormat
        f.Alignment = StringAlignment.Center
        f.LineAlignment = StringAlignment.Center

        g.Clear(BackColor)
        g.DrawRectangle(Pens.Red, r)
        g.DrawString(RichTextBox1.Text, RichTextBox1.Font, Brushes.Blue, r, f)
        b.RotateFlip(RotateFlipType.RotateNoneFlipX)
        d.DrawImageUnscaled(b, 10, 10)

        g.Dispose()
        b.Dispose()
        d.Dispose()
    End Sub
End Class
Arvie
  • 47
  • 4