因為條形碼有不同的編碼格式,使用時要註意編碼格式。首先在 NuGet包管理器中引用下載BarcodeLib.dll文件。BarcodeLib.dll一維條碼庫,支持以下條碼格式:UPC-A,UPC-E,UPC 2 Digit Ext,UPC 5 Digit Ext.,EAN-13,JAN-13,EA ...
因為條形碼有不同的編碼格式,使用時要註意編碼格式。首先在 NuGet包管理器中引用下載BarcodeLib.dll文件。BarcodeLib.dll一維條碼庫,支持以下條碼格式:UPC-A,UPC-E,UPC 2 Digit Ext,UPC 5 Digit Ext.,EAN-13,JAN-13,EAN-8,ITF-14,Codabar,PostNet,Bookland/ISBN,Code 11,Code 39,Code 39 Extended,Code 93,LOGMARS,MSI,Interleaved 2 of 5,Standard 2 of 5,Code 128,Code 128-A,Code 128-B,Code 128-C,Telepen ;
要執行的方法下:
System.Drawing.Image image; int width = 148, height = 55; string fileSavePath = AppDomain.CurrentDomain.BaseDirectory + "BarcodePattern.jpg"; if (File.Exists(fileSavePath)) File.Delete(fileSavePath); GetBarcode(height, width, BarcodeLib.TYPE.CODE128//編碼格式 , "要編碼的數據", out image, fileSavePath); pictureBox1.Image = Image.FromFile("BarcodePattern.jpg");
註意要改的參數
public static void GetBarcode(int height, int width, BarcodeLib.TYPE type, string code, out System.Drawing.Image image, string fileSaveUrl) { try { image = null; BarcodeLib.Barcode b = new BarcodeLib.Barcode(); b.BackColor = System.Drawing.Color.White;//圖片背景顏色 b.ForeColor = System.Drawing.Color.Black;//條碼顏色 b.IncludeLabel = true; b.Alignment = BarcodeLib.AlignmentPositions.LEFT; b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER; b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;//圖片格式 System.Drawing.Font font = new System.Drawing.Font("verdana", 10f);//字體設置 b.LabelFont = font; b.Height = height;//圖片高度設置(px單位) b.Width = width;//圖片寬度設置(px單位) image = b.Encode(type, code);//生成圖片 image.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception ex) { image = null; } }
借鑒於掙脫生命的束縛:http://www.cnblogs.com/qqhfeng/p/4240597.html
詳細的講解:http://www.barcodelib.com/net_barcode/main.html
二維碼
適用於WinForm ,需要引用 zxing.dll文件 下載地址:http://pan.baidu.com/s/1ntNr79v
註意要引用的文件,空間
using com.google.zxing;
using COMMON = com.google.zxing.common;
代碼如下:
private void BtnBm_Click(object sender, EventArgs e) //生成二維碼 { if (string.IsNullOrEmpty(this.textBox1.Text.Trim())) { MessageBox.Show("請輸入需要轉換的信息!"); } else { string content = this.textBox1.Text;//待編碼數據 try { int QSize = Int32.Parse(txtSize.Text);//二維碼大小 string s = hScrollBar1.Value.ToString("X");//二維碼透明度 string q = hScrollBar2.Value.ToString("X");//背景透明度 string Scolor = "0x" + s + txtYSe.Text;//二維碼顏色 string Qcolor = "0x" + q + txtBys.Text;//背景顏色 COMMON.ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QSize, QSize); Bitmap bt = toBitmap(byteMatrix, Scolor, Qcolor); pictureBox1.Image = bt; } catch (Exception ex) { MessageBox.Show(ex.Message); } } } public static Bitmap toBitmap(COMMON.ByteMatrix matrix, string scolor, string qcolor) { int width = matrix.Width; int height = matrix.Height; Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml(scolor) : ColorTranslator.FromHtml(qcolor)); } } return bmap; }private void BtnSave_Click(object sender, EventArgs e) //保存 { Image img = pictureBox1.Image; if (img != null) { SaveFileDialog sFD = new SaveFileDialog(); sFD.Filter = "*.png|*.png"; if (sFD.ShowDialog() == DialogResult.OK) { Bitmap bmap = new Bitmap(img, img.Width, img.Height); bmap.Save(sFD.FileName); MessageBox.Show("保存成功!"); } } else { MessageBox.Show("您還沒有生成二維碼!"); } } private void BtnJm_Click(object sender, EventArgs e) //讀碼解碼 { if (this.openFileDialog1.ShowDialog() != DialogResult.OK) { return; } Bitmap bmap; try { Image img = Image.FromFile(this.openFileDialog1.FileName); bmap = new Bitmap(img); if (bmap == null) { MessageBox.Show("解碼錯誤,請確保二維碼圖片已打開!"); return; } } catch { MessageBox.Show("解碼錯誤,請確保圖片格式正確!"); return; } LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height); com.google.zxing.BinaryBitmap bitmap = new com.google.zxing.BinaryBitmap(new COMMON.HybridBinarizer(source)); Result result; try { result = new MultiFormatReader().decode(bitmap); } catch { string str = "解碼失敗,失敗原因可能是:" + "\n"; str += "1.您打開的圖片非二維碼圖片!" + "\n"; str += "2.您打開的二維碼圖片背景色太深!" + "\n"; str += "3.您打開的二維碼圖片二維碼和背景色太接近!" + "\n"; MessageBox.Show(str); return; } textBox1.Text = result.Text; }
文章參考:https://www.cnblogs.com/xuhang/p/3831776.html,大家有時間可以去看看!