0

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

**

4

2 回答 2

3

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

所以,正如斯蒂芬已经说过的,这在服务器上是不可能的,因为有很多因素会影响服务器无法控制的文本大小。

通过将桌面应用程序转换为 mvc 应用程序,这部分 (GUI) 的等价物将是JavaScript中的实现。
在 JavaScript 中,您可以测量文本大小

于 2016-03-16T06:03:37.573 回答
0
        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;
    }
于 2020-09-08T20:33:11.417 回答