欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

根据内容来产生一个二维码

程序员文章站 2022-04-14 10:56:51
实现的功能以及效果如下: 样式代码: .divtable { display: table; border-collapse: collapse; border-spacing: 0; margin-right: auto; margin-left: auto; width: 100%; } .di ......

实现的功能以及效果如下:

 

样式代码:

 

根据内容来产生一个二维码
.divtable {
    display: table;
    border-collapse: collapse;
    border-spacing: 0;
    margin-right: auto;
    margin-left: auto;
    width: 100%;
}

.divth, .divtr {
    display: table-row;
    vertical-align: middle;
    height: 20px;
    padding-left: 2px;
    padding-right: 2px;
}

.divth {
    background-color: #efebde;
    text-align: center;
}

.divtd {
    display: table-cell;
    border: 1px solid #c0c0c0;
}
source code

 

前端asp.net 代码:

 

根据内容来产生一个二维码
<div class="divtable" >
                    <div class="divtr">
                        <div class="divtd">
                            内容
                        </div>
                        <div class="divtd" >
                            <asp:textbox id="textboxqrcode" runat="server" width="500" textmode="multiline" rows="20"></asp:textbox>
                        </div>
                    </div>
                    <div class="divtr"  style="line-height:40px;" >
                        <div class="divtd">
                           尺寸
                        </div>
                        <div class="divtd">
                            宽度:<asp:textbox id="textboxwidth" runat="server" text="300"></asp:textbox>高度:<asp:textbox id="textboxheight" runat="server" text="300"></asp:textbox>
                        </div>
                    </div>
                    <div class="divtr"  style="line-height:40px;" >
                        <div class="divtd">
                           
                        </div>
                        <div class="divtd">
                            <asp:button id="buttongenerate" runat="server" text="生成" onclick="buttongenerate_click" />
                        </div>
                    </div>
                    <div class="divtr"  style="line-height:120px;" >
                        <div class="divtd">
                           二维码
                        </div>
                        <div class="divtd">
                            <asp:placeholder id="placeholderbarcode" runat="server" />
                        </div>
                    </div>
                </div>
source code

 

把qrcoder.dll类库引入bin目录中去:

然后在aspx.cs引用命名空间:

using qrcoder;
using system.io;
using system.drawing;

 
onclick事件:

 

根据内容来产生一个二维码
protected void buttongenerate_click(object sender, eventargs e)
    {
        string code = this.textboxqrcode.text;
        qrcodegenerator qrgenerator = new qrcodegenerator();
        qrcodegenerator.qrcode qrcode = qrgenerator.createqrcode(code, qrcodegenerator.ecclevel.q);
        system.web.ui.webcontrols.image imagebarcode = new system.web.ui.webcontrols.image();
        imagebarcode.height = string.isnullorempty(this.textboxheight.text.trim()) ? 300 : convert.toint32(this.textboxheight.text.trim());
        imagebarcode.width = string.isnullorempty(this.textboxwidth.text.trim()) ? 300 : convert.toint32(this.textboxwidth.text.trim());

        using (bitmap bitmap = qrcode.getgraphic(20))
        {
            using (memorystream ms = new memorystream())
            {
                bitmap.save(ms, system.drawing.imaging.imageformat.png);
                byte[] byteimage = ms.toarray();
                imagebarcode.imageurl = "data:image/png;base64," + convert.tobase64string(byteimage);
            }
            this.placeholderbarcode.controls.add(imagebarcode);
        }
    }
source code