123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Drawing;
- using System.ComponentModel;
- using System.Drawing.Drawing2D;
- namespace Bowin.Common.BarCode
- {
- /// <summary>
- /// 用于生成条码
- /// </summary>
- public class GenerateBarCode
- {
- private int width = 300;
- private int height = 150;
- private bool includeLabel = false;
- private Font labelFont = new Font("Microsoft Sans Serif", 7, FontStyle.Bold);
- private BarCodeBase barCode;
- private string rawData;
- private string encodeData;
- private AlignmentPosition alignment = AlignmentPosition.Center;
- private Color codeColor = Color.Black;
- private readonly Color ForeColor = Color.Black;
- private readonly Color BackColor = Color.White;
- /// <summary>
- /// 获取或设置宽度
- /// </summary>
- [DefaultValue(300)]
- public int Width
- {
- get
- {
- return width;
- }
- set
- {
- width = value;
- }
- }
- /// <summary>
- /// 获取或设置高度
- /// </summary>
- [DefaultValue(150)]
- public int Height
- {
- get
- {
- return height;
- }
- set
- {
- height = value;
- }
- }
- /// <summary>
- /// 获取或设置对齐方式
- /// </summary>
- [DefaultValue("Center")]
- public AlignmentPosition Alignment
- {
- get
- {
- return alignment;
- }
- set
- {
- alignment = value;
- }
- }
- /// <summary>
- /// 是否绘制条码文本
- /// </summary>
- [DefaultValue(false)]
- public bool IncludeLabel
- {
- get
- {
- return includeLabel;
- }
- set
- {
- includeLabel = value;
- }
- }
- /// <summary>
- /// 绘制条码文本字体
- /// </summary>
- public Font LabelFont
- {
- get
- {
- return labelFont;
- }
- set
- {
- labelFont = value;
- }
- }
- /// <summary>
- /// 条形码颜色
- /// </summary>
- public Color CodeColor
- {
- get
- {
- return codeColor;
- }
- set
- {
- codeColor = value;
- }
- }
- public GenerateBarCode(int width, int height, string rawData)
- : this(width, height, rawData, BarCodeType.Code128)
- {
- }
- public GenerateBarCode(int width, int height, string rawData, BarCodeType type)
- {
- this.width = width;
- this.height = height;
- this.rawData = rawData;
- barCode = BarCodeFactory.CreateBarCard(type, rawData);
- encodeData = barCode.EncodeValue;
- }
- /// <summary>
- /// 生成条码图片
- /// </summary>
- /// <returns></returns>
- public Bitmap GetImage()
- {
- Bitmap b = new Bitmap(Width, Height);
- int barWidth = Width / encodeData.Length;
- if (barWidth <= 0)
- {
- throw new Exception("宽度不够");
- }
- //获取偏移量
- int shiftAdjustment = 0;
- GetshiftAdjustment(ref shiftAdjustment);
- //位置
- int pos = 0;
- using (Graphics g = Graphics.FromImage(b))
- {
- //清除背景色
- g.Clear(BackColor);
- using (Pen forePen = new Pen(CodeColor, barWidth))
- {
- while (pos < encodeData.Length)
- {
- if (encodeData[pos] == '1')
- {
- g.DrawLine(forePen, new Point(pos * barWidth + shiftAdjustment + (int)(barWidth * 0.5), 0),
- new Point(pos * barWidth + shiftAdjustment + (int)(barWidth * 0.5), Height));
- }
- pos++;
- }
- }
- }
- if (IncludeLabel)
- {
- DrawLabel((Image)b);
- }
- return b;
- }
- private Image DrawLabel(Image image)
- {
- try
- {
- Font font = this.LabelFont;
- using (Graphics g = Graphics.FromImage(image))
- {
- g.DrawImage(image, 0f, 0f);
- g.SmoothingMode = SmoothingMode.HighQuality;
- g.InterpolationMode = InterpolationMode.HighQualityBicubic;
- g.PixelOffsetMode = PixelOffsetMode.HighQuality;
- g.CompositingQuality = CompositingQuality.HighQuality;
- StringFormat f = new StringFormat();
- f.Alignment = StringAlignment.Center;
- f.LineAlignment = StringAlignment.Near;
- int LabelX = image.Width / 2;
- int LabelY = image.Height - (font.Height);
- g.FillRectangle(new SolidBrush(this.BackColor), new RectangleF((float)0, (float)LabelY, (float)image.Width, (float)font.Height));
- g.DrawString(this.rawData, font, new SolidBrush(this.ForeColor), new RectangleF((float)0, (float)LabelY, (float)image.Width, (float)font.Height), f);
- g.Save();
- }
- return image;
- }
- catch (Exception)
- {
- throw;
- }
- }
- /// <summary>
- /// 获取偏移量
- /// </summary>
- /// <param name="shiftNum"></param>
- private void GetshiftAdjustment(ref int shiftNum)
- {
- switch (Alignment)
- {
- case AlignmentPosition.Center:
- shiftNum = (Width % encodeData.Length) / 2;
- break;
- case AlignmentPosition.Left:
- shiftNum = 0;
- break;
- case AlignmentPosition.Right:
- shiftNum = Width % encodeData.Length;
- break;
- default:
- shiftNum = (Width % encodeData.Length) / 2;
- break;
- }
- }
- }
- }
|