zoukankan      html  css  js  c++  java
  • qrcode生成二维码图片

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using ThoughtWorks.QRCode.Codec;
    using ThoughtWorks.QRCode.Codec.Data;
    using ThoughtWorks.QRCode.Codec.Util;
    using System.IO;
    using System.Text;
    using System.Net;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    
    public partial class 测试 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Click+=Button1_Click;
        }
    
        void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                string mypath = "";
                //创建新对象
                QRCodeEncoder qr = new QRCodeEncoder();
                //字节:文字,英文,数字都可以
                qr.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
                qr.QRCodeScale = 3;//尺寸
                qr.QRCodeVersion = 5;//点数
                qr.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//容错率
    
                string data = TextBox1.Text.ToString();//要转换成二维码的信息
                System.Drawing.Bitmap img = qr.Encode(data);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                string path = mypath + "img/";//data.Trim().ToString()、Guid.NewGuid().ToString()
                string tempname = data.Trim().ToString() + ".png";//图片名称
                Directory.CreateDirectory(Server.MapPath(path));
                FileStream fs = new FileStream(Server.MapPath(path + tempname), FileMode.CreateNew, FileAccess.ReadWrite);
                BinaryWriter bw = new BinaryWriter(fs, UTF8Encoding.UTF8);
                byte[] by = ms.ToArray();
                for (int i = 0; i < ms.ToArray().Length; i++)
                {
                    bw.Write(by[i]);
                }
                fs.Close();
                Image1.Src = path + tempname;
            }
            catch { }
    
        }
    }

    bin文件夹需要添加的文件:ThoughtWorks.QRCode.dll 和 ThoughtWorks.QRCode.pdb

    前台:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ceshi.aspx.cs" Inherits="测试" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            &nbsp;&nbsp;
            <asp:Button ID="Button1" runat="server" Text="生成二维码及相关信息" OnClick="Button1_Click"/>
            <br /><br />
            <div style=" 500px; height: 200px;border:1px solid gray;">
                <img id="Image1" src="" runat="server" />
            </div>
        </form>
    </body>
    </html>

    后台:

  • 相关阅读:
    输入一个字符串,输出该字符串中字符的所有组合 C#【总结】
    Asp.net的身份验证【转载】
    C#面试基础问题【转载】
    Testing and Checking Refined
    Exploratory Testing 3.0 探索式测试
    博客搬家
    Seven Kinds of Testers - 七种类型的测试
    使用Spring的Property文件存储测试数据 - 初始化
    Protractor 怎样运行
    Protractor-引入Cucumber
  • 原文地址:https://www.cnblogs.com/123lucy/p/6420134.html
Copyright © 2011-2022 走看看