在ASP.NET Web服务器上安装字体

3

我在我的asp.net网站中使用这段代码。它是生成条形码的代码。问题在于,这段代码依赖于(IDAutomationHC39M)这个字体。所以在本地主机上,我已经将这个字体安装在我的字体文件夹中,并且代码在本地成功运行。但是我不知道如何在服务器上安装这个字体。

   string barCode =  Request.QueryString["id"].ToString();
    System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
    using (Bitmap bitMap = new Bitmap(barCode.Length * 40, 80))
    {
        using (Graphics graphics = Graphics.FromImage(bitMap))
        {
            Font oFont = new Font("IDAutomationHC39M", 16);

            PointF point = new PointF(2f, 2f);
            SolidBrush blackBrush = new SolidBrush(Color.Black);
            SolidBrush whiteBrush = new SolidBrush(Color.White);
            graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
            graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);
        }
        using (MemoryStream ms = new MemoryStream())
        {
            bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            byte[] byteImage = ms.ToArray();

            Convert.ToBase64String(byteImage);
            imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
        }
        plBarCode.Controls.Add(imgBarCode);
    }

可能是 https://dev59.com/AHRB5IYBdhLWcg3wtJKF 的重复问题,在 C# 应用程序中从文件加载字体。 - Vasily Sliounaiev
2个回答

1
如果您正在开发相对较新的浏览器,您可以了解一下@font-face
或者,这可能只是一个简单的问题,只需要在您的Web服务器上安装字体,通常您只需要将字体文件复制到服务器上的某个文件夹中,比如桌面,然后右键单击并选择“安装字体”。

1
假设基于Web的字体无法使用(即您需要在服务器端呈现条形码并可能将其嵌入其他图形/图像),则可以采用以下方法。我没有编写此代码,但已经使用它,并且它确实有效。
您需要从资源或可能通过URL加载字体。然后将位传递给以下内容。如果从资源加载,请谷歌一下,因为有相当多的示例。
您还可以使用fontCollection.AddFontFile()简化您的代码,但这将需要访问本地(服务器上的)文件系统。
public FontFamily GetFontFamily(byte[] bytes)
{
    FontFamily fontFamily = null;

    var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);

    try
    {
        var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(bytes, 0);
        var fontCollection = new PrivateFontCollection();
        fontCollection.AddMemoryFont(ptr, bytes.Length);
        fontFamily = fontCollection.Families[0];
    }
    finally
    {
        // don't forget to unpin the array!
        handle.Free();
    }

    return fontFamily;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接