【问题标题】:install font on asp.net web server在 asp.net 网络服务器上安装字体
【发布时间】:2014-04-26 00:21:41
【问题描述】:

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

【问题讨论】:

标签: c# asp.net fonts


【解决方案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;
}

【讨论】:

    【解决方案2】:

    如果您正在为相对现代的浏览器进行开发,您可以阅读 @font-face

    或者,在您的网络服务器上安装字体可能只是一件简单的事情,通常您只需将字体文件复制到服务器上的某个文件夹,例如桌面,这应该只是一件简单的事情右键单击并选择“安装字体”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      • 2010-09-06
      相关资源
      最近更新 更多