【问题标题】:how to generate bar code without installing the font "IDAutomationHC39M"如何在不安装字体“IDAutomationHC39M”的情况下生成条码
【发布时间】:2014-03-17 12:27:50
【问题描述】:

我正在使用 IDAutomationHC39M 字体生成条形码。

如果我需要在客户端系统中工作,我必须安装该字体。

如何绕过这个解决方案?

【问题讨论】:

    标签: c# asp.net barcode


    【解决方案1】:

    看看这篇 CodeProject 文章。他在不使用字体的情况下生成 Code 39 条码。

    http://www.codeproject.com/Articles/10344/Barcode-NET-Control

    还有这个SO问题:

    How to generate Code39 barcodes in vb.net

    在 Google 上快速搜索“Code 39 条码 .net”也会为您提供一些免费和商业的条码生成库和控件。

    【讨论】:

      【解决方案2】:

      您可以将字体作为资源添加到项目中,然后检索它:

      var value = Resources.MyFont; // it's stored as byte[]
      var fonts = new PrivateFontCollection();
      var memory = IntPtr.Zero;
      
      try
      {
          memory = Marshal.AllocCoTaskMem(value.Length);
      
          Marshal.Copy(value, 0, memory, value.Length);
          fonts.AddMemoryFont(memory, value.Length);
      }
      finally
      {
          Marshal.FreeCoTaskMem(memory);
      }
      
      var font = new Font(fonts.Families[0], 12F); // choose the size
      

      【讨论】:

        【解决方案3】:

        这是我使用的扩展方法:

        public static Image GetBarCode(this string data, int fontSizeEm)
                {
                    data = "*" + data + "*";
                    Image img = null;
                    Graphics drawing = null;
                    try
                    {
                        using (var pfc = new PrivateFontCollection())
                        {
                            pfc.AddFontFile(@"{{PATH TO YOUR FONT}}");
                            using (var myfont = new Font(pfc.Families[0], fontSizeEm))
                            {
                                img = new Bitmap(1, 1);
                                drawing = Graphics.FromImage(img);
                                var textSize = drawing.MeasureString(data, myfont);
                                img.Dispose();
                                drawing.Dispose();
                                img = new Bitmap((int) textSize.Width, (int) textSize.Height);
                                drawing = Graphics.FromImage(img);
                                drawing.DrawString(data, myfont, new SolidBrush(Color.Black), 0, 0);
                            }
                            drawing.Save();
                        }
                        return img;
                    }
                    catch
                    {
                        //Handle exception
                        return null;
                    }
                    finally
                    {
                        if(drawing!= null)
                            drawing.Dispose();
                    }
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-09-10
          • 2020-01-04
          • 2020-02-22
          • 2010-12-15
          • 1970-01-01
          • 2011-05-09
          • 1970-01-01
          • 2016-06-10
          相关资源
          最近更新 更多