如何以编程方式访问字体

How to access the font programmatically

我正在尝试以编程方式访问字体,因为我无法在共享主机上安装字体

我用这个代码

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    Dim collection As New PrivateFontCollection()
    collection.AddFontFile(Server.MapPath("~\ShadowsIntoLight.ttf"))

    Dim image As New Bitmap(500, 500)
    Dim g As Graphics = Graphics.FromImage(image)

    Dim RedBrush As New SolidBrush(Color.Black)
    Dim drawFont As New Font("Shadows Into Light", 36)

    g.DrawString("the lazy fox jumped over the brown log", drawFont, RedBrush, 0, 100)

    Dim path As String = Server.MapPath("~/image.png")
    image.Save(path, ImageFormat.Png)
    g.Dispose()
    image.Dispose()

    Response.Write("<img src=""image.png""/>")

End Sub

但它始终显示 Arial 字体。 我怎样才能让它显示特定的字体

谢谢

后台能不能不用CSS? 将自定义字体添加到 'Fonts folder' 中的解决方案 > 将现有项目添加到 visual studio

中的字体文件夹

示例:(使用我下载的随机字体)

@font-face{
font-family: myFont;
src: url(/Fonts/Belleza-Regular.ttf);

}

.MyClass{
color:green;
font-family: myFont;
 }

然后将此字体附加到代码中的任何位置?

myControl.CssClass = "MyClass" etc..



可能比您想要的稍长一些,并且只有在您附加到控件时才会起作用,但这可能是一个不错的解决方法。

编辑:

也许是这样的? Using custom TTF font for DrawString image rendering