Code39是条形码的一种。由于编制简单、能够对任意长度的数据进行编码、支持设备广泛等特性而被广泛采用。
-
能够对任意长度的数据进行编码。其局限在于印刷品的长度和条码阅读器的识别范围。
-
支持设备广泛。目前几乎所有的条形码阅读设备都能阅读Code39码,打印机也是同样情况。
-
编制简单。简单的开发技术就能快速生成相应的编码图像。
-
一般Code39码由5条线和分开它们的4条缝隙共9个元素构成。线和缝隙有宽窄之分,而且无论线还是缝隙仅有3个比其他的元素要宽一定比例。39码因此得名
Code39条形码规则
1、 每五条线表示一个字符;
2、 粗线表示1,细线表示0;
3、 线条间的间隙宽的表示1,窄的表示0;
4、 五条线加上它们之间的四条间隙就是九位二进制编码,而且这九位中必定有三位是1,所以称为39码;
5、 条形码的首尾各一个 * 标识开始和结束。
%26nbsp;
Code 39只接受如下43个有效输入字符:
1、26个大写字母(A - Z),
2、十个数字(0 - 9),
3、连接号(-),句号(.),空格,美圆符号($),斜扛(/),加号(+)以及百分号(%)。
%26nbsp; %26nbsp; 4、其余的输入将被忽略。
5、code39通常情况下不需要校验码。但是对於精确度要求高的应用,需要在code39条形码後面增加一个校验码。
1、26个大写字母(A - Z),
2、十个数字(0 - 9),
3、连接号(-),句号(.),空格,美圆符号($),斜扛(/),加号(+)以及百分号(%)。
%26nbsp; %26nbsp; 4、其余的输入将被忽略。
5、code39通常情况下不需要校验码。但是对於精确度要求高的应用,需要在code39条形码後面增加一个校验码。
%26nbsp;
条形码的生成网上也有很多库文件,做的特别好的都是商业授权。一维码二维码在.net开源项目中的生成读取比较知名的有:Zxing.net(http://zxingnet.codeplex.com/)、BarcodeLib等
但有些时候开源软件所实现的并不满足当下的需求,所以还要做一些其他尝试。效果如下图:
%26nbsp;
代码分享:
1 /// %26lt;summary%26gt; 2 /// Code39一维码生成类 3 /// %26lt;/summary%26gt; 4 public class CSharpCode39 5 { 6 7 #region 数据码 8 private byte[,] c39_bp = new byte[,] { 9 { 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x30, 0x31, 0x30, 0x30 }, { 0x31, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31 }, { 0x32, 0x30, 0x30, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31 }, { 0x33, 0x31, 0x30, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30 }, { 0x34, 0x30, 0x30, 0x30, 0x31, 0x31, 0x30, 0x30, 0x30, 0x31 }, { 0x35, 0x31, 0x30, 0x30, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30 }, { 0x36, 0x30, 0x30, 0x31, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30 }, { 0x37, 0x30, 0x30, 0x30, 0x31, 0x30, 0x30, 0x31, 0x30, 0x31 }, { 0x38, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30 }, { 0x39, 0x30, 0x30, 0x31, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30 }, { 0x41, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31, 0x30, 0x30, 0x31 }, { 0x42, 0x30, 0x30, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30, 0x31 }, { 0x43, 0x31, 0x30, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30 }, { 0x44, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x30, 0x30, 0x31 }, { 0x45, 0x31, 0x30, 0x30, 0x30, 0x31, 0x31, 0x30, 0x30, 0x30 }, { 70, 0x30, 0x30, 0x31, 0x30, 0x31, 0x31, 0x30, 0x30, 0x30 }, 10 { 0x47, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x30, 0x31 }, { 0x48, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x30, 0x30 }, { 0x49, 0x30, 0x30, 0x31, 0x30, 0x30, 0x31, 0x31, 0x30, 0x30 }, { 0x4a, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x31, 0x30, 0x30 }, { 0x4b, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31 }, { 0x4c, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31 }, { 0x4d, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31, 0x30 }, { 0x4e, 0x30, 0x30, 0x30, 0x30, 0x31, 0x30, 0x30, 0x31, 0x31 }, { 0x4f, 0x31, 0x30, 0x30, 0x30, 0x31, 0x30, 0x30, 0x31, 0x30 }, { 80, 0x30, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x31, 0x30 }, { 0x51, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x31 }, { 0x52, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x30 }, { 0x53, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x31, 0x31, 0x30 }, { 0x54, 0x30, 0x30, 0x30, 0x30, 0x31, 0x30, 0x31, 0x31, 0x30 }, { 0x55, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31 }, { 0x56, 0x30, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31 }, 11 { 0x57, 0x31, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 }, { 0x58, 0x30, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x31 }, { 0x59, 0x31, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30 }, { 90, 0x30, 0x31, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30 }, { 0x2d, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31, 0x30, 0x31 }, { 0x2e, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31, 0x30, 0x30 }, { 0x20, 0x30, 0x31, 0x31, 0x30, 0x30, 0x30, 0x31, 0x30, 0x30 }, { 0x2a, 0x30, 0x31, 0x30, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30 }, { 0x24, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30 }, { 0x2f, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x31, 0x30 }, { 0x2b, 0x30, 0x31, 0x30, 0x30, 0x30, 0x31, 0x30, 0x31, 0x30 }, { 0x25, 0x30, 0x30, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30 } 12 }; 13 //code39合法字符集 [0-9A-Z+-*/%. ] 共43个 14 private byte[] c39_cw = new byte[] { 15 0x30, 0x31, 50, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 70, 16 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 80, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 17 0x57, 0x58, 0x59, 90, 0x2d, 0x2e, 0x20, 0x24, 0x2f, 0x2b, 0x25 18 }; 19 private byte[,] c39_ex = new byte[,] { 20 { 1, 0x24, 0x41 }, { 2, 0x24, 0x42 }, { 3, 0x24, 0x43 }, { 4, 0x24, 0x44 }, { 5, 0x24, 0x45 }, { 6, 0x24, 70 }, { 7, 0x24, 0x47 }, { 8, 0x24, 0x48 }, { 9, 0x24, 0x49 }, { 10, 0x24, 0x4a }, { 11, 0x24, 0x4b }, { 12, 0x24, 0x4c }, { 13, 0x24, 0x4d }, { 14, 0x24, 0x4e }, { 15, 0x24, 0x4f }, { 0x10, 0x24, 80 }, 21 { 0x11, 0x24, 0x51 }, { 0x12, 0x24, 0x52 }, { 0x13, 0x24, 0x53 }, { 20, 0x24, 0x54 }, { 0x15, 0x24, 0x55 }, { 0x16, 0x24, 0x56 }, { 0x17, 0x24, 0x57 }, { 0x18, 0x24, 0x58 }, { 0x19, 0x24, 0x59 }, { 0x1a, 0x24, 90 }, { 0x1b, 0x25, 0x41 }, { 0x1c, 0x25, 0x42 }, { 0x1d, 0x25, 0x43 }, { 30, 0x25, 0x44 }, { 0x1f, 0x25, 0x45 }, { 0x3b, 0x25, 70 }, 22 { 60, 0x25, 0x47 }, { 0x3d, 0x25, 0x48 }, { 0x3e, 0x25, 0x49 }, { 0x3f, 0x25, 0x4a }, { 0x5b, 0x25, 0x4b }, { 0x5c, 0x25, 0x4c }, { 0x5d, 0x25, 0x4d }, { 0x5e, 0x25, 0x4e }, { 0x5f, 0x25, 0x4f }, { 0x7b, 0x25, 80 }, { 0x7c, 0x25, 0x51 }, { 0x7d, 0x25, 0x52 }, { 0x7e, 0x25, 0x53 }, { 0x7f, 0x25, 0x54 }, { 0, 0x25, 0x55 }, { 0x40, 0x25, 0x56 }, 23 { 0x60, 0x25, 0x57 }, { 0x21, 0x2f, 0x41 }, { 0x22, 0x2f, 0x42 }, { 0x23, 0x2f, 0x43 }, { 0x26, 0x2f, 70 }, { 0x27, 0x2f, 0x47 }, { 40, 0x2f, 0x48 }, { 0x29, 0x2f, 0x49 }, { 0x2a, 0x2f, 0x4a }, { 0x2c, 0x2f, 0x4c }, { 0x3a, 0x2f, 90 }, { 0x61, 0x2b, 0x41 }, { 0x62, 0x2b, 0x42 }, { 0x63, 0x2b, 0x43 }, { 100, 0x2b, 0x44 }, { 0x65, 0x2b, 0x45 }, 24 { 0x66, 0x2b, 70 }, { 0x67, 0x2b, 0x47 }, { 0x68, 0x2b, 0x48 }, { 0x69, 0x2b, 0x49 }, { 0x6a, 0x2b, 0x4a }, { 0x6b, 0x2b, 0x4b }, { 0x6c, 0x2b, 0x4c }, { 0x6d, 0x2b, 0x4d }, { 110, 0x2b, 0x4e }, { 0x6f, 0x2b, 0x4f }, { 0x70, 0x2b, 80 }, { 0x71, 0x2b, 0x51 }, { 0x72, 0x2b, 0x52 }, { 0x73, 0x2b, 0x53 }, { 0x74, 0x2b, 0x54 }, { 0x75, 0x2b, 0x55 }, 25 { 0x76, 0x2b, 0x56 }, { 0x77, 0x2b, 0x57 }, { 120, 0x2b, 0x58 }, { 0x79, 0x2b, 0x59 }, { 0x7a, 0x2b, 90 } 26 }; 27 #endregion 28 #region 字段和属性 29 30 31 32 private bool _checksum; 33 private string _dataToEncode; 34 private bool _humanReadable; 35 private string _humanReadableFont; 36 private float _humanReadableSize; 37 private float _marginX; 38 private float _marginY; 39 private float _moduleHeight; 40 private float _moduleWidth; 41 private float _ratio; 42 private float _reduction; 43 private Color _codeBarColor = Color.Black; 44 private bool _isDisplayCheckCode; 45 private string _checkData; 46 private bool _isDisplayStartStopSign; 47 /// %26lt;summary%26gt; 48 /// 是否检查效验 49 /// %26lt;/summary%26gt; 50 public bool Checksum 51 { 52 get 53 { 54 return _checksum; 55 } 56 set 57 { 58 _checksum = value; 59 } 60 } 61 62 63 /// %26lt;summary%26gt; 64 /// 要进行编码的数据 65 /// %26lt;/summary%26gt; 66 public string DataToEncode 67 { 68 get 69 { 70 return _dataToEncode; 71 } 72 set 73 { 74 _dataToEncode = value; 75 } 76 } 77 78 79 80 /// %26lt;summary%26gt; 81 /// 是否显示文本内容 82 /// %26lt;/summary%26gt; 83 public bool HumanReadable 84 { 85 get 86 { 87 return _humanReadable; 88 } 89 set 90 { 91 _humanReadable = value; 92 } 93 } 94 /// %26lt;summary%26gt; 95 /// 用于显示文本内容的字体 96 /// %26lt;/summary%26gt; 97 public string HumanReadableFont 98 { 99 get 100 { 101 return _humanReadableFont; 102 } 103 set 104 { 105 _humanReadableFont = value; 106 } 107 } 108 /// %26lt;summary%26gt; 109 /// 用于显示文本内容文字的代大小 110 /// %26lt;/summary%26gt; 111 public float HumanReadableSize 112 { 113 get 114 { 115 return _humanReadableSize; 116 } 117 set 118 { 119 _humanReadableSize = value; 120 } 121 } 122 123 /// %26lt;summary%26gt; 124 /// 水平方向边距 125 /// 水平方向建议尽量留白 126 /// 如果没有留白同时模块宽度较小可能会造成无法识别 127 /// %26lt;/summary%26gt; 128 public float MarginX 129 { 130 get 131 { 132 return _marginX; 133 } 134 set 135 { 136 _marginX = value; 137 } 138 } 139 /// %26lt;summary%26gt; 140 /// 垂直方向边距 141 /// %26lt;/summary%26gt; 142 public float MarginY 143 { 144 get 145 { 146 return _marginY; 147 } 148 set 149 { 150 _marginY = value; 151 } 152 } 153 154 /// %26lt;summary%26gt; 155 /// 模块高度 156 /// %26lt;/summary%26gt; 157 public float ModuleHeight 158 { 159 get 160 { 161 return _moduleHeight; 162 } 163 set 164 { 165 _moduleHeight = value; 166 } 167 } 168 169 /// %26lt;summary%26gt; 170 /// 模块宽度 171 /// 模块宽度不应低于0.2646f 172 /// 模块宽度过低会造成数据丢失因而读不出数据或者会误读 173 /// %26lt;/summary%26gt; 174 public float ModuleWidth 175 { 176 get 177 { 178 return _moduleWidth; 179 } 180 set 181 { 182 _moduleWidth = value; 183 } 184 } 185 186 187 /// %26lt;summary%26gt; 188 /// 放大比率 189 /// %26lt;/summary%26gt; 190 public float Ratio 191 { 192 get 193 { 194 return _ratio; 195 } 196 set 197 { 198 _ratio = value; 199 } 200 } 201 /// %26lt;summary%26gt; 202 /// 缩小 203 /// %26lt;/summary%26gt; 204 public float Reduction 205 { 206 get 207 { 208 return _reduction; 209 } 210 set 211 { 212 _reduction = value; 213 } 214 } 215 /// %26lt;summary%26gt; 216 /// 设置条形码的颜色 217 /// %26lt;/summary%26gt; 218 public Color CodeBarColor 219 { 220 get 221 { 222 return _codeBarColor; 223 } 224 set 225 { 226 _codeBarColor = value; 227 } 228 } 229 230 /// %26lt;summary%26gt; 231 /// 是否显示效验码 232 /// 此属性要在Checksum属性为true的情况下有用 233 /// %26lt;/summary%26gt; 234 public bool IsDisplayCheckCode 235 { 236 get { return this._isDisplayCheckCode; } 237 set { this._isDisplayCheckCode = value; } 238 } 239 /// %26lt;summary%26gt; 240 /// 供人识别是否显示起止符 241 /// %26lt;/summary%26gt; 242 public bool IsDisplayStartStopSign 243 { 244 get { return this._isDisplayStartStopSign; } 245 set { this._isDisplayStartStopSign = value; } 246 } 247 #endregion 248 /// %26lt;summary%26gt; 249 /// 默认构造函数 250 /// 初始化 251 /// %26lt;/summary%26gt; 252 public CSharpCode39() 253 { 254 this.initData(); 255 256 } 257 public CSharpCode39(string dataToEncode) 258 { 259 this.initData(); 260 this._dataToEncode = dataToEncode; 261 } 262 /// %26lt;summary%26gt; 263 /// 默认构造函数 264 /// 初始化数据 265 /// %26lt;/summary%26gt; 266 private void initData() 267 { 268 this._humanReadableFont = "Arial"; 269 this._humanReadableSize = 10f; 270 this._codeBarColor = Color.Black; 271 this._moduleHeight = 15f; 272 this._moduleWidth = 0.35f; 273 this._ratio = 3f; 274 this._marginX =2; 275 this._marginY =2; 276 this._checksum = true; 277 this._isDisplayCheckCode = false; 278 this._isDisplayStartStopSign = false; 279 } 280 private char[] _bitpattern_c39(string rawdata, ref int finalLength) 281 { 282 //0x27 39 283 //0x50 80 284 if ((rawdata.Length == 0) || (rawdata.Length %26gt; 0x50 /*0x27*/)) 285 { 286 return null; 287 } 288 for (int i = 0; i %26lt; rawdata.Length; i++) 289 { 290 if ((rawdata[i] == '\0') || (rawdata[i] %26gt; '\x007f')) 291 { 292 return null; 293 } 294 } 295 byte[] data = processTilde(rawdata); 296 if (data.Length == 0) 297 { 298 return null; 299 } 300 byte[] buffer2 = this.processExtended(data); 301 if ((buffer2.Length == 0) || (buffer2.Length %26gt; /*40*/80)) 302 { 303 return null; 304 } 305 finalLength = this._checksum ? ((buffer2.Length + 2) + 1) : (buffer2.Length + 2); 306 return this.getPattern_c39(buffer2); 307 } 308 309 310 /// %26lt;summary%26gt; 311 /// 计算效验值 312 /// %26lt;/summary%26gt; 313 /// %26lt;param name="data"%26gt;%26lt;/param%26gt; 314 /// %26lt;param name="len"%26gt;%26lt;/param%26gt; 315 /// %26lt;returns%26gt;%26lt;/returns%26gt; 316 private byte _checksum_c39(byte[] data, int len) 317 { 318 //0x2b 43 319 //字符值的总和除以合法字符集的个数43 取余数 余数在合法字符数组中对应的数值就是效验值 320 int num2 = 0; 321 for (int i = 1; i %26lt; len; i++) 322 { 323 num2 += this.valueFromCharacter(data[i]); 324 } 325 return this.c39_cw[num2 % 0x2b]; 326 } 327 328 private char[] Code39_bitpattern(string dataToEncode) 329 { 330 int finalLength = 0; 331 return this._bitpattern_c39(dataToEncode, ref finalLength); 332 } 333 /// %26lt;summary%26gt; 334 /// 获得Code39条码图片 335 /// %26lt;/summary%26gt; 336 /// %26lt;param name="resolution"%26gt;%26lt;/param%26gt; 337 /// %26lt;returns%26gt;%26lt;/returns%26gt; 338 public Bitmap getBitmapImage(float resolution) 339 { 340 return Code39_createCode(resolution); 341 } 342 343 private Bitmap Code39_createCode(float resolution) 344 { 345 int num6; 346 int finalLength = 0; 347 char[] chArray = this._bitpattern_c39(DataToEncode, ref finalLength); 348 if (chArray == null) 349 { 350 return null; 351 } 352 float fontsize = this._humanReadable ? (0.3527778f * this._humanReadableSize) : 0f; 353 // float num3 = (7f * ModuleWidth) + ((3f * Ratio) * ModuleWidth); 354 float num3 = (7f * this._moduleWidth) + ((3f * this._ratio) * this._moduleWidth); 355 float width = (finalLength * num3) + (2f * this._marginX); 356 float height = (this._moduleHeight + (2f * this._marginY)) + fontsize; 357 width *= resolution / 25.4f; 358 height *= resolution / 25.4f; 359 Bitmap image = new Bitmap((int)width, (int)height, PixelFormat.Format32bppPArgb); 360 image.SetResolution(resolution, resolution); 361 //image.SetResolution(300, 300); 362 Graphics g = Graphics.FromImage(image); 363 g.Clear(Color.White); 364 g.PageUnit = GraphicsUnit.Millimeter; //以毫米为度量单位 365 g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, /*(int)width*/image.Width, /*(int)height*/image.Height)); 366 //new Pen(Color.Black, 2f); 367 //new SolidBrush(Color.White); 368 SolidBrush brush = new SolidBrush(Color.Black); 369 if (resolution %26lt; 300f) 370 { 371 //g.TextRenderingHint = TextRenderingHint.AntiAlias; 372 //g.SmoothingMode = SmoothingMode.AntiAlias; 373 g.CompositingQuality = CompositingQuality.HighQuality; 374 //g.SmoothingMode = SmoothingMode.HighQuality; 375 g.InterpolationMode = InterpolationMode.HighQualityBicubic; 376 g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; 377 } 378 float num7 = 0f; 379 for (num6 = 0; num6 %26lt; chArray.Length; num6++) 380 { 381 if (chArray[num6] == '0') 382 { 383 if ((num6 %26amp; 1) != 1) 384 { 385 RectangleF rect = new RectangleF(MarginX + num7, MarginY, ModuleWidth, ModuleHeight); 386 MakeBar(g, rect, Reduction); 387 } 388 num7 += 1f * ModuleWidth; 389 } 390 else 391 { 392 if ((num6 %26amp; 1) != 1) 393 { 394 RectangleF ef2 = new RectangleF(MarginX + num7, MarginY, Ratio * ModuleWidth, ModuleHeight); 395 MakeBar(g, ef2, Reduction); 396 } 397 num7 += Ratio * ModuleWidth; 398 } 399 } 400 401 #region 供人识别内容 402 if (this._humanReadable) 403 { 404 #region 保留 405 /*byte[] buffer2 = processTilde(this._dataToEncode); 406 int index = 0; 407 List%26lt;byte%26gt; arr = new List%26lt;byte%26gt;(); 408 for (num6 = 0; num6 %26lt; buffer2.Length; num6++) 409 { 410 //0x20 32 0x7e 126 411 if ((buffer2[num6] %26gt;= 0x20) %26amp;%26amp; (buffer2[num6] %26lt;= 0x7e)) 412 { 413 arr.Add(buffer2[num6]); 414 } 415 } 416 byte[] bytes = new byte[arr.Count]; 417 for (int i = 0; i %26lt; arr.Count; i++) 418 { 419 bytes[i] = arr[i]; 420 } 421 index = arr.Count; 422 423 //string text = new ASCIIEncoding().GetString(bytes, 0, index); 424 */ 425 #endregion 426 string text = this._dataToEncode; 427 if (this._isDisplayCheckCode%26amp;%26amp;!string.IsNullOrEmpty(this._checkData)) { 428 text += this._checkData; 429 } 430 if (this._isDisplayStartStopSign) 431 { 432 text = "*" + text + "*"; 433 } 434 Font font = new Font(this._humanReadableFont, this._humanReadableSize); 435 //g.DrawString(text, font, brush, new PointF(MarginX, MarginY + ModuleHeight)); 436 //新增字符串格式 437 var drawFormat = new StringFormat { Alignment = StringAlignment.Center }; 438 float inpix = image.Width / resolution;//根据DPi求出 英寸数 439 float widthmm = inpix * 25.4f; //有每英寸像素求出毫米 440 //g.DrawString(text, font, Brushes.Black, width / 2, height - 14, drawFormat); 441 g.DrawString(text, font, /*Brushes.Black*/brush, new PointF(/*MarginX*/(int)(widthmm / 2), MarginY + ModuleHeight + 1), drawFormat); 442 443 } 444 445 #endregion 446 447 448 449 return image; 450 } 451 452 /// %26lt;summary%26gt; 453 /// 进行图形填充 454 /// %26lt;/summary%26gt; 455 /// %26lt;param name="g"%26gt;%26lt;/param%26gt; 456 /// %26lt;param name="rect"%26gt;%26lt;/param%26gt; 457 /// %26lt;param name="reduction"%26gt;%26lt;/param%26gt; 458 private void MakeBar(Graphics g, RectangleF rect, float reduction) 459 { 460 MakeBar(g, rect, reduction, Color.Black); 461 } 462 463 /// %26lt;summary%26gt; 464 /// 进行图形填充 465 /// %26lt;/summary%26gt; 466 /// %26lt;param name="g"%26gt;%26lt;/param%26gt; 467 /// %26lt;param name="rect"%26gt;%26lt;/param%26gt; 468 /// %26lt;param name="reduction"%26gt;%26lt;/param%26gt; 469 /// %26lt;param name="brushColor"%26gt;%26lt;/param%26gt; 470 private void MakeBar(Graphics g, RectangleF rect, float reduction, Color brushColor) 471 { 472 float num = rect.Width * (reduction / 200f); 473 float num2 = rect.Width - (rect.Width * (reduction / 200f)); 474 475 RectangleF ef = new RectangleF 476 { 477 X = rect.X + num, 478 Y = rect.Y, 479 Width = num2, 480 Height = rect.Height 481 }; 482 SolidBrush brush = new SolidBrush(brushColor); 483 g.FillRectangle(brush, ef); 484 485 } 486 487 private char[] getPattern_c39(byte[] data) 488 { //0x2a 42为* 489 //int num = 0x27; 490 int num = 80; 491 byte[] buffer = new byte[num + 1]; 492 buffer[0] = 0x2a; 493 int index = 1; 494 for (int i = 0; i %26lt; data.Length; i++) 495 { 496 buffer[index] = data[i]; 497 index++; 498 } 499 if (Checksum) 500 { 501 buffer[index] = this._checksum_c39(buffer, index); 502 if (_isDisplayCheckCode) 503 { 504 this._checkData = ((char)buffer[index]).ToString(); 505 } 506 index++; 507 } 508 buffer[index] = 0x2a; 509 index++; 510 char[] chArray = new char[index * 10]; 511 int num5 = 0; 512 for (int j = 0; j %26lt; index; j++) 513 { 514 byte c = buffer[j]; 515 int num9 = this.indexFromCharacter(c); 516 for (int k = 0; k %26lt; 9; k++) 517 { 518 chArray[num5] = (char)this.c39_bp[num9, k + 1]; 519 num5++; 520 } 521 chArray[num5] = '0'; 522 num5++; 523 } 524 return chArray; 525 } 526 527 private int indexFromCharacter(byte c) 528 { 529 //0x2c==44 530 for (int i = 0; i %26lt; 0x2c; i++) 531 { 532 if (this.c39_bp[i, 0] == c) 533 { 534 return i; 535 } 536 } 537 return -1; 538 } 539 540 541 private byte[] processExtended(byte[] data) 542 { 543 //0x25 38 544 //0x4F 79 0x4E 78 545 //int num = 0x4F; 546 int num = data.Length - 1; 547 byte[] sourceArray = new byte[num + 1]; 548 int index = 0; 549 for (int i = 0; i %26lt; data.Length; i++) 550 { 551 byte c = data[i]; 552 if (this.valueFromCharacter(c) != -1) 553 { 554 sourceArray[index] = c; 555 index++; 556 } 557 else 558 { 559 byte num5 = 0; 560 byte num6 = 0; 561 if (this.valuesFromExtended(c, ref num5, ref num6)) 562 { 563 sourceArray[index] = num5; 564 sourceArray[index + 1] = num6; 565 index += 2; 566 } 567 } 568 } 569 byte[] destinationArray = new byte[index]; 570 Array.Copy(sourceArray, destinationArray, index); 571 return destinationArray; 572 } 573 574 /// %26lt;summary%26gt; 575 /// 返回指定字符在code39合法字符数组中对应的索引 576 /// %26lt;/summary%26gt; 577 /// %26lt;param name="c"%26gt;%26lt;/param%26gt; 578 /// %26lt;returns%26gt;%26lt;/returns%26gt; 579 private int valueFromCharacter(byte c) 580 { 581 //c39_cw为数组,保存的为合法的字符集信息[0-9A-Z+-*/%. ] 共43个 582 //如果存在这个字符就返回c39_cw对应的索引 583 for (int i = 0; i %26lt; /*0x2b*/this.c39_cw.Length; i++) 584 { 585 if (this.c39_cw[i] == c) 586 { 587 return i; 588 } 589 } 590 return -1; 591 } 592 593 /// %26lt;summary%26gt; 594 /// 判断字符集是否存在Extended 595 /// %26lt;/summary%26gt; 596 /// %26lt;param name="c"%26gt;%26lt;/param%26gt; 597 /// %26lt;param name="v1"%26gt;%26lt;/param%26gt; 598 /// %26lt;param name="v2"%26gt;%26lt;/param%26gt; 599 /// %26lt;returns%26gt;%26lt;/returns%26gt; 600 private bool valuesFromExtended(byte c, ref byte v1, ref byte v2) 601 { 602 //0x55 85 603 for (int i = 0; i %26lt; 0x55; i++) 604 { 605 if (this.c39_ex[i, 0] == c) 606 { 607 v1 = this.c39_ex[i, 1]; 608 v2 = this.c39_ex[i, 2]; 609 return true; 610 } 611 } 612 return false; 613 } 614 615 private byte[] processTilde(string rawdata) 616 { 617 byte[] sourceArray = new byte[rawdata.Length]; 618 int index = 0; 619 for (int i = 0; i %26lt; rawdata.Length; i++) 620 { 621 if (rawdata[i] != '~') 622 { 623 sourceArray[index] = (byte)rawdata[i]; 624 index++; 625 } 626 else if ((i + 3) %26lt; rawdata.Length) 627 { 628 string str = new string(new char[] { rawdata[i + 1], rawdata[i + 2], rawdata[i + 3] }); 629 int num3 = Convert.ToInt32(str, 10); 630 if ((num3 %26gt; 0) %26amp;%26amp; (num3 %26lt;= 0xff)) 631 { 632 sourceArray[index] = (byte)num3; 633 index++; 634 } 635 if (num3 == 0x3e7) 636 { 637 sourceArray[index] = 0x86; 638 index++; 639 } 640 i += 3; 641 } 642 else 643 { 644 sourceArray[index] = (byte)rawdata[i]; 645 index++; 646 } 647 } 648 byte[] destinationArray = new byte[index]; 649 Array.Copy(sourceArray, destinationArray, index); 650 return destinationArray; 651 } 652 }
%26nbsp;WinForm的例子下载:点击下载DEMO
%26nbsp;