/// <summary>
/// 生成二维码的按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn1_Click(object sender, EventArgs e)
{
//判断输入的文本框是否为空
if (this.txt1.Text.Trim() != "")
{
//获取文本框的 字符
string ecodestring = txt1.Text;
//初始化二维码方法
QRCodeEncoder qrcode = new QRCodeEncoder();
//根据文本框的内容来生成二维码图片,计算算法是Encoding.UTF8
//UTF-8 中文字符3个字节、英文字符1字节,UTF8最多只能编码122个字节即40个汉字
picBox1.Image = qrcode.Encode(ecodestring, Encoding.UTF8);
txt2.Text = "";
}
else
{
MessageBox.Show("请输入需要转换的字符");
}
}
/// <summary>
/// 保存二维码图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn4_Click(object sender, EventArgs e)
{
if (picBox1.Image != null)
{
//用户选择保存文件的位置
SaveFileDialog save = new SaveFileDialog();
save.Title = "保存二维码图片";//标题
save.Filter = "图片文件夹(*.jpg)|*.jpg";//文件类型框中出现的内容
if (save.ShowDialog() == DialogResult.OK)
try
{
picBox1.Image.Save(save.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
MessageBox.Show("保存成功");
picBox1.Image = null;
txt1.Text = "";
}
catch
{
MessageBox.Show("保存失败");
}
}
}
/// <summary>
/// 上传二维码图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();//创建一个对话框对象
ofd.Title = "请选择上传的图片";//为对话框设置标题
ofd.Filter = "图片格式|*";//设置筛选的图片格式
ofd.Multiselect = false;//是否允许多选
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)//判断选择文件是否OK
{
string fliePath = ofd.FileName;//获取文件名
txt2.Text = fliePath;//在文本框中显示文件名
int postion = fliePath.LastIndexOf("\\");
string fileName = fliePath.Substring(postion + 1);
using (Stream stream = ofd.OpenFile())
{
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
stream.CopyTo(fs);
fs.Flush();
}
this.picBox1.ImageLocation = fileName;
}
}
}
/// <summary>
/// 将上传的二维码解码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn2_Click(object sender, EventArgs e)
{
System.Threading.Thread t = new System.Threading.Thread(ss);
t.IsBackground = true;
t.Start();
}
/// <summary>
/// 解码的方法
/// </summary>
private void ss()
{
if (txt2.Text != "")
{
string tt = "";
try
{
Invoke((EventHandler)delegate
{
btn2.Enabled = false;
btn2.Text = "Waiting......";
picBox1.Image = new Bitmap(txt2.Text);
});
QRCodeDecoder qrde = new QRCodeDecoder();
string txtmsg = qrde.decode(new QRCodeBitmapImage(new Bitmap(picBox1.Image)), Encoding.UTF8);
tt = txtmsg;
}
catch
{
tt = "识别失败";
}
Invoke((EventHandler)delegate
{
txt1.Text = tt;
btn2.Enabled = true;
btn2.Text = "解码OK";
});
}
System.Threading.Thread.CurrentThread.Abort();
}
/// <summary>
/// 生成条形码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn5_Click(object sender, EventArgs e)
{
Regex rg = new Regex("^[0-9]{12}$");
if (!rg.IsMatch(txt1.Text))
{
MessageBox.Show("本例子采用EAN_13编码,需要输入12位数字");
return;
}
try
{
EncodingOptions options = null;
BarcodeWriter writer = null;
options = new EncodingOptions
{
Width = picBox1.Width,
Height = picBox1.Height
};
writer = new BarcodeWriter();
writer.Format = BarcodeFormat.ITF;
writer.Options = options;
Bitmap bitmap = writer.Write(txt1.Text);
picBox1.Image = bitmap;
}
catch
{
MessageBox.Show("失败");
}
}
/// <summary>
/// 保存条形码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn6_Click(object sender, EventArgs e)
{
}
////////////////////////////////生成带图片的二维码////////////////////////////////////////////////////////////
private object logoImg; //用于存放logo图片
///<summary>
///获取二维码生成图片
///</summary>
///<returns></returns>
public Bitmap getcode(string writeStr)
{
QRCodeEncoder qrcode = new QRCodeEncoder();
//根据文本框的内容来生成二维码图片,计算算法是Encoding.UTF8
Bitmap image = qrcode.Encode(writeStr, Encoding.UTF8);//这是一种编码格式
//QRCodeEncoder qRCodeEncoder = new QRCodeEncoder();
//qRCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//这也是一种编码格式
//qRCodeEncoder.QRCodeVersion = 4; //设置编码测量度
//qRCodeEncoder.QRCodeVersion = 7; //设置编码版本
//qRCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder. ERROR_CORRECTION.M; //设置错误校验
//Bitmap image = qRCodeEncoder.Encode(writeStr);
return image;
}
/// <summary>
/// 生成带图片的二维码,需要先上传logoA在根据内容生成二维码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn7_Click(object sender, EventArgs e)
{
if (txt1.Text.Trim()!= "") //验证输入的生成内容是否为空
{
Bitmap bCode = getcode(txt1.Text); // 获取二维码图片
if (logoImg == null)
{ picBox1.Image = bCode; }
else
{
Bitmap bLogo = logoImg as Bitmap; //获取logo图片对象
bLogo = new Bitmap(bLogo, 50, 50); //改变图片的大小这里我们设置为30
int Y = bCode.Height;
int X = bCode.Width;
Point point = new Point(X / 2 - 25, Y / 2-30);//logo图片绘制到二维码上,这里将简单计算一下logo所在的坐标 以二维码的左上角为原点坐标
Graphics g = Graphics.FromImage(bCode);//创建一个画布
g.DrawImage(bLogo, point);//将logo图片绘制到二维码图片上
picBox1.Image = bCode;
}
}
else
{
MessageBox.Show("输入生成内容!");
}
}
/// <summary>
/// 上传Logo
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn8_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();//文件打开对话框
openFileDialog.Filter = "JPEG|*.jpeg;*.jpg|位图文件|*.bmp|所有文件|*.*";//只能打开我们设置的这几类文件
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string fileName = openFileDialog.FileName;
Bitmap newBmp = new Bitmap(fileName);
Bitmap bmp = new Bitmap(newBmp);
picBox1.Image = bmp;
logoImg = bmp;
}
}
/// <summary>
/// 清空所有
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn9_Click(object sender, EventArgs e)
{
this.txt1.Text = "";
this.txt2.Text = "";
this.picBox1.Image = null;
}