3

i am using this code in my asp.net website. its barcode generation code.. problem this code is depend on (IDAutomationHC39M) this font. so on localhost i have installed this font in my fonts folder and code is working successfully in local. but i do not know how to install this on server

   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);
    }
dvirus
  • 201
  • 2
  • 21

2 Answers2

1

if you are developing for relatively modern browsers you can read up on @font-face

or, it may be just a simple matter of installing the font on your web server, typically you just need to copy the font file over to some folder on the server, say the desktop, and it should just be a simple matter of right clicking and selecting "install font"

fnostro
  • 4,531
  • 1
  • 15
  • 23
1

Assuming a web based font will not work (ie you need to render your barcode server side and possibly embed it with other graphics / images), you can take the following approach. I didn't write this code but have used it and it does work.

You will need to load your font from a resource or possibly via a URL. Then pass the bits to the following. If loading from a resource, google that as there are a fair number of examples.

You can also use fontCollection.AddFontFile() and simplify your code but that will require access to the local (on server) filesystem.

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;
}
andleer
  • 22,388
  • 8
  • 62
  • 82