C#生成二维码,返回Base64字符
作者:互联网
using ThoughtWorks.QRCode.Codec;
public static string GetQrCodeImage() { //二维码生成对象,需引用DLL QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; qrCodeEncoder.QRCodeVersion = 0;//版本控制,根据dll的版本,不同版本支持的二维码内容存储的长度不同 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; string data = string.Format(ConfigurationManager.AppSettings["mpServicePath"], "Html/ProviderReg/ProviderOpen.aspx?QrCodeInfoId=001"); qrCodeEncoder.QRCodeScale = data.Length; //data为扫描二维码跳转的地址(也可以是文本) System.Drawing.Bitmap image = qrCodeEncoder.Encode(data, Encoding.UTF8); System.IO.MemoryStream MStream = new System.IO.MemoryStream(); image.Save(MStream, System.Drawing.Imaging.ImageFormat.Png); byte[] by = new byte[MStream.Length]; MStream.Position = 0; MStream.Read(by, 0, (int)MStream.Length);//写入进去。 MStream.Close();//释放资源 string base64 = Convert.ToBase64String(by);//转换成字节流 Dictionary<string, string> dic = new Dictionary<string, string>(); dic["base64"] = base64; JavaScriptSerializer jss = new JavaScriptSerializer(); return jss.Serialize(dic);//返回json格式 }
需百度下载ThoughtWorks.QRCode.dll,引用到项目中
提示:二维码内容存储的长度有一定限制,如果参url数较多,建议将所有参数存到数据表中,然后只传一个ID,在跳转后的页面根据ID查询参数
标签:C#,Base64,System,qrCodeEncoder,二维码,new,MStream,QRCodeEncoder 来源: https://www.cnblogs.com/HRainet/p/14336473.html