程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#版二維碼生成器附皮膚下載

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皮膚美 化

代碼說明

public MainForm()
        {
            InitializeComponent();
            this.skinEngine1.SkinFile = Application.StartupPath + "//skins//SportsGreen.ssk";
            this.p1.Visible = true;
            //this.p2.Visible = false;
    
            //用於實現PictureBox的鼠標移動過去的提示
            ToolTip toolTip = new ToolTip();
            toolTip.AutoPopDelay = 5000;//一下4個都是屬性
            toolTip.InitialDelay = 1000;
            toolTip.ReshowDelay = 500;
            toolTip.ShowAlways = true;
            toolTip.SetToolTip(this.open, "打開");//參數1是button名,參數2是要顯示的內容
            toolTip.SetToolTip(this.save, "保存");
            toolTip.SetToolTip(this.print,"打印二維碼");
            toolTip.SetToolTip(this.decod,"解碼");
            toolTip.SetToolTip(this.exit,"返回生成二維碼");
        }

主窗體顯示,皮膚,按鈕圖表鼠標移動顯示提示等代碼。

//隨文本框輸入內容

生成二維碼圖片
        private void txtContent_TextChanged(object sender, EventArgs e)
        {
            string content = txtContent.Text;
            if (content == "")
            {
                MessageBox.Show("請輸入內容", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 200, 200);
            Bitmap bitmap = toBitmap(byteMatrix);
            orpic.Image = bitmap;
                
        }
        //生成圖片代碼
        public static Bitmap toBitmap(ByteMatrix matrix)
        {
            int width = matrix.Width;
            int height = matrix.Height;
            Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));
                }
            }
            return bmap;
        }
        //時間顯示代碼
        private void timer_Tick(object sender, EventArgs e)
        {
            this.timeshowlable.Text = DateTime.Now.ToString();
        }

隨文本框輸入內容生成二維碼圖片及時間顯示代碼。

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 }

鼠標移動到按鈕圖片,圖片切換代碼。

private void open_Click(object sender, 

EventArgs e)
        {
            if (p1.Visible == true)
            {
                if (this.openFileDialog1.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                string path = this.openFileDialog1.FileName;
                openFileDialog1.DefaultExt = "*.txt";
                openFileDialog1.Filter = "*.txt|";
                if (path.EndsWith(".txt"))
                {
                    string content = File.ReadAllText(path, Encoding.Default);
                    this.txtContent.Text = content;
                }
                else
                {
                    MessageBox.Show("只能讀取txt文件","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                }
            }
            else
            {
                if (this.openFileDialog1.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                string path = this.openFileDialog1.FileName;
                if (!path.EndsWith(".png"))
                {
                    MessageBox.Show("圖像格式不正確,只能讀取png文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    Image img = Image.FromFile(path);
                    Bitmap bmap;
                    try
                    {
                        bmap = new Bitmap(img);
                    }
                    catch (System.IO.IOException ioe)
                    {
                        MessageBox.Show(ioe.ToString());
                        return;
                    }
                    if (bmap == null)
                    {
                        MessageBox.Show("Could not decode image");
                        return;
                    }
                    else
                    {
                        this.picOpenQr.Image = bmap;
                    }
                    LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);
                    com.google.zxing.BinaryBitmap bitmap = new com.google.zxing.BinaryBitmap(new com.google.zxing.common.HybridBinarizer(source));
                    Result result;
                    try
                    {
                        result = new MultiFormatReader().decode(bitmap);
                    }
                    catch (ReaderException re)
                    {
                        MessageBox.Show(re.ToString());
                        return;
                    }
    
                    this.txtDencoder.Text = (result.Text);
                }
            }
        }
    
        private void save_Click(object sender, EventArgs e)
        {
            if (p1.Visible == true)
            {
                saveFileDialog1.Filter = "*.png|";
                //saveFileDialog1.DefaultExt = "*.png";
                saveFileDialog1.RestoreDirectory = true;
                if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string localPath = saveFileDialog1.FileName.ToString();
                    Image qrcode = orpic.Image;
                    if (qrcode == null)
                    {
                        MessageBox.Show("無保存項", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        if (localPath.EndsWith(".png"))
                        {
                            qrcode.Save(localPath);
                        }
                        else
                        {
                            qrcode.Save(localPath + ".png");
                        }
    
                    }
                }
            }
            else
            {
                saveFileDialog1.Filter = "*.txt|";
                //saveFileDialog1.DefaultExt = "*.txt";
                if(this.saveFileDialog1.ShowDialog()==DialogResult.OK)
                {
                    string localPath = saveFileDialog1.FileName.ToString()+".txt";
                    if (File.Exists(localPath))
                    {
                        DialogResult RESULT = MessageBox.Show("是否確認覆蓋原有文件?", "信息提示", MessageBoxButtons.YesNo);
                        if (RESULT.ToString().Equals("Yes"))
                        {
                            File.Delete(localPath);
                            File.AppendAllText(localPath, this.txtDencoder.Text, Encoding.Default);
                        }
                        else
                        {
                            return;
                        }
                    }
                    else
                    {
                        File.AppendAllText(localPath, this.txtDencoder.Text, Encoding.Default);
                    }
                }
            }
        }
    
        private void exit_Click(object sender, EventArgs e)
        {
            this.p2.Visible = false;
            this.p1.Visible=true;
        }
    
        private void decod_Click(object sender, EventArgs e)
        {
            this.p2.Visible=true;
            this.p1.Visible = !(p1.Visible);
        }

打開,保存,面板切換代碼。

private void print_Click(object sender, EventArgs e)
        {
            PrintDialog printDialog = new PrintDialog();
            printDialog.Document = printDocument;
            if (printDialog.ShowDialog() == DialogResult.OK) { try
                {
                    printDocument.Print();
                }
                catch
                {   //停止打印
                    printDocument.PrintController.OnEndPrint(printDocument, new System.Drawing.Printing.PrintEventArgs());
                }
            }
        }
    
        private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            if (this.p1.Visible == true)
            {
                e.Graphics.DrawImage(this.orpic.Image, 20, 20);
            }
            else
            {
                e.Graphics.DrawImage(this.picOpenQr.Image, 20, 20);
            }
        }

打印代碼。

關鍵代碼在txtContent_TextChanged()及打開方法中處理了。

最後說明:程序比較簡單,不做過多說明,時間倉促代碼存在很多不合理的地方,歡迎拍磚。

附件下 載(帶皮膚):http://down.51cto.com/data/888138/p>

源碼下載: http://files.cnblogs.com/flykai/GoogleQRCode.zip

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved