这期内容当中小编将会给大家带来有关怎么在C#中使用QRCode动态生成自定义二维码图片,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
设置图片的数据源为动态图片
<body> <form id="form1" runat="server" > <div> <img src="GenerateImage.aspx?type=2" /> </div> </form> </body>
动态生成图片
GenerateImage.aspx.cs文件内容
protected void Page_Load(object sender, EventArgs e) { string type = Request.QueryString["type"].ToString(); Bitmap codeImage = Create_QRCode("分享才能获得更多,我尽力而为(5201314)", 6); MemoryStream ms = Create_ImgCode(codeImage, "分享才能获得更多,我尽力而为", "5201314", type); Response.ClearContent(); Response.ContentType = "image/Png"; Response.BinaryWrite(ms.ToArray()); Response.End(); } private Bitmap Create_QRCode(string codeNumber, int size) { //创建二维码生成类 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); //设置编码模式 qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; //设置编码测量度 qrCodeEncoder.QRCodeScale = size; //设置编码版本 qrCodeEncoder.QRCodeVersion = 10; //设置编码错误纠正 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; //生成二维码图片 System.Drawing.Bitmap codeImage = qrCodeEncoder.Encode(codeNumber, Encoding.UTF8); return codeImage; } /// <summary> /// 生成自定义图片 /// </summary> /// <param name="codeImage">生成的二维码</param> /// <param name="objectName">物体名称</param> /// <returns>自定义图片内存流</returns> private MemoryStream Create_ImgCode(Bitmap codeImage, string objectName, string objectCode, string type) { string path = string.Empty; if (type == "1") { //设置背景图片 path = Server.MapPath("Images/backimg1.png"); } else if (type == "2") { //设置背景图片 path = Server.MapPath("Images/backimg2.png"); } System.Drawing.Image img = System.Drawing.Image.FromFile(path); Bitmap bg = new Bitmap(img); //为画布bg(图片bg)创建一只画笔 Graphics g = Graphics.FromImage(bg); if (type == "1") { //【1】将位图文件codeImage画到画布g上 //【2】codeImage左上角距画布左边界25px、距画布上边界56px //【3】codeImage的长为原长、宽为原宽 g.DrawImage(codeImage, 25, 56, codeImage.Width, codeImage.Height); } else if (type == "2") { g.DrawImage(codeImage, 132, 19, 162, 162); System.Drawing.Brush b = new SolidBrush(Color.Black); Font font = new Font("宋体", 8, FontStyle.Regular); StringFormat sf = new StringFormat(); sf.LineAlignment = StringAlignment.Center; // 垂直居中 sf.Alignment = StringAlignment.Near; // 水平左对齐 //string也是画到画布上的,当画的string长度大于112px时会自动换行 SizeF stringSize = g.MeasureString("我的宣言:", font, 112, sf); int nWidth = (int)stringSize.Width + 1; int nHeight = (int)stringSize.Height + 1; RectangleF rf = new Rectangle(new Point(12, 64), new Size(nWidth, nHeight)); g.DrawString("我的宣言:", font, b, rf, sf); stringSize = g.MeasureString(objectName, font, 112, sf); int objectWidth = (int)stringSize.Width + 1; int objectHeight = (int)stringSize.Height + 1; rf = new Rectangle(new Point(12, 64 + nHeight + 8), new Size(objectWidth, objectHeight)); g.DrawString(objectName, font, b, rf, sf); SizeF stringSize1 = g.MeasureString("幸运数字:", font, 112, sf); nWidth = (int)stringSize1.Width + 1; nHeight = (int)stringSize1.Height + 1; RectangleF rf1 = new Rectangle(new Point(12, 136), new Size(nWidth, nHeight)); g.DrawString("幸运数字:", font, b, rf1, sf); stringSize1 = g.MeasureString(objectCode, font, 112, sf); objectWidth = (int)stringSize1.Width + 1; objectHeight = (int)stringSize1.Height + 1; rf1 = new Rectangle(new Point(12, 136 + nHeight + 8), new Size(objectWidth, objectHeight)); g.DrawString(objectCode, font, b, rf1, sf); } g.Dispose(); GC.Collect(); System.IO.MemoryStream ms = new System.IO.MemoryStream(); bg.Save(ms, System.Drawing.Imaging.ImageFormat.Png); //将画布bg(图片bg)保存到指定路径 path = Server.MapPath("Images"); bg.Save(path + "\\photoName.png", System.Drawing.Imaging.ImageFormat.Png); codeImage.Dispose(); bg.Dispose(); return ms; }
上述就是小编为大家分享的怎么在C#中使用QRCode动态生成自定义二维码图片了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。