标签:
1.下载Qrcode库源码, 下载地址:http://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library
2.打开源码时, 部分类库可能会加载失败, 不用理会, 只需正常加载 QRCodeLib, QRCodeSampleApp 即可.
3.生成时, 会提示编译出错, Error‘ThoughtWorks.QRCode.Properties.Resources‘ does not contain a definition for ‘GetResource‘.
需修改错误提示的代码行为:
MemoryStream memoryStream = new MemoryStream((byte[])Resources.ResourceManager.GetObject(fileName));
修改支持中文
修改库QRCodeLib中的类QRCodeEncoder.cs
public virtual Bitmap Encode(String content) { if (QRCodeUtility.IsUniCode(content)) { return Encode(content, Encoding.Unicode); } else { return Encode(content, Encoding.ASCII); } }
为
public virtual Bitmap Encode(String content) { if (QRCodeUtility.IsUniCode(content)) { return Encode(content, Encoding.GetEncoding("gb2312")); } else { return Encode(content, Encoding.ASCII); } }
对应的,QRCodeDecoder.cs类中的相应代码也要改。这样就中英文支持了。(改为UTF8也可以)
扩展
1、修改 QRCodeEncoder.cs 跟 QRCodeDecoder.cs. 搜索一下, 將 Encoding.Unicode 替換成 Encoding.UTF8
2、修改 QRCodeUtility.cs. 將 UnicodeEncoding encoding = new UnicodeEncoding(); 改成 UTF8Encoding encoding = new UTF8Encoding();
使用
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); String encoding = cboEncoding.Text ; if (encoding == "Byte") { qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; } else if (encoding == "AlphaNumeric") { qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC; } else if (encoding == "Numeric") { qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC; } try { int scale = Convert.ToInt16(txtSize.Text); qrCodeEncoder.QRCodeScale = scale; } catch (Exception ex) { MessageBox.Show("Invalid size!"); return; } try { int version = Convert.ToInt16(cboVersion.Text) ; qrCodeEncoder.QRCodeVersion = version; } catch (Exception ex) { MessageBox.Show("Invalid version !"); } string errorCorrect = cboCorrectionLevel.Text; if (errorCorrect == "L") qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L; else if (errorCorrect == "M") qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; else if (errorCorrect == "Q") qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q; else if (errorCorrect == "H") qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H; Image image; String data = txtEncodeData.Text; image = qrCodeEncoder.Encode(data); picEncode.Image = image;
带图片
/// <summary> /// 生成二维码. /// </summary> /// <param name="data">需要添加进去的文本</param> /// <returns></returns> public System.Drawing.Image GCode(String data) { QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; qrCodeEncoder.QRCodeScale = 5; qrCodeEncoder.QRCodeVersion = 7; qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L; var pbImg = qrCodeEncoder.Encode(data, System.Text.Encoding.UTF8); var width = pbImg.Width / 10; var dwidth = width * 2; Bitmap bmp = new Bitmap(pbImg.Width + dwidth, pbImg.Height + dwidth); Graphics g = Graphics.FromImage(bmp); var c = System.Drawing.Color.White; g.FillRectangle(new SolidBrush(c), 0, 0, pbImg.Width + dwidth, pbImg.Height + dwidth); g.DrawImage(pbImg, width, width); g.Dispose(); return bmp; } /// <summary> /// 调用此函数后使此两种图片合并,类似相册,有个 /// 背景图,中间贴自己的目标图片 /// </summary> /// <param name="sourceImg">粘贴的源图片</param> /// <param name="destImg">粘贴的目标图片</param> public static System.Drawing.Image CombinImage(System.Drawing.Image imgBack, string destImg) { System.Drawing.Image img = System.Drawing.Image.FromFile(destImg); //照片图片 if (img.Height != 50 || img.Width != 50) { img = KiResizeImage(img, 50, 50, 0); } Graphics g = Graphics.FromImage(imgBack); g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height); //g.DrawImage(imgBack, 0, 0, 相框宽, 相框高); //g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一层黑色边框 //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高); g.DrawImage(img, imgBack.Width / 2 - img.Width / 2, imgBack.Width / 2 - img.Width / 2 , img.Width, img.Height); GC.Collect(); return imgBack; } /// <summary> /// Resize图片 /// </summary> /// <param name="bmp">原始Bitmap</param> /// <param name="newW">新的宽度</param> /// <param name="newH">新的高度</param> /// <param name="Mode">保留着,暂时未用</param> /// <returns>处理以后的图片</returns> public static System.Drawing.Image KiResizeImage(System.Drawing.Image bmp, int newW, int newH, int Mode) { try { System.Drawing.Image b = new Bitmap(newW, newH); Graphics g = Graphics.FromImage(b); // 插值算法的质量 g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel); g.Dispose(); return b; } catch { return null; } }
原文地址:http://blog.csdn.net/a497785609/article/details/43019951
完整Demo下载:http://download.csdn.net/detail/a497785609/8387311
标签:
原文地址:http://www.cnblogs.com/jiyang2008/p/5455200.html