Gma.QrCodeNet.Encoding.ErrorCorrectionLevel ToErrorCorrectionLevel(ErrorCorrectionLevel level) { switch (level) { case ErrorCorrectionLevel.L: return(Gma.QrCodeNet.Encoding.ErrorCorrectionLevel.L); case ErrorCorrectionLevel.M: return(Gma.QrCodeNet.Encoding.ErrorCorrectionLevel.M); case ErrorCorrectionLevel.Q: return(Gma.QrCodeNet.Encoding.ErrorCorrectionLevel.Q); case ErrorCorrectionLevel.H: return(Gma.QrCodeNet.Encoding.ErrorCorrectionLevel.H); default: throw new NotSupportedException("不受支持的ErrorCorrectionLevel值: " + level.ToString()); } }
/// <summary> /// Generates an QR BarCode containing the specified data. /// </summary> /// <param name="data">The data to encode.</param> /// <param name="size">Image size.</param> /// <param name="margin">The width of the white border around the data portion of the code. This is in rows, not in pixels.</param> /// <param name="errorCorrection">QR codes support four levels of error correction to enable recovery of missing, misread, or obscured data. Greater redundancy is achieved at the cost of being able to store less data.</param> /// <returns>The image containing the generated QR BarCode.</returns> public static Image Generate(string data, int size, int margin, ErrorCorrectionLevel errorCorrection) { if (data == null) { throw new ArgumentNullException("data"); } if (size < 1) { throw new ArgumentOutOfRangeException("size", size, "Must be greater than zero."); } if (margin < 0) { throw new ArgumentOutOfRangeException("margin", margin, "Must be greater than or equal to zero."); } if (!Enum.IsDefined(typeof(ErrorCorrectionLevel), errorCorrection)) { throw new InvalidEnumArgumentException("errorCorrectionLevel", (int)errorCorrection, typeof(ErrorCorrectionLevel)); } var link = string.Format(m_GoogleApiUrl, size, HttpUtility.UrlEncode(data), errorCorrection.ToString()[0], margin); using (var ms = new MemoryStream(m_WebClient.DownloadData(link))) { return(Image.FromStream(ms)); } }