史上最简洁C# 生成条形码图片思路及示例分享

2019-12-26 11:33:00王振洲

        /// <summary>
        /// 根据条形码绘制图片
        /// </summary>
        /// <param name="strNumber">条形码</param>
        /// <param name="intFontSize">字体大小</param>
        public void DrawingBarCode(string strNumber,int intFontSize)
        {
            PrivateFontCollection fonts = new PrivateFontCollection();
            //39带数字
            //fonts.AddFontFile(HttpContext.Current.Server.MapPath(".") + "/BarCodeFonts/V100010_.TTF");
            //FontFamily ff = new FontFamily("C39HrP48DlTt", fonts);
            //39码
             strNumber = "*" + strNumber + "*";
            fonts.AddFontFile(HttpContext.Current.Server.MapPath(".") + "/BarCodeFonts/FREE3OF9X.TTF");
            FontFamily ff = new FontFamily("Free 3 of 9 Extended", fonts);
            //接近条形码
            //fonts.AddFontFile(HttpContext.Current.Server.MapPath(".") + "/BarCodeFonts/V100014_.TTF");
            //FontFamily ff = new FontFamily("C39P24DlTt", fonts);
            Font font = new Font(ff, intFontSize);
            //设置图片大小
            Image img = new Bitmap(1, 1);
            Graphics g = Graphics.FromImage(img);
            SizeF fontSize = g.MeasureString(strNumber, font);
            int intWidth = Convert.ToInt32(fontSize.Width);
            int intHeight = Convert.ToInt32(fontSize.Height);
            g.Dispose();
            img.Dispose();