public static Bitmap GetCode(string str, ENCODE_MODE em = ENCODE_MODE.BYTE, ERROR_CORRECTION ec = ERROR_CORRECTION.MIDDLE, int Version = 0, int Scale = 4 ) { try { if (String.IsNullOrEmpty(str)) { throw new Exception("字符串为空"); } //初始化二维码生成工具 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeEncodeMode = (QRCodeEncoder.ENCODE_MODE)em; qrCodeEncoder.QRCodeErrorCorrect = (QRCodeEncoder.ERROR_CORRECTION)ec; qrCodeEncoder.QRCodeVersion = Version; qrCodeEncoder.QRCodeScale = Scale; //将字符串生成二维码图片 Bitmap image = qrCodeEncoder.Encode(str, Encoding.UTF8); return(image); } catch (Exception ex) { throw ex; } }
public QRCodeEncoder() { this.qrcodeErrorCorrect = ERROR_CORRECTION.M; this.qrcodeEncodeMode = ENCODE_MODE.BYTE; this.qrcodeVersion = 7; this.qrcodeStructureappendN = 0; this.qrcodeStructureappendM = 0; this.qrcodeStructureappendParity = 0; this.qrcodeStructureappendOriginaldata = ""; this.qrCodeScale = 4; this.qrCodeBackgroundColor = System.Drawing.Color.White; this.qrCodeForegroundColor = System.Drawing.Color.Black; }
public QRCodeEncoder() { qrcodeErrorCorrect = ERROR_CORRECTION.M; qrcodeEncodeMode = ENCODE_MODE.BYTE; qrcodeVersion = 7; qrcodeStructureappendN = 0; qrcodeStructureappendM = 0; qrcodeStructureappendParity = 0; qrcodeStructureappendOriginaldata = ""; qrCodeScale = 4; }
public QRCodeEncoder() { qrcodeErrorCorrect = ERROR_CORRECTION.M; qrcodeEncodeMode = ENCODE_MODE.BYTE; qrcodeVersion = 7; qrcodeStructureappendN = 0; qrcodeStructureappendM = 0; qrcodeStructureappendParity = 0; qrcodeStructureappendOriginaldata = ""; qrCodeScale = 4; qrCodeBackgroundColor = Color.White; qrCodeForegroundColor = Color.Black; }
/// <summary> /// Constructor /// </summary> public QRCodeEncoder() { qrcodeErrorCorrect = ERROR_CORRECTION.M; qrcodeEncodeMode = ENCODE_MODE.BYTE; qrcodeVersion = 7; qrcodeStructureappendN = 0; qrcodeStructureappendM = 0; qrcodeStructureappendParity = 0; qrcodeStructureappendOriginaldata = ""; qrCodeScale = 4; qrCodeBackgroundColor = Color.White; qrCodeForegroundColor = Color.Black; }
/// <summary> /// Constructor /// </summary> public QRCodeEncoder() { qrcodeErrorCorrect = ERROR_CORRECTION.M; qrcodeEncodeMode = ENCODE_MODE.BYTE; qrcodeVersion = 7; qrcodeStructureappendN = 0; qrcodeStructureappendM = 0; qrcodeStructureappendParity = 0; qrcodeStructureappendOriginaldata = ""; qrCodeScale = 4; qrCodeBackgroundColor = Color.White; qrCodeForegroundColor = Color.Black; //QRCODE_DATA_PATH = Environment.CurrentDirectory + @"\" + DATA_PATH; }
/// <summary> /// Constructor /// </summary> public QRCodeEncoder() { qrcodeErrorCorrect = ERROR_CORRECTION.M; qrcodeEncodeMode = ENCODE_MODE.BYTE; qrcodeVersion = 7; qrcodeStructureappendN = 0; qrcodeStructureappendM = 0; qrcodeStructureappendParity = 0; qrcodeStructureappendOriginaldata = ""; qrCodeScale = 4; qrCodeBackgroundColor = Color.White; qrCodeForegroundColor = Color.Black; QRCODE_DATA_PATH = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName) + @"\" + DATA_PATH; }
/// <summary> /// 生成二维码 /// </summary> /// <param name="text">二维码文本</param> /// <param name="scale">尺寸</param> /// <param name="version">版本</param> /// <param name="mode">编码模式</param> /// <param name="errorCorrect">错误纠正级别</param> /// <returns></returns> public static Image GenerateQRCode(string text, int scale = 5, int version = 0, ENCODE_MODE mode = ENCODE_MODE.BYTE, ERROR_CORRECTION errorCorrect = ERROR_CORRECTION.M) { try { if (!string.IsNullOrEmpty(text)) { var qrCoder = new QRCodeEncoder(); qrCoder.QRCodeEncodeMode = EnumHelper.Parse <QRCodeEncoder.ENCODE_MODE>(mode.ToString()); qrCoder.QRCodeScale = 5; qrCoder.QRCodeVersion = 0; qrCoder.QRCodeErrorCorrect = EnumHelper.Parse <QRCodeEncoder.ERROR_CORRECTION>(errorCorrect.ToString()); return(Image.FromHbitmap(qrCoder.Encode(text, Encoding.GetEncoding("GB2312")).GetHbitmap())); } } catch { } return(null); }
/// 生成二维码 /// </summary> /// <param name="dir">生成二维码目录</param> /// <param name="strData">要生成的文字或者数字,支持中文。如: "4408810820 深圳-广州" 或者:4444444444</param> /// <param name="qrEncoding">三种尺寸:BYTE ,ALPHA_NUMERIC,NUMERIC</param> /// <param name="level">大小:L M Q H</param> /// <param name="version">版本:如 8</param> /// <param name="scale">比例:如 4</param> /// <returns></returns> public static string CreateCode_Choose(string dir, string strData, ENCODE_MODE qrEncoding, ERROR_CORRECTION level, int version, int scale) { QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); ENCODE_MODE encoding = qrEncoding; switch (encoding) { case ENCODE_MODE.BYTE: qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; break; case ENCODE_MODE.ALPHA_NUMERIC: qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC; break; case ENCODE_MODE.NUMERIC: qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC; break; default: qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; break; } qrCodeEncoder.QRCodeScale = scale; qrCodeEncoder.QRCodeVersion = version; switch (level) { case ERROR_CORRECTION.L: qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L; break; case ERROR_CORRECTION.M: qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; break; case ERROR_CORRECTION.Q: qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q; break; default: qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H; break; } //文字生成图片 Image image = qrCodeEncoder.Encode(strData); string filename = DateTime.Now.ToString("yyyyMMddhhmmssfff").ToString() + ".jpg"; string filepath = dir.TrimEnd('\\') + "\\" + filename; //如果文件夹不存在,则创建 //if (!Directory.Exists(filepath)) // Directory.CreateDirectory(filepath); System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write); image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); fs.Close(); image.Dispose(); return(filepath); }