c#mvc中的TextRenderer.MeasureText函数等价物是什么

What is TextRenderer.MeasureText fucntion equivalent in c# mvc

我正在使用 Visual studio 2013 和 Mvc 框架。我们正在将 window 桌面应用程序迁移到 mvc web application.Right 现在我正在搜索我在我的项目中使用的 c# 的 TextRenderer.MeasureText 等效函数。 很简单,我想在 mvc c# 中使用这个转换函数。基本上我正在搜索 Textrender.MeasureText 此项技术的替代选项。

  **

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

**

文字大小取决于许多不同因素,例如字体大小,缩放系数,字体本身等等。
您甚至不知道您正在使用的字体是否安装在客户端上。

因此,正如 Stephen 所说,这在服务器上是不可能的,因为影响文本大小的因素有很多,服务器无法控制。

通过将桌面应用程序转换为 mvc 应用程序,此部分 (GUI) 的 等价物 将是 JavaScript.
在JavaScript你can measure the text size.

        private static double GetWidth(System.Drawing.Font stringFont, string text)
    {
        
        SizeF textSize;

        using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
        {
            textSize = g.MeasureString(text, stringFont);
        }
        double width = (double)(((textSize.Width / (double)7) * 256) - (128 / 7)) / 256;
        width = (double)decimal.Round((decimal)width + 0.5M, 2);

        return width;
    }