2

I want to rotate the string in the image but can't do it, can anybody help me, i tried "drawformat" but it doesn't rotate but just center. I want rotate B in Case "K-11" 90 degrees, B 45 degrees and C 90 degrees in "K-21" or somethings like that. Thank you very much.

Public Class ImageRebartoRevit
    Private Sub OK_Click(sender As Object, e As EventArgs) Handles OK.Click
        xuat_shapcode_revit(ShapeCode.Text)
    End Sub
    Function xuat_shapcode_revit(shapecode As String) As Boolean
        Dim newBitmap As Bitmap
        newBitmap = New Bitmap("C:\Users\ADMIN\Desktop\IMG\" + shapecode + ".png")
        Dim Graphics As Graphics
        Dim A As String = L1.Text
        Dim B As String = L2.Text
        Dim C As String = L3.Text        
        Graphics = Graphics.FromImage(newBitmap)

        Dim drawformat As New StringFormat()
        drawformat.Alignment = StringAlignment.Center

        Select Case shapecode
            Case "K-00"
                Graphics.DrawString(A, New Font("Arial", 50, FontStyle.Regular), New SolidBrush(Color.Black), New Point(470, 90), drawformat)
            Case "K-11"
                Graphics.DrawString(A.ToString, New Font("Arial", 50, FontStyle.Regular), New SolidBrush(Color.Black), New Point(843, 128), drawformat)
                Graphics.DrawString(B.ToString, New Font("Arial", 50, FontStyle.Regular), New SolidBrush(Color.Black), New Point(457, 124), drawformat)
            Case "K-21"
                Graphics.DrawString(A.ToString, New Font("Arial", 50, FontStyle.Regular), New SolidBrush(Color.Black), New Point(75, 139), drawformat)
                Graphics.DrawString(B.ToString, New Font("Arial", 50, FontStyle.Regular), New SolidBrush(Color.Black), New Point(467, 124), drawformat)
                Graphics.DrawString(C.ToString, New Font("Arial", 50, FontStyle.Regular), New SolidBrush(Color.Black), New Point(859, 139), drawformat)
            
        End Select
        newBitmap.Save("C:\Users\ADMIN\Desktop\IMG\out_put_" + shapecode + ".bmp", System.Drawing.Imaging.ImageFormat.Jpeg)
        Return True
    End Function
End Class
serges_newj
  • 795
  • 1
  • 13
  • 23
  • 2
    *"it doesn't rotate but just center"*. Of course it does, because that's specifically what you have told it to do by setting the `Alignment`. The `FormatFlags` property can be used to draw the text vertically although, from memory, that still draws the characters normally but each under the previous one. If you actually want the characters rotated then you need transform the matrix associated with the `Graphics` object. You should look into the `RotateTransform` method of that object, but you will likely have to call `TranslateTransform` first. – jmcilhinney Oct 29 '20 at 06:12
  • 2
    By the way, you should be disposing your `Graphics` object when you're done with it. ALWAYS dispose objects that support it when you're done with them. If you use them only in a narrow scope, as here, you should create them with a `Using` statement and then they will be implicitly disposed at the end of the block. – jmcilhinney Oct 29 '20 at 06:13
  • Draw the strings as usual then rotate the image. Something like [this](https://stackoverflow.com/a/58444045/14171304). – dr.null Oct 29 '20 at 07:35
  • 1
    Can you post a sample Image of the desired results? Including the content of `L1` and friends. @dr.null That's terrifying :) Just [Matrix.RotateAt(Angle, Point)](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.drawing2d.matrix.rotateat) is needed. – Jimi Oct 29 '20 at 14:31
  • Thanks all, I'm trying the ways you showed me. I have learned a lot. I'm a beginner. – maxlevel2011 Oct 29 '20 at 15:02
  • @Jimi Then something like [this](https://stackoverflow.com/a/56075512/14171304). (^_-) – dr.null Oct 29 '20 at 15:06
  • @dr.null That could work :) Btw, if/when the OP has posted a sample of the results, you can take that code an post an answer if you want. In that case, note that Bitmap, Graphics objects, Font, Brush and StringFormat (if actually needed), all need to be declared with `Using` statements. The path of the folder is `dim folderPath as string = $"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\IMG"` – Jimi Oct 29 '20 at 15:19

1 Answers1

0
            Graphics.DrawString(A.ToString, New Font("Arial", 50, FontStyle.Regular), New SolidBrush(Color.Black), New Point(220, 215), drawformat)

            Graphics1.TranslateTransform(50, 230)
            Graphics1.RotateTransform(-29)
            Graphics1.DrawString(B.ToString, New Font("Arial", 50, FontStyle.Regular), New SolidBrush(Color.Black), New Point(408, 119), drawformat)

            Graphics.DrawString(C.ToString, New Font("Arial", 50, FontStyle.Regular), New SolidBrush(Color.Black), New Point(820, 45), drawformat)

            Graphics2.TranslateTransform(910, 1010)
            Graphics2.RotateTransform(-90)
            Graphics2.DrawString(D.ToString, New Font("Arial", 50, FontStyle.Regular), New SolidBrush(Color.Black), New Point(940, 40), drawformat)

Hi everyone, I figured it out and done it, did you guys see the best way?

  • Is this an answer? Or are you asking a question? If you are asking a question then please delete this and add it to the question above. If this is an answer then please make it look more like an answer. Add some explanation... – Sabito stands with Ukraine Nov 13 '20 at 02:44