共用程式的寫法:
public static MemoryStream GetQRCode(string data)
{
//String data = context.Request["CodeText"];
System.Web.HttpContext context = System.Web.HttpContext.Current;
System.IO.MemoryStream MStream = new MemoryStream();
if (!string.IsNullOrEmpty(data))
{
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrCodeEncoder.QRCodeScale = 4;
qrCodeEncoder.QRCodeVersion = 8;
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
System.Drawing.Bitmap image = qrCodeEncoder.Encode(data);
MStream = new System.IO.MemoryStream();
image.Save(MStream, System.Drawing.Imaging.ImageFormat.Png);
context.Response.ClearContent();
context.Response.ContentType = "image/Png";
context.Response.BinaryWrite(MStream.ToArray());
}
context.Response.Flush();
context.Response.End();
return MStream;
}
image存在主頁面的程式
MemoryStream ms = new MemoryStream();
ms = SysHelp.GetQRCode(imUrl1);
Image1.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(ms.ToArray(), 0, ms.ToArray().Length);