迷你5207专属论坛

注册

 

发新话题 回复该主题

[DotNet] C#实现Code128条形码 [复制链接]

发表者
先把代码贴出,可以拿去用了。。
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4. using System.IO;

  5. namespace TestCode128
  6. {
  7.     public sealed class Code128
  8.     {
  9.         #region private variables
  10.         /// <summary>
  11.         /// The Space Between each of Title, BarCode, BarCodeString
  12.         /// </summary>
  13.         private const int SPACE_HEIGHT = 3;
  14.         SizeF _sizeLabel = SizeF.Empty;
  15.         SizeF _sizeBarCodeValue = SizeF.Empty;
  16.         SizeF _sizeBarCodeString = SizeF.Empty;
  17.         SizeF _sizeAdditionalInfo = SizeF.Empty;

  18.         #endregion

  19.         #region Label
  20.         private string _label = null;
  21.         private Font _labelFont = null;
  22.         /// <summary>
  23.         /// BarCode Title (条码标签)
  24.         /// </summary>
  25.         public string Label
  26.         {
  27.             set { _label = value; }
  28.         }
  29.         /// <summary>
  30.         /// BarCode Title Font (条码标签使用的字体)
  31.         /// </summary>
  32.         public Font LabelFont
  33.         {
  34.             get
  35.             {
  36.                 if (_labelFont == null)
  37.                     return new Font("Arial", 10);
  38.                 return _labelFont;
  39.             }
  40.             set { _labelFont = value; }
  41.         }
  42.         #endregion

  43.         private string _additionalInfo = null;
  44.         private Font _addtionalInfoFont = null;
  45.         /// <summary>
  46.         /// Additional Info Font (附加信息字体)
  47.         /// </summary>
  48.         public Font AdditionalInfoFont
  49.         {
  50.             set { _addtionalInfoFont = value; }
  51.             get
  52.             {
  53.                 if (_addtionalInfoFont == null) return new Font("Arial", 10);
  54.                 return _addtionalInfoFont;
  55.             }
  56.         }
  57.         /// <summary>
  58.         /// Additional Info Content, if "ShowBarCodeValue" is true, the info is unvisible
  59.         /// 附加信息,如果设置ShowBarCodeValue为true,则此项不显示
  60.         /// </summary>
  61.         public string AdditionalInfo
  62.         {
  63.             set { _additionalInfo = value; }
  64.         }

  65.         #region BarCode Value and Font
  66.         private string _barCodeValue = null;
  67.         /// <summary>
  68.         /// BarCode Value (条码值)
  69.         /// </summary>
  70.         public string BarCodeValue
  71.         {
  72.             get
  73.             {
  74.                 if (string.IsNullOrEmpty(_barCodeValue))
  75.                     throw new NullReferenceException("The BarCodeValue has not been set yet!");
  76.                 return _barCodeValue;
  77.             }
  78.             set { _barCodeValue = value.StartsWith("*") && value.EndsWith("*") ? value : "*" + value + "*"; }
  79.         }

  80.         private bool _showBarCodeValue = false;
  81.         /// <summary>
  82.         /// whether to show the original string of barcode value bellow the barcode
  83.         /// 是否在条码下方显示条码值,默认为false
  84.         /// </summary>
  85.         public bool ShowBarCodeValue
  86.         {
  87.             set { _showBarCodeValue = value; }
  88.         }

  89.         private Font _barCodeValueFont = null;
  90.         /// <summary>
  91.         /// the font of the codestring to show
  92.         /// 条码下方显示的条码值的字体
  93.         /// </summary>
  94.         public Font BarCodeValueFont
  95.         {
  96.             get
  97.             {
  98.                 if (_barCodeValueFont == null)
  99.                     return new Font("Arial", 10);
  100.                 return _barCodeValueFont;
  101.             }
  102.             set { _barCodeValueFont = value; }
  103.         }

  104.         private int _barCodeFontSize = 50;
  105.         /// <summary>
  106.         /// the font size of the barcode value to draw
  107.         /// 条码绘制的大小,默认50
  108.         /// </summary>
  109.         public int BarCodeFontSize
  110.         {
  111.             set { _barCodeFontSize = value; }
  112.         }
  113.         #endregion

  114.         #region generate the barcode image
  115.         private Bitmap BlankBackImage
  116.         {
  117.             get
  118.             {
  119.                 int barCodeWidth = 0, barCodeHeight = 0;
  120.                 using (Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format32bppArgb))
  121.                 {
  122.                     using (Graphics g = Graphics.FromImage(bmp))
  123.                     {
  124.                         if (!string.IsNullOrEmpty(_label))
  125.                         {
  126.                             _sizeLabel = g.MeasureString(_label, LabelFont);
  127.                             barCodeWidth = (int)_sizeLabel.Width;
  128.                             barCodeHeight = (int)_sizeLabel.Height + SPACE_HEIGHT;
  129.                         }

  130.                         _sizeBarCodeValue = g.MeasureString(BarCodeValue, new Font("Code 128", _barCodeFontSize));
  131.                         barCodeWidth = Math.Max(barCodeWidth, (int)_sizeBarCodeValue.Width);
  132.                         barCodeHeight += (int)_sizeBarCodeValue.Height;

  133.                         if (_showBarCodeValue)
  134.                         {
  135.                             _sizeBarCodeString = g.MeasureString(_barCodeValue, BarCodeValueFont);
  136.                             barCodeWidth = Math.Max(barCodeWidth, (int)_sizeBarCodeString.Width);
  137.                             barCodeHeight += (int)_sizeBarCodeString.Height + SPACE_HEIGHT;
  138.                         }
  139.                         else
  140.                         {
  141.                             if (!string.IsNullOrEmpty(_additionalInfo))
  142.                             {
  143.                                 _sizeAdditionalInfo = g.MeasureString(_additionalInfo, AdditionalInfoFont);
  144.                                 barCodeWidth = Math.Max(barCodeWidth, (int)_sizeAdditionalInfo.Width);
  145.                                 barCodeHeight += (int)_sizeAdditionalInfo.Height + SPACE_HEIGHT;
  146.                             }
  147.                         }
  148.                     }
  149.                 }

  150.                 return new Bitmap(barCodeWidth, barCodeHeight, PixelFormat.Format32bppArgb);
  151.             }
  152.         }

  153.         /// <summary>
  154.         /// Draw the barcode value to the blank back image and output it to the browser
  155.         /// 绘制WebForm形式的条码
  156.         /// </summary>
  157.         /// <param name="ms">Recommand the "Response.OutputStream" 使用 Response.OutputStream</param>
  158.         /// <param name="imageFormat">The Image format to the Browser 输出到浏览器到图片格式,建议GIF</param>
  159.         public void CreateWebForm(Stream ms, ImageFormat imageFormat)
  160.         {
  161.             int barCodeWidth, barCodeHeight;
  162.             using (Bitmap bmp = this.BlankBackImage)
  163.             {
  164.                 barCodeHeight = bmp.Height;
  165.                 barCodeWidth = bmp.Width;
  166.                 using (Graphics g = Graphics.FromImage(bmp))
  167.                 {
  168.                     g.Clear(Color.White);
  169.                     g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
  170.                     g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  171.                     int vPos = 0;
  172.                     ////Draw Label String
  173.                     if (!string.IsNullOrEmpty(_label))
  174.                     {
  175.                         g.DrawString(_label, LabelFont, new SolidBrush(Color.Black),
  176.                             XCenter((int)_sizeLabel.Width, barCodeWidth), vPos);
  177.                         vPos += (int)_sizeLabel.Height + SPACE_HEIGHT;
  178.                     }
  179.                     else { vPos = SPACE_HEIGHT; }

  180.                     ////Draw The Bar Value
  181.                     g.DrawString(_barCodeValue, new Font("Code 128", _barCodeFontSize), new SolidBrush(Color.Black),
  182.                         XCenter((int)_sizeBarCodeValue.Width, barCodeWidth), vPos);
  183.                     ////Draw the BarValue String
  184.                     if (_showBarCodeValue)
  185.                     {
  186.                         g.DrawString(_barCodeValue, BarCodeValueFont, new SolidBrush(Color.Black),
  187.                             XCenter((int)_sizeBarCodeString.Width, barCodeWidth),
  188.                             vPos + (int)_sizeBarCodeValue.Height);
  189.                     }
  190.                     else
  191.                     {
  192.                         if (!string.IsNullOrEmpty(_additionalInfo))
  193.                         {
  194.                             g.DrawString(_additionalInfo, AdditionalInfoFont, new SolidBrush(Color.Black),
  195.                             XCenter((int)_sizeAdditionalInfo.Width, barCodeWidth),
  196.                             vPos + (int)_sizeBarCodeValue.Height);
  197.                         }
  198.                     }
  199.                 }

  200.                 bmp.Save(ms, imageFormat);
  201.             }
  202.         }

  203.         /// <summary>
  204.         /// 生成winform格式的条码
  205.         /// </summary>
  206.         /// <param name="imageFormat">图片格式,建议GIF</param>
  207.         /// <returns>Stream类型</returns>
  208.         public Stream CreateWinForm(ImageFormat imageFormat)
  209.         {
  210.             int barCodeWidth, barCodeHeight;
  211.             using (Bitmap bmp = this.BlankBackImage)
  212.             {
  213.                 barCodeHeight = bmp.Height;
  214.                 barCodeWidth = bmp.Width;
  215.                 using (Graphics g = Graphics.FromImage(bmp))
  216.                 {
  217.                     g.Clear(Color.White);
  218.                     g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
  219.                     g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  220.                     int vPos = 0;
  221.                     ////Draw Label String
  222.                     if (!string.IsNullOrEmpty(_label))
  223.                     {
  224.                         g.DrawString(_label, LabelFont, new SolidBrush(Color.Black),
  225.                             XCenter((int)_sizeLabel.Width, barCodeWidth), vPos);
  226.                         vPos += (int)_sizeLabel.Height + SPACE_HEIGHT;
  227.                     }
  228.                     else { vPos = SPACE_HEIGHT; }

  229.                     ////Draw The Bar Value
  230.                     g.DrawString(_barCodeValue, new Font("Code 128", _barCodeFontSize), new SolidBrush(Color.Black),
  231.                         XCenter((int)_sizeBarCodeValue.Width, barCodeWidth), vPos);
  232.                     ////Draw the BarValue String
  233.                     if (_showBarCodeValue)
  234.                     {
  235.                         g.DrawString(_barCodeValue, BarCodeValueFont, new SolidBrush(Color.Black),
  236.                             XCenter((int)_sizeBarCodeString.Width, barCodeWidth),
  237.                             vPos + (int)_sizeBarCodeValue.Height);
  238.                     }
  239.                     else
  240.                     {
  241.                         if (!string.IsNullOrEmpty(_additionalInfo))
  242.                         {
  243.                             g.DrawString(_additionalInfo, AdditionalInfoFont, new SolidBrush(Color.Black),
  244.                             XCenter((int)_sizeAdditionalInfo.Width, barCodeWidth),
  245.                             vPos + (int)_sizeBarCodeValue.Height);
  246.                         }
  247.                     }
  248.                 }

  249.                 Stream ms = new MemoryStream();
  250.                 bmp.Save(ms, imageFormat);
  251.                 return ms;
  252.             }
  253.         }
  254.         #endregion

  255.         private static int XCenter(int subWidth, int globalWidth)
  256.         {
  257.             return (globalWidth - subWidth) / 2;
  258.         }

  259.     }
  260. }
复制代码
上面code128的源代码,下面是调用的代码示例:
  1.         private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             Code128 code128 = new Code128();
  4.             code128.BarCodeValue = "*211232231131221213131222212222221224121213121211321132331112";
  5.             code128.BarCodeFontSize = 30;
  6.             code128.ShowBarCodeValue = true;

  7.             Stream ms = new MemoryStream();
  8.             ms = code128.CreateWinForm(System.Drawing.Imaging.ImageFormat.Gif);
  9.             pictureBox1.Image = Image.FromStream(ms);

  10.             code128 = null;
  11.         }
复制代码
代码就不解释了,另外要注意一点如果直接执行上面的代码是不能打印成功的,输出的是一串文字图片,这是因为没有安装code128字体的原因,安装字体即可。
下面是字体包下载:
code_128.zip (, 下载次数:65)
分享 转发
相信与不相信都是矛盾的.  5207宣!欢迎您来到点滴论坛
TOP
发新话题 回复该主题