背后的新 System.Drawing.Font 代码

Dan*_*ick 2 css c# fonts

我正在尝试使用名为 Black Rose 的字体在 c# 中创建图像。图像被创建,但不是我想要的字体。这是我用来创建图像的代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        string MoneyImg = "1000";
        Bitmap bitmap = new Bitmap(MoneyImg.Length * 40, 150);

        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            Font oFont = new System.Drawing.Font("BLACKR", 20);
            PointF point = new PointF(2f, 2f);
            SolidBrush black = new SolidBrush(Color.Black);
            SolidBrush white = new SolidBrush(Color.White);
            graphics.FillRectangle(white, 0, 0, bitmap.Width, bitmap.Height);
            graphics.DrawString("$" + MoneyImg , oFont, black, point);
        }
    }
Run Code Online (Sandbox Code Playgroud)

我试着改变这条线

Font oFont = new System.Drawing.Font("BLACKR", 20);
Run Code Online (Sandbox Code Playgroud)

Font oFont = new System.Drawing.Font("BLACKR.TTF", 20);
Run Code Online (Sandbox Code Playgroud)

但没有任何区别。我知道字体文件位于正确的位置,因为我使用 CSS 样式进行了测试,结果显示正常。这是 CSS 代码和屏幕截图。

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
        @font-face 
        { 
        font-family: Black Rose; src: url('BLACKR.TTF'); 
        } 
        h3 {
        font-family: 'Black Rose'
        }
</style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Image ID="Image1" runat="server" />
        <br />
        <h3>This is what the Image Font Looks Like</h3>
    </div>
    </form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

测试截图

Dav*_*vid 6

主机系统可能根本不知道该字体。该文件可能位于 Web 应用程序文件层次结构中的某个位置,但这对底层系统没有影响。Windows 不会仅仅因为该字体文件存在于硬盘驱动器上的某个位置就知道该字体。

您可以将字体添加到PrivateFontCollectionin 代码并从中加载它。像这样的东西:

var myFonts = new System.Drawing.Text.PrivateFontCollection();
myFonts.AddFontFile(@"C:\path\to\BLACKR.TTF");
var oFont = new System.Drawing.Font(myFonts.Families[0], 20);
Run Code Online (Sandbox Code Playgroud)