Search code examples
c#asp.net-mvchtml5-canvastextrenderer

What is TextRenderer.MeasureText fucntion equivalent in c# mvc


I am using Visual studio 2013 and Mvc Framework. We are migrating the window desktop application to mvc web application.Right now i am searching the TextRenderer.MeasureText equivalent function for c# which i use in my project. Simple i want this convert function in mvc c#. Basically i searching Textrender.MeasureText Alternate option for this technology.

  **

Private Sub DrawPointText(ByRef gr As Graphics, ByVal Color As Drawing.Color, ByRef Point As PointF, _
                        ByVal Corner As String, ByVal strOutput As String, Optional ByVal optFont As Font = Nothing, _
                        Optional ByVal intRotate As Integer = 0)
        Dim fnt As New Font("New Times Roman", 12, FontStyle.Bold)
        Dim strX As String
        Dim TextPositionX As Double
        Dim TextPositionY As Double
        Dim TextShift As Size
        Dim OrgPoint As VGS.PointD
        Dim drawFormat As New System.Drawing.StringFormat
        If Not optFont Is Nothing Then
            fnt = optFont
        End If
        OrgPoint = RevertValue(Point)
        strX = strOutput
        TextShift = TextRenderer.MeasureText(strX, fnt)
        Select Case Corner
            Case "NE"
                TextPositionX = Point.X
                TextPositionY = Point.Y - TextShift.Height
            Case "SE"
                TextPositionX = Point.X
                TextPositionY = Point.Y
            Case "SW"
                TextPositionX = Point.X - TextShift.Width
                TextPositionY = Point.Y
            Case "NW"
                TextPositionX = Point.X - TextShift.Width
                TextPositionY = Point.Y - TextShift.Height
            Case Else
                MessageBox.Show("Unknown Corner In DrawPointText")
        End Select
        If intRotate = 0 Then
            gr.DrawString(strX, fnt, New SolidBrush(Color), TextPositionX, TextPositionY, drawFormat)
        Else
            drawFormat.FormatFlags = StringFormatFlags.DirectionVertical
            gr.RotateTransform(intRotate)
            If intRotate > 0 Then
                gr.DrawString(strX, fnt, New SolidBrush(Color), TextPositionY, -1 * TextPositionX) ', drawFormat
            Else
                gr.DrawString(strX, fnt, New SolidBrush(Color), -1 * TextPositionY, TextPositionX) ', drawFormat
            End If
            gr.RotateTransform(-1 * intRotate)
        End If
    End Sub

**


Solution

  • The text size is dependent on many different factors, like the font size, zooming factor, the font itself and so on.
    You don't even know if the font you are using is installed on the client.

    So, as Stephen already said, this is impossible on the server, because there are many factors that affect the text size the server can't control.

    By converting a desktop application to an mvc application, the equivalent for this part (GUI) would be an implementation in JavaScript.
    In JavaScript you can measure the text size.