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 { /// /// 用于生成条码 /// 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; /// /// 获取或设置宽度 /// [DefaultValue(300)] public int Width { get { return width; } set { width = value; } } /// /// 获取或设置高度 /// [DefaultValue(150)] public int Height { get { return height; } set { height = value; } } /// /// 获取或设置对齐方式 /// [DefaultValue("Center")] public AlignmentPosition Alignment { get { return alignment; } set { alignment = value; } } /// /// 是否绘制条码文本 /// [DefaultValue(false)] public bool IncludeLabel { get { return includeLabel; } set { includeLabel = value; } } /// /// 绘制条码文本字体 /// public Font LabelFont { get { return labelFont; } set { labelFont = value; } } /// /// 条形码颜色 /// 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; } /// /// 生成条码图片 /// /// 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; } } /// /// 获取偏移量 /// /// 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; } } } }