【问题标题】:What is TextRenderer.MeasureText fucntion equivalent in c# mvc什么是 c# mvc 中的 TextRenderer.MeasureText 等效功能
【发布时间】:2016-03-16 05:36:26
【问题描述】:

我正在使用 Visual Studio 2013 和 Mvc 框架。我们正在将窗口桌面应用程序迁移到 mvc Web 应用程序。现在我正在为我在项目中使用的 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

**

【问题讨论】:

  • 你这样做是为了什么?它是一个 Web 应用程序,因此它是执行渲染的客户端浏览器,而不是服务器。
  • 我正在使用画布(html5),我想根据点(x,y)制作绘图文本
  • 您可以创建服务器端 HtmlHeper 扩展方法来创建 html,包括在画布上定位文本的原点,但您需要 javascript 来确定呈现的文本宽度

标签: c# asp.net-mvc html5-canvas textrenderer


【解决方案1】:

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

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

通过将桌面应用程序转换为 mvc 应用程序,这部分 (GUI) 的等效 将是 JavaScript 中的实现。
在 JavaScript 中你 can measure the text size.

【讨论】:

    【解决方案2】:
            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;
        }
    

    【讨论】:

    • 欢迎来到 StackOverflow!我是否可以通过在代码的作用中添加上下文来改进您的答案。另外,常数是干什么用的?可能给他们一个名字将有助于这个线程的未来读者了解他们是什么。
    猜你喜欢
    • 1970-01-01
    • 2021-03-25
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 2012-01-11
    相关资源
    最近更新 更多