开源项目barcodelib-C#条形码图像生成库
生活随笔
收集整理的這篇文章主要介紹了
开源项目barcodelib-C#条形码图像生成库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
介紹
該庫旨在為開發人員提供一個簡單的類,以便他們在需要根據數據字符串生成條形碼圖像時使用。
用法
該庫包含一個名為BarcodeLib的類,其中包含三個構造函數:
Barcode(); Barcode(string); Barcode(string, BarcodeLib.TYPE);如果決定使用參數創建實例,則參數如下:字符串是要編碼到條形碼中的數據,而BarcodeLib.TYPE是用于編碼數據的符號體系。如果在創建實例時未選擇指定數據和類型,則可以稍后(但在編碼之前)通過適當的屬性指定它們。
BarcodeLib.Barcode b = new BarcodeLib.Barcode(); Image img = b.Encode(BarcodeLib.TYPE.UPCA, "038000356216", Color.Black, Color.White, 290, 120); private void btnEncode_Click(object sender, EventArgs e){errorProvider1.Clear();int W = Convert.ToInt32(this.txtWidth.Text.Trim());int H = Convert.ToInt32(this.txtHeight.Text.Trim());b.Alignment = AlignmentPositions.CENTER;//barcode alignmentswitch (cbBarcodeAlign.SelectedItem.ToString().Trim().ToLower()){case "left": b.Alignment = AlignmentPositions.LEFT; break;case "right": b.Alignment = AlignmentPositions.RIGHT; break;default: b.Alignment = AlignmentPositions.CENTER; break;}//switchTYPE type = TYPE.UNSPECIFIED;switch (cbEncodeType.SelectedItem.ToString().Trim()){case "UPC-A": type = TYPE.UPCA; break;case "UPC-E": type = TYPE.UPCE; break;case "UPC 2 Digit Ext.": type = TYPE.UPC_SUPPLEMENTAL_2DIGIT; break;case "UPC 5 Digit Ext.": type = TYPE.UPC_SUPPLEMENTAL_5DIGIT; break;case "EAN-13": type = TYPE.EAN13; break;case "JAN-13": type = TYPE.JAN13; break;case "EAN-8": type = TYPE.EAN8; break;case "ITF-14": type = TYPE.ITF14; break;case "Codabar": type = TYPE.Codabar; break;case "PostNet": type = TYPE.PostNet; break;case "Bookland/ISBN": type = TYPE.BOOKLAND; break;case "Code 11": type = TYPE.CODE11; break;case "Code 39": type = TYPE.CODE39; break;case "Code 39 Extended": type = TYPE.CODE39Extended; break;case "Code 39 Mod 43": type = TYPE.CODE39_Mod43; break;case "Code 93": type = TYPE.CODE93; break;case "LOGMARS": type = TYPE.LOGMARS; break;case "MSI": type = TYPE.MSI_Mod10; break;case "Interleaved 2 of 5": type = TYPE.Interleaved2of5; break;case "Interleaved 2 of 5 Mod 10": type = TYPE.Interleaved2of5_Mod10; break;case "Standard 2 of 5": type = TYPE.Standard2of5; break;case "Standard 2 of 5 Mod 10": type = TYPE.Standard2of5_Mod10; break;case "Code 128": type = TYPE.CODE128; break;case "Code 128-A": type = TYPE.CODE128A; break;case "Code 128-B": type = TYPE.CODE128B; break;case "Code 128-C": type = TYPE.CODE128C; break;case "Telepen": type = TYPE.TELEPEN; break;case "FIM": type = TYPE.FIM; break;case "Pharmacode": type = TYPE.PHARMACODE; break;default: MessageBox.Show("Please specify the encoding type."); break;}//switchtry{if (type != TYPE.UNSPECIFIED){try{b.BarWidth = textBoxBarWidth.Text.Trim().Length < 1 ? null : (int?)Convert.ToInt32(textBoxBarWidth.Text.Trim());}catch (Exception ex){throw new Exception("Unable to parse BarWidth: " + ex.Message, ex);}try{b.AspectRatio = textBoxAspectRatio.Text.Trim().Length < 1 ? null : (double?)Convert.ToDouble(textBoxAspectRatio.Text.Trim());}catch (Exception ex){throw new Exception("Unable to parse AspectRatio: " + ex.Message, ex);}b.IncludeLabel = this.chkGenerateLabel.Checked;b.RotateFlipType = (RotateFlipType)Enum.Parse(typeof(RotateFlipType), this.cbRotateFlip.SelectedItem.ToString(), true);if (!String.IsNullOrEmpty(this.textBox1.Text.Trim()))b.AlternateLabel = this.textBox1.Text;elseb.AlternateLabel = this.txtData.Text;//label alignment and positionswitch (this.cbLabelLocation.SelectedItem.ToString().Trim().ToUpper()){case "BOTTOMLEFT": b.LabelPosition = LabelPositions.BOTTOMLEFT; break;case "BOTTOMRIGHT": b.LabelPosition = LabelPositions.BOTTOMRIGHT; break;case "TOPCENTER": b.LabelPosition = LabelPositions.TOPCENTER; break;case "TOPLEFT": b.LabelPosition = LabelPositions.TOPLEFT; break;case "TOPRIGHT": b.LabelPosition = LabelPositions.TOPRIGHT; break;default: b.LabelPosition = LabelPositions.BOTTOMCENTER; break;}//switch//===== Encoding performed here =====barcode.BackgroundImage = b.Encode(type, this.txtData.Text.Trim(), this.btnForeColor.BackColor, this.btnBackColor.BackColor, W, H);//===================================//show the encoding timethis.lblEncodingTime.Text = "(" + Math.Round(b.EncodingTime, 0, MidpointRounding.AwayFromZero).ToString() + "ms)";txtEncoded.Text = b.EncodedValue;tsslEncodedType.Text = "Encoding Type: " + b.EncodedType.ToString();// Read dynamically calculated Width/Height because the user is interested.if (b.BarWidth.HasValue)txtWidth.Text = b.Width.ToString();if (b.AspectRatio.HasValue)txtHeight.Text = b.Height.ToString();}//if//reposition the barcode image to the middlebarcode.Location = new Point((this.barcode.Location.X + this.barcode.Width / 2) - barcode.Width / 2, (this.barcode.Location.Y + this.barcode.Height / 2) - barcode.Height / 2);}//trycatch (Exception ex){MessageBox.Show(ex.Message);}//catch}//btnEncode_Click版權和許可
?Apache-2.0 License
開源地址
https://github.com/barnhill/barcodelib
總結
以上是生活随笔為你收集整理的开源项目barcodelib-C#条形码图像生成库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 晋升新一线的合肥,跨平台的.NET氛围究
- 下一篇: 2021技术人新展望