GenerateBarCode.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.ComponentModel;
  7. using System.Drawing.Drawing2D;
  8. namespace Bowin.Common.BarCode
  9. {
  10. /// <summary>
  11. /// 用于生成条码
  12. /// </summary>
  13. public class GenerateBarCode
  14. {
  15. private int width = 300;
  16. private int height = 150;
  17. private bool includeLabel = false;
  18. private Font labelFont = new Font("Microsoft Sans Serif", 7, FontStyle.Bold);
  19. private BarCodeBase barCode;
  20. private string rawData;
  21. private string encodeData;
  22. private AlignmentPosition alignment = AlignmentPosition.Center;
  23. private Color codeColor = Color.Black;
  24. private readonly Color ForeColor = Color.Black;
  25. private readonly Color BackColor = Color.White;
  26. /// <summary>
  27. /// 获取或设置宽度
  28. /// </summary>
  29. [DefaultValue(300)]
  30. public int Width
  31. {
  32. get
  33. {
  34. return width;
  35. }
  36. set
  37. {
  38. width = value;
  39. }
  40. }
  41. /// <summary>
  42. /// 获取或设置高度
  43. /// </summary>
  44. [DefaultValue(150)]
  45. public int Height
  46. {
  47. get
  48. {
  49. return height;
  50. }
  51. set
  52. {
  53. height = value;
  54. }
  55. }
  56. /// <summary>
  57. /// 获取或设置对齐方式
  58. /// </summary>
  59. [DefaultValue("Center")]
  60. public AlignmentPosition Alignment
  61. {
  62. get
  63. {
  64. return alignment;
  65. }
  66. set
  67. {
  68. alignment = value;
  69. }
  70. }
  71. /// <summary>
  72. /// 是否绘制条码文本
  73. /// </summary>
  74. [DefaultValue(false)]
  75. public bool IncludeLabel
  76. {
  77. get
  78. {
  79. return includeLabel;
  80. }
  81. set
  82. {
  83. includeLabel = value;
  84. }
  85. }
  86. /// <summary>
  87. /// 绘制条码文本字体
  88. /// </summary>
  89. public Font LabelFont
  90. {
  91. get
  92. {
  93. return labelFont;
  94. }
  95. set
  96. {
  97. labelFont = value;
  98. }
  99. }
  100. /// <summary>
  101. /// 条形码颜色
  102. /// </summary>
  103. public Color CodeColor
  104. {
  105. get
  106. {
  107. return codeColor;
  108. }
  109. set
  110. {
  111. codeColor = value;
  112. }
  113. }
  114. public GenerateBarCode(int width, int height, string rawData)
  115. : this(width, height, rawData, BarCodeType.Code128)
  116. {
  117. }
  118. public GenerateBarCode(int width, int height, string rawData, BarCodeType type)
  119. {
  120. this.width = width;
  121. this.height = height;
  122. this.rawData = rawData;
  123. barCode = BarCodeFactory.CreateBarCard(type, rawData);
  124. encodeData = barCode.EncodeValue;
  125. }
  126. /// <summary>
  127. /// 生成条码图片
  128. /// </summary>
  129. /// <returns></returns>
  130. public Bitmap GetImage()
  131. {
  132. Bitmap b = new Bitmap(Width, Height);
  133. int barWidth = Width / encodeData.Length;
  134. if (barWidth <= 0)
  135. {
  136. throw new Exception("宽度不够");
  137. }
  138. //获取偏移量
  139. int shiftAdjustment = 0;
  140. GetshiftAdjustment(ref shiftAdjustment);
  141. //位置
  142. int pos = 0;
  143. using (Graphics g = Graphics.FromImage(b))
  144. {
  145. //清除背景色
  146. g.Clear(BackColor);
  147. using (Pen forePen = new Pen(CodeColor, barWidth))
  148. {
  149. while (pos < encodeData.Length)
  150. {
  151. if (encodeData[pos] == '1')
  152. {
  153. g.DrawLine(forePen, new Point(pos * barWidth + shiftAdjustment + (int)(barWidth * 0.5), 0),
  154. new Point(pos * barWidth + shiftAdjustment + (int)(barWidth * 0.5), Height));
  155. }
  156. pos++;
  157. }
  158. }
  159. }
  160. if (IncludeLabel)
  161. {
  162. DrawLabel((Image)b);
  163. }
  164. return b;
  165. }
  166. private Image DrawLabel(Image image)
  167. {
  168. try
  169. {
  170. Font font = this.LabelFont;
  171. using (Graphics g = Graphics.FromImage(image))
  172. {
  173. g.DrawImage(image, 0f, 0f);
  174. g.SmoothingMode = SmoothingMode.HighQuality;
  175. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  176. g.PixelOffsetMode = PixelOffsetMode.HighQuality;
  177. g.CompositingQuality = CompositingQuality.HighQuality;
  178. StringFormat f = new StringFormat();
  179. f.Alignment = StringAlignment.Center;
  180. f.LineAlignment = StringAlignment.Near;
  181. int LabelX = image.Width / 2;
  182. int LabelY = image.Height - (font.Height);
  183. g.FillRectangle(new SolidBrush(this.BackColor), new RectangleF((float)0, (float)LabelY, (float)image.Width, (float)font.Height));
  184. g.DrawString(this.rawData, font, new SolidBrush(this.ForeColor), new RectangleF((float)0, (float)LabelY, (float)image.Width, (float)font.Height), f);
  185. g.Save();
  186. }
  187. return image;
  188. }
  189. catch (Exception)
  190. {
  191. throw;
  192. }
  193. }
  194. /// <summary>
  195. /// 获取偏移量
  196. /// </summary>
  197. /// <param name="shiftNum"></param>
  198. private void GetshiftAdjustment(ref int shiftNum)
  199. {
  200. switch (Alignment)
  201. {
  202. case AlignmentPosition.Center:
  203. shiftNum = (Width % encodeData.Length) / 2;
  204. break;
  205. case AlignmentPosition.Left:
  206. shiftNum = 0;
  207. break;
  208. case AlignmentPosition.Right:
  209. shiftNum = Width % encodeData.Length;
  210. break;
  211. default:
  212. shiftNum = (Width % encodeData.Length) / 2;
  213. break;
  214. }
  215. }
  216. }
  217. }