C# 在asp.net web服务器上安装字体

C# 在asp.net web服务器上安装字体,c#,asp.net,fonts,C#,Asp.net,Fonts,我正在asp.net网站中使用此代码。其条形码生成代码。。问题此代码依赖于(IDAutomationHC39M)此字体。所以在localhost上,我已经在我的字体文件夹中安装了这种字体,并且代码在本地成功运行。但我不知道如何在服务器上安装这个 string barCode = Request.QueryString["id"].ToString(); System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.W

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

   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);
    }

如果你正在开发相对现代的浏览器,你可以在上面阅读


或者,这可能只是在web服务器上安装字体的一个简单问题,通常您只需要将字体文件复制到服务器上的某个文件夹(例如桌面),并且只需右键单击并选择“安装字体”(假设基于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;
}
可能重复的