文章来自iteye:http://hzy3774.iteye.com/blog/1876392
下载地址:http://zxingnet.codeplex.com/
zxing.net是.net平台下编解条形码和二维码的工具,使用非常方便。
首先下载二进制dll文件,引入工程;
代码:
C#代码 收藏代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZXing.QrCode;
using ZXing;
using ZXing.Common;
using ZXing.Rendering;
namespace zxingTest
{
public partial class Form1 : Form
{
EncodingOptions options = null;
BarcodeWriter writer = null;
public Form1()
{
InitializeComponent();
options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = pictureBoxQr.Width,
Height = pictureBoxQr.Height
};
writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options;
}
private void buttonQr_Click(object sender, EventArgs e)
{
if (textBoxText.Text == string.Empty)
{
MessageBox.Show("输入内容不能为空!");
return;
}
Bitmap bitmap = writer.Write(textBoxText.Text);
pictureBoxQr.Image = bitmap;
}
}
}
效果:
将字符编码时可以指定字符格式;默认为ISO-8859-1英文字符集,但一般移动设备常用UTF-8字符集编码,
可以通过QrCodeEncodingOptions设置编码方式。
如果要生成其他zxing支持的条形码,只要修改BarcodeWriter.Format就可以了。
C#代码 收藏代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZXing.QrCode;
using ZXing;
using ZXing.Common;
using ZXing.Rendering;
namespace zxingTest
{
public partial class Form1 : Form
{
EncodingOptions options = null;
BarcodeWriter writer = null;
public Form1()
{
InitializeComponent();
options = new EncodingOptions
{
//DisableECI = true,
//CharacterSet = "UTF-8",
Width = pictureBoxQr.Width,
Height = pictureBoxQr.Height
};
writer = new BarcodeWriter();
writer.Format = BarcodeFormat.ITF;
writer.Options = options;
}
private void buttonQr_Click(object sender, EventArgs e)
{
if (textBoxText.Text == string.Empty)
{
MessageBox.Show("输入内容不能为空!");
return;
}
Bitmap bitmap = writer.Write(textBoxText.Text);
pictureBoxQr.Image = bitmap;
}
}
}
效果:
输入字符串需要符合编码的格式,不然会报错。
解码方式:
C#代码 收藏代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZXing.QrCode;
using ZXing;
using ZXing.Common;
using ZXing.Rendering;
namespace zxingTest
{
public partial class Form1 : Form
{
BarcodeReader reader = null;
public Form1()
{
InitializeComponent();
reader = new BarcodeReader();
}
private void Form1_DragEnter(object sender, DragEventArgs e)//当拖放进入窗体
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy; //显示拷贝效应
else
e.Effect = DragDropEffects.None;
}
private void Form1_DragDrop(object sender, DragEventArgs e) //当拖放放在窗体上
{
string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop, false); //获取文件名
if (fileNames.Length > 0)
{
pictureBoxPic.Load(fileNames[0]); //显示图片
Result result = reader.Decode((Bitmap)pictureBoxPic.Image); //通过reader解码
textBoxText.Text = result.Text; //显示解析结果
}
}
}
}
zxingDecoder.zip (401.4 KB)
下载次数: 493
zxingEncoder.zip (395.8 KB)
下载次数: 404
查看图片附件
分享到:
C#窗体程序实现文件拖放 | PHP日期时间相关date,time
2013-05-26 22:06浏览 28718评论(11)分类:编程语言相关推荐
参考知识库
Java SE知识库
20650 关注 | 468 收录
微信开发知识库
16984 关注 | 681 收录
React Native知识库
4950 关注 | 1164 收录
CSS3知识库
8671 关注 | 1693 收录
评论
11 楼 wck173168 2016-04-11
请问如何调用摄像头,谢谢
10 楼 calosteward 2015-06-17
谢了
经常看到的都是Zxing barcode 和java相关的教程。很少看到.net相关的,非常感谢你的分享。
.net barcode generation
9 楼 hzy3774 2015-01-09
abcba123 写道
请问是要把zxing。dll加入reference?还是其他。dll?
zxing.dll即可
8 楼 abcba123 2015-01-09
请问是要把zxing。dll加入reference?还是其他。dll?
7 楼 itzenghaixiong 2014-05-07
生成的二维码外面会有白色的边框,怎么把这些白色边框去掉
6 楼 恩铭-杨 2014-03-17
我是看不懂滴,只会写AU3的东东!但是AU3没有脱机的二维码编译。55555555
5 楼 恩铭-杨 2014-03-17
我睡不着觉的时候,找来的!
4 楼 恩铭-杨 2014-03-17
using com.google.zxing;
using com.google.zxing.qrcode;
using com.google.zxing.qrcode.decoder;
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
public class b : Form
{
private IContainer a;
private Button b;
private TextBox c;
private PictureBox d;
private Button e;
private SaveFileDialog f;
private TextBox g;
private TextBox h;
private Label i;
private Label j;
private Label k;
public b()
{
this.a();
}
private void a()
{
this.b = new Button();
this.c = new TextBox();
this.d = new PictureBox();
this.e = new Button();
this.f = new SaveFileDialog();
this.g = new TextBox();
this.h = new TextBox();
this.i = new Label();
this.j = new Label();
this.k = new Label();
((ISupportInitialize) this.d).BeginInit();
base.SuspendLayout();
this.b.Location = new Point(0x1a0, 0x42);
this.b.Name = "button1";
this.b.Size = new Size(0x77, 0x2a);
this.b.TabIndex = 0;
this.b.Text = "生成二维码(&C)";
this.b.UseVisualStyleBackColor = true;
this.b.Click += new EventHandler(this.b);
this.c.Location = new Point(120, 30);
this.c.MaxLength = 0x400;
this.c.Name = "textBox1";
this.c.Size = new Size(0x12d, 0x15);
this.c.TabIndex = 1;
this.c.Text = "http://blog.csdn.net/kimmking";
this.d.BackColor = Color.White;
this.d.Location = new Point(30, 0x77);
this.d.Name = "pictureBox1";
this.d.Size = new Size(0x200, 0x200);
this.d.TabIndex = 2;
this.d.TabStop = false;
this.e.Location = new Point(450, 0x16);
this.e.Name = "button2";
this.e.Size = new Size(0x55, 0x22);
this.e.TabIndex = 3;
this.e.Text = "保存(&S)";
this.e.UseVisualStyleBackColor = true;
this.e.Click += new EventHandler(this.a);
this.f.Filter = "bmp文件|*.bmp|jpg文件|*.jpg|所有文件|*.*";
this.f.Title = "保存二维码图片";
this.f.FileOk += new CancelEventHandler(this.a);
this.g.Location = new Point(0x7b, 0x4b);
this.g.MaxLength = 20;
this.g.Name = "textBox2";
this.g.Size = new Size(0x62, 0x15);
this.g.TabIndex = 4;
this.g.Text = "我的博客";
this.h.Location = new Point(0x121, 0x4b);
this.h.MaxLength = 8;
this.h.Name = "textBox3";
this.h.Size = new Size(0x62, 0x15);
this.h.TabIndex = 5;
this.h.Text = "KK";
this.i.AutoSize = true;
this.i.Location = new Point(0x2b, 0x21);
this.i.Name = "label1";
this.i.Size = new Size(0x4d, 12);
this.i.TabIndex = 6;
this.i.Text = "网址或内容:";
this.j.AutoSize = true;
this.j.Location = new Point(0x2b, 0x4f);
this.j.Name = "label2";
this.j.Size = new Size(0x29, 12);
this.j.TabIndex = 7;
this.j.Text = "标题:";
this.k.AutoSize = true;
this.k.Location = new Point(0xf2, 0x4f);
this.k.Name = "label3";
this.k.Size = new Size(0x29, 12);
this.k.TabIndex = 8;
this.k.Text = "标题:";
base.AutoScaleDimensions = new SizeF(6f, 12f);
base.AutoScaleMode = AutoScaleMode.Font;
base.ClientSize = new Size(0x23c, 0x291);
base.Controls.Add(this.k);
base.Controls.Add(this.j);
base.Controls.Add(this.i);
base.Controls.Add(this.h);
base.Controls.Add(this.g);
base.Controls.Add(this.e);
base.Controls.Add(this.d);
base.Controls.Add(this.c);
base.Controls.Add(this.b);
base.Name = "Form1";
this.Text = "彩色二维码生成器";
((ISupportInitialize) this.d).EndInit();
base.ResumeLayout(false);
base.PerformLayout();
}
private static Bitmap a(int A_0, int A_1)
{
Bitmap image = new Bitmap(A_0, A_1, PixelFormat.Format32bppArgb);
Rectangle rect = new Rectangle(0, 0, A_0, A_1);
LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(230, 0x23, 0xa9, 160), Color.FromArgb(0xff, 8, 60, 0x63), LinearGradientMode.Vertical);
Graphics graphics = Graphics.FromImage(image);
graphics.FillRectangle(brush, rect);
graphics.Dispose();
return image;
}
private void a(object A_0, CancelEventArgs A_1)
{
}
private void a(object A_0, EventArgs A_1)
{
if (this.d.Image == null)
{
MessageBox.Show("请先生成二维码!!!", "图片保存失败");
}
else if (this.f.ShowDialog() == DialogResult.OK)
{
string filename = this.f.FileName.ToString();
this.d.Image.Save(filename);
MessageBox.Show("成功保存图片到" + filename + "!", "图片保存成功");
}
}
private void b(object A_0, EventArgs A_1)
{
string text = this.c.Text;
if (text.Length <
{
text = "http://blog.csdn.net/kimmking";
}
QRCodeWriter writer = new QRCodeWriter();
Hashtable hints = new Hashtable();
hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.Add(EncodeHintType.VERSION_START, 5);
Bitmap image = writer.encode(text, BarcodeFormat.QR_CODE, 0x200, 0x200, hints).ToBitmap();
Bitmap bitmap2 = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(bitmap2);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.DrawImage(image, 0, 0);
image.Dispose();
SizeF ef = new SizeF();
Bitmap bitmap3 = a(bitmap2.Width, bitmap2.Height);
Color color = Color.FromArgb(200, 0xe0, 0x72, 1);
int num = 140;
try
{
num -= (Encoding.UTF8.GetBytes(text).Length - 20) / 2;
}
catch (Exception)
{
}
int num2 = num;
int num3 = num2;
for (int i = 0; i < bitmap2.Width; i++)
{
for (int j = 0; j < bitmap2.Height; j++)
{
Color color3;
Color pixel = bitmap2.GetPixel(i, j);
if ((i < num2) && (j < num3))
{
color3 = ((pixel.A == 0xff) && (pixel.B == 0)) ? color : pixel;
}
else
{
color3 = ((pixel.A == 0xff) && (pixel.B == 0)) ? bitmap3.GetPixel(i, j) : pixel;
}
bitmap2.SetPixel(i, j, color3);
}
}
bitmap3.Dispose();
string str2 = this.g.Text;
if (str2.Length < 1)
{
str2 = "我的博客";
}
float emSize = 32f;
emSize -= (str2.Length - 4) * 1.8f;
Font font = new Font("微软雅黑", emSize, FontStyle.Bold);
ef = graphics.MeasureString(str2, font);
float num7 = (bitmap2.Width - ef.Width) / 2f;
Brush brush = new SolidBrush(Color.FromArgb(0xff, 0x3a, 0xb2, 0xc2));
Brush brush2 = new SolidBrush(Color.White);
int y = 50;
graphics.FillRectangle(brush2, new Rectangle((int) num7, y, (int) ef.Width, (int) ef.Height));
graphics.DrawString(str2, font, brush, (float) ((int) num7), (float) y);
Brush brush3 = new SolidBrush(Color.FromArgb(0xff, 0x3a, 0xb2, 0xc2));
int width = 140;
graphics.FillEllipse(brush2, (bitmap2.Width - width) / 2, (bitmap2.Height - width) / 2, width, width);
int num10 = 0x80;
graphics.FillEllipse(brush3, (bitmap2.Width - num10) / 2, (bitmap2.Height - num10) / 2, num10, num10);
int num11 = 110;
graphics.FillEllipse(brush2, (bitmap2.Width - num11) / 2, (bitmap2.Height - num11) / 2, num11, num11);
str2 = this.h.Text;
if (str2.Length < 1)
{
str2 = "KK";
}
float num12 = 32f;
num12 -= (str2.Length - 3) * 3.5f;
Font font2 = new Font("Meiryo", num12, FontStyle.Bold);
ef = graphics.MeasureString(str2, font2);
float x = ((bitmap2.Width - ef.Width) / 2f) + 2f;
float num14 = ((bitmap2.Height - ef.Height) / 2f) + 8f;
graphics.DrawString(str2, font2, brush3, x, num14);
graphics.Dispose();
if (this.d.Image != null)
{
this.d.Image.Dispose();
}
this.d.Height = bitmap2.Height;
this.d.Width = bitmap2.Width;
this.d.Image = bitmap2;
}
protected override void Dispose(bool disposing)
{
if (disposing && (this.a != null))
{
this.a.Dispose();
}
base.Dispose(disposing);
}
}