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

ASP.NET实现二维码

程序员文章站 2022-12-17 15:37:51
using System;using System.Collections.Generic;using System.Drawing;using System.Linq;using System.Text;using System.Web;using System.Web.UI;using Syst ......

using system;
using system.collections.generic;
using system.drawing;
using system.linq;
using system.text;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using thoughtworks.qrcode.codec;

namespace wechat
{
public partial class webform2 : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{

}

protected void btncrc_click(object sender, eventargs e)
{
//创建时间
datetime currenttime = new system.datetime();
//取当前年月日时分秒
currenttime = system.datetime.now;
//取当前年
int year = currenttime.year;
//取当前月
int month = currenttime.month;
//取当前日
int day = currenttime.day;
if (day < 10)
{
day = 0 + day;
}
//取当前时
int hour = currenttime.hour;
//取当前分
int minute = currenttime.minute;
int minute1 = currenttime.minute + 10;
//取当前秒
int second = currenttime.second;
//取当前毫秒
int millisecond = currenttime.millisecond;
this.txtcrc.text = second.tostring();
thoughtworksqrcode("您好欢迎使用二维码查看!");
}
private void thoughtworksqrcode(string strtxt)
{
try
{
bitmap bt;
string encodestring = strtxt;
qrcodeencoder qrcodeencoder = new qrcodeencoder();
bt = qrcodeencoder.encode(encodestring, encoding.utf8);
string filename = datetime.now.tostring("yyyymmddhhmmss");
filename = filename.replace(" ", "");
filename = filename.replace(":", "");
filename = filename.replace("-", "");
filename = filename.replace(".", "");
bt.save(server.mappath("~/images/") + filename + ".jpg");
this.image1.imageurl = "~/images/" + filename + ".jpg";
}
catch (exception e)
{
throw e;
}

}

//public void generateqrcode(string content)
//{
// try
// {
// // _filelog.info("generateqrcode|开始生成二维码");
// if (null != content)
// {
// //初始化二维码生成工具
// var qrcodeencoder = new qrcodeencoder
// {
// qrcodeencodemode = qrcodeencoder.encode_mode.byte,
// qrcodeerrorcorrect = qrcodeencoder.error_correction.m,
// qrcodeversion = 0,
// qrcodescale = 4
// };
// //将字符串生成二维码图片
// var image = qrcodeencoder.encode(content);
// var ms = new memorystream();
// image.save(ms, imageformat.png);
// response.binarywrite(ms.getbuffer());
// response.end();
// }
// //_filelog.info("cloudcardcontroller|generateqrcode|成功生成二维码");
// }
// catch (exception ex)
// {
// //_filelog.error("generateqrcode|生成二维码出现异常,异常信息:" + ex.message);
// }

//}
//private void createcode_simple(string nr)
//{
// qrcodeencoder qrcodeencoder = new qrcodeencoder();
// qrcodeencoder.qrcodeencodemode = qrcodeencoder.encode_mode.byte;
// qrcodeencoder.qrcodescale = 4;
// qrcodeencoder.qrcodeversion = 8;
// qrcodeencoder.qrcodeerrorcorrect = qrcodeencoder.error_correction.m;
// //system.drawing.image image = qrcodeencoder.encode("4408810820 深圳-广州 小江");
// system.drawing.image image = qrcodeencoder.encode(nr);
// string filename = datetime.now.tostring("yyyymmddhhmmssfff").tostring() + ".jpg";
// string filepath = server.mappath(@"~\upload") + "\\" + filename;
// system.io.filestream fs = new system.io.filestream(filepath, system.io.filemode.openorcreate, system.io.fileaccess.write);
// image.save(fs, system.drawing.imaging.imageformat.jpeg);

// fs.close();
// image.dispose();
// //二维码解码
// var codedecoder = codedecoder(filepath);
//}

///// <summary>
///// 二维码解码
///// </summary>
///// <param name="filepath">图片路径</param>
///// <returns></returns>
//public string codedecoder(string filepath)
//{
// if (!system.io.file.exists(filepath))
// return null;
// bitmap mybitmap = new bitmap(image.fromfile(filepath));
// qrcodedecoder decoder = new qrcodedecoder();
// string decodedstring = decoder.decode(new qrcodebitmapimage(mybitmap));
// return decodedstring;
//}
}
}

ASP.NET实现二维码