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

标签 c# asp.net fonts

我在我的 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);
    }

最佳答案

假设基于 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;
}

关于c# - 在 asp.net web 服务器上安装字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23158288/

相关文章:

asp.net - 适合新 .NET 开发人员的评估表?

html - CSS 字体无法在设备上正确显示

c# - 静态与非静态方法

c# - 为什么 PasswordBox 在 Silverlight 中被密封?

c# - 无法添加具有空角色定义绑定(bind)集合的角色分配

asp.net - IIS 7 工作进程瓶颈,应用程序池 ASP.NET 3.5 + 2.0 下大量等待请求

Asp.net FluentValidation 单元测试对象为 null

java - 通用更改字体 - richfaces

html - 使用@font-face 呈现 Gill Sans 字体系列

c# - 将 Mac 通知与 Monomac 结合使用