[转载]C#版二维码生成器附皮肤下载 - 狼性法则 - 博客园

[转载]C#版二维码生成器附皮肤下载 – 狼性法则 – 博客园.

前言

本文所使用的二维码生成代码是谷歌开源的条形码图像处理库完成的,C#版的代码可去https://code.google.com/p/zxing/downloads/list下载压缩包。

截止目前为止最新版本为2.2,提供以下编码格式的支持:

  • UPC-A and UPC-E
  • EAN-8 and EAN-13
  • Code 39
  • Code 93
  • Code 128
  • QR Code
  • ITF
  • Codabar
  • RSS-14 (all variants)
  • Data Matrix
  • PDF 417 (‘alpha’ quality)
  • Aztec (‘alpha’ quality)

同时官网提供了 Android、cpp、C#、iPhone、j2me、j2se、jruby、objc、rim、symbian等多种应用的类库,具体详情可以参考下载的源码包中。

本文已经下载好了,C#版的zxing代码,并编译生成了类库,供程序直接调用。

软件截图及说明

功能说明:1、根据文字输入及时生成二维码,支持保存,打印

2、支持解码,打开二维码文件解码

3、采用IrisSkin4皮肤美化

代码说明

复制代码
 1 public MainForm()
 2         {
 3             InitializeComponent();
 4             this.skinEngine1.SkinFile = Application.StartupPath + "//skins//SportsGreen.ssk";
 5             this.p1.Visible = true;
 6             //this.p2.Visible = false;
 7 
 8             //用于实现PictureBox的鼠标移动过去的提示
 9             ToolTip toolTip = new ToolTip();
10             toolTip.AutoPopDelay = 5000;//一下4个都是属性
11             toolTip.InitialDelay = 1000;
12             toolTip.ReshowDelay = 500;
13             toolTip.ShowAlways = true;
14             toolTip.SetToolTip(this.open, "打开");//参数1是button名,参数2是要显示的内容
15             toolTip.SetToolTip(this.save, "保存");
16             toolTip.SetToolTip(this.print,"打印二维码");
17             toolTip.SetToolTip(this.decod,"解码");
18             toolTip.SetToolTip(this.exit,"返回生成二维码");
19         }
复制代码

主窗体显示,皮肤,按钮图表鼠标移动显示提示等代码。

复制代码
 1 //随文本框输入内容生成二维码图片
 2         private void txtContent_TextChanged(object sender, EventArgs e)
 3         {
 4             string content = txtContent.Text;
 5             if (content == "")
 6             {
 7                 MessageBox.Show("请输入内容", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
 8                 return;
 9             }
10             ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 200, 200);
11             Bitmap bitmap = toBitmap(byteMatrix);
12             orpic.Image = bitmap;
13             
14         }
15         //生成图片代码
16         public static Bitmap toBitmap(ByteMatrix matrix)
17         {
18             int width = matrix.Width;
19             int height = matrix.Height;
20             Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
21             for (int x = 0; x < width; x++)
22             {
23                 for (int y = 0; y < height; y++)
24                 {
25                     bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));
26                 }
27             }
28             return bmap;
29         }
30         //时间显示代码
31         private void timer_Tick(object sender, EventArgs e)
32         {
33             this.timeshowlable.Text = DateTime.Now.ToString();
34         }
复制代码

随文本框输入内容生成二维码图片及时间显示代码。

复制代码
 1 private void exit_MouseMove(object sender, MouseEventArgs e)
 2         {
 3             System.Drawing.Bitmap map = new Bitmap(Application.StartupPath+@"\image\exit-1.png");
 4             this.exit.Image = map;
 5         }
 6 
 7         private void exit_MouseLeave(object sender, EventArgs e)
 8         {
 9             System.Drawing.Bitmap map = new Bitmap(Application.StartupPath + @"\image\exit.png");
10             this.exit.Image = map;
11         }
12 
13         private void print_MouseMove(object sender, MouseEventArgs e)
14         {
15             System.Drawing.Bitmap map = new Bitmap(Application.StartupPath + @"\image\print-1.png");
16             this.print.Image = map;
17         }
18 
19         private void print_MouseLeave(object sender, EventArgs e)
20         {
21             System.Drawing.Bitmap map = new Bitmap(Application.StartupPath + @"\image\print.png");
22             this.print.Image = map;
23         }
24 
25         private void open_MouseLeave(object sender, EventArgs e)
26         {
27             System.Drawing.Bitmap map = new Bitmap(Application.StartupPath + @"\image\open.png");
28             this.open.Image = map;
29         }
30 
31         private void open_MouseMove(object sender, MouseEventArgs e)
32         {
33             System.Drawing.Bitmap map = new Bitmap(Application.StartupPath + @"\image\open-1.png");
34             this.open.Image = map;
35         }
36 
37         private void save_MouseLeave(object sender, EventArgs e)
38         {
39             System.Drawing.Bitmap map = new Bitmap(Application.StartupPath + @"\image\save.png");
40             this.save.Image = map;
41         }
42 
43         private void save_MouseMove(object sender, MouseEventArgs e)
44         {
45             System.Drawing.Bitmap map = new Bitmap(Application.StartupPath + @"\image\save-1.png");
46             this.save.Image = map;
47         }
48 
49         private void decod_MouseMove(object sender, MouseEventArgs e)
50         {
51             System.Drawing.Bitmap map = new Bitmap(Application.StartupPath + @"\image\encoding-1.png");
52             this.decod.Image = map;
53         }
54 
55         private void decod_MouseLeave(object sender, EventArgs e)
56         {
57             System.Drawing.Bitmap map = new Bitmap(Application.StartupPath + @"\image\encoding.png");
58             this.decod.Image = map;
59         }
复制代码

鼠标移动到按钮图片,图片切换代码。

复制代码
  1 private void open_Click(object sender, EventArgs e)
  2         {
  3             if (p1.Visible == true)
  4             {
  5                 if (this.openFileDialog1.ShowDialog() != DialogResult.OK)
  6                 {
  7                     return;
  8                 }
  9                 string path = this.openFileDialog1.FileName;
 10                 openFileDialog1.DefaultExt = "*.txt";
 11                 openFileDialog1.Filter = "*.txt|";
 12                 if (path.EndsWith(".txt"))
 13                 {
 14                     string content = File.ReadAllText(path, Encoding.Default);
 15                     this.txtContent.Text = content;
 16                 }
 17                 else
 18                 {
 19                     MessageBox.Show("只能读取txt文件","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
 20                 }
 21             }
 22             else
 23             {
 24                 if (this.openFileDialog1.ShowDialog() != DialogResult.OK)
 25                 {
 26                     return;
 27                 }
 28                 string path = this.openFileDialog1.FileName;
 29                 if (!path.EndsWith(".png"))
 30                 {
 31                     MessageBox.Show("图像格式不正确,只能读取png文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
 32                 }
 33                 else
 34                 {
 35                     Image img = Image.FromFile(path);
 36                     Bitmap bmap;
 37                     try
 38                     {
 39                         bmap = new Bitmap(img);
 40                     }
 41                     catch (System.IO.IOException ioe)
 42                     {
 43                         MessageBox.Show(ioe.ToString());
 44                         return;
 45                     }
 46                     if (bmap == null)
 47                     {
 48                         MessageBox.Show("Could not decode image");
 49                         return;
 50                     }
 51                     else
 52                     {
 53                         this.picOpenQr.Image = bmap;
 54                     }
 55                     LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);
 56                     com.google.zxing.BinaryBitmap bitmap = new com.google.zxing.BinaryBitmap(new com.google.zxing.common.HybridBinarizer(source));
 57                     Result result;
 58                     try
 59                     {
 60                         result = new MultiFormatReader().decode(bitmap);
 61                     }
 62                     catch (ReaderException re)
 63                     {
 64                         MessageBox.Show(re.ToString());
 65                         return;
 66                     }
 67 
 68                     this.txtDencoder.Text = (result.Text);
 69                 }
 70             }
 71         }
 72 
 73         private void save_Click(object sender, EventArgs e)
 74         {
 75             if (p1.Visible == true)
 76             {
 77                 saveFileDialog1.Filter = "*.png|";
 78                 //saveFileDialog1.DefaultExt = "*.png";
 79                 saveFileDialog1.RestoreDirectory = true;
 80                 if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
 81                 {
 82                     string localPath = saveFileDialog1.FileName.ToString();
 83                     Image qrcode = orpic.Image;
 84                     if (qrcode == null)
 85                     {
 86                         MessageBox.Show("无保存项", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
 87                     }
 88                     else
 89                     {
 90                         if (localPath.EndsWith(".png"))
 91                         {
 92                             qrcode.Save(localPath);
 93                         }
 94                         else
 95                         {
 96                             qrcode.Save(localPath + ".png");
 97                         }
 98 
 99                     }
100                 }
101             }
102             else
103             {
104                 saveFileDialog1.Filter = "*.txt|";
105                 //saveFileDialog1.DefaultExt = "*.txt";
106                 if(this.saveFileDialog1.ShowDialog()==DialogResult.OK)
107                 {
108                     string localPath = saveFileDialog1.FileName.ToString()+".txt";
109                     if (File.Exists(localPath))
110                     {
111                         DialogResult RESULT = MessageBox.Show("是否确认覆盖原有文件?", "信息提示", MessageBoxButtons.YesNo);
112                         if (RESULT.ToString().Equals("Yes"))
113                         {
114                             File.Delete(localPath);
115                             File.AppendAllText(localPath, this.txtDencoder.Text, Encoding.Default);
116                         }
117                         else
118                         {
119                             return;
120                         }
121                     }
122                     else
123                     {
124                         File.AppendAllText(localPath, this.txtDencoder.Text, Encoding.Default);
125                     }
126                 }
127             }
128         }
129 
130         private void exit_Click(object sender, EventArgs e)
131         {
132             this.p2.Visible = false;
133             this.p1.Visible=true;
134         }
135 
136         private void decod_Click(object sender, EventArgs e)
137         {
138             this.p2.Visible=true;
139             this.p1.Visible = !(p1.Visible);
140         }
复制代码

打开,保存,面板切换代码。

复制代码
 1 private void print_Click(object sender, EventArgs e)
 2         {
 3             PrintDialog printDialog = new PrintDialog();
 4             printDialog.Document = printDocument;
 5             if (printDialog.ShowDialog() == DialogResult.OK) { try
 6                 {
 7                     printDocument.Print();
 8                 }
 9                 catch
10                 {   //停止打印
11                     printDocument.PrintController.OnEndPrint(printDocument, new System.Drawing.Printing.PrintEventArgs());
12                 }
13             }
14         }
15 
16         private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
17         {
18             if (this.p1.Visible == true)
19             {
20                 e.Graphics.DrawImage(this.orpic.Image, 20, 20);
21             }
22             else
23             {
24                 e.Graphics.DrawImage(this.picOpenQr.Image, 20, 20);
25             }
26         }
复制代码

打印代码。

关键代码在txtContent_TextChanged()及打开方法中处理了。

最后说明:程序比较简单,不做过多说明,时间仓促代码存在很多不合理的地方,欢迎拍砖。

附件下载(带皮肤):点我去下载

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏