基本信息
源码名称:C# 生成以及读取 条形码/二维码 示例源码(zxing库)
源码大小:2.85M
文件格式:.zip
开发语言:C#
更新时间:2018-07-23
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXing;
using ZXing.Common;

namespace ZXingCode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //生成条形码-生成
        private void button1_Click(object sender, EventArgs e)
        {
            //设置条形码规格
            EncodingOptions encodeOption = new EncodingOptions();
            //设置宽和高
            encodeOption.Height = 130;
            encodeOption.Width = 240;
            BarcodeWriter wr = new BarcodeWriter();
            wr.Options = encodeOption;
            //条形码:根据自己的需要选择条形码格式
            wr.Format = BarcodeFormat.EAN_13;
            //生成条形码
            Bitmap image = wr.Write(textBox1.Text);
            //显示
            pictureBox1.Image = image;
        }

        //生成条形码-保存
        private void button6_Click(object sender, EventArgs e)
        {
            //保存图片
            saveImage(pictureBox1, textBox1.Text);
        }

        //读取条形码-选择图片
        private void button2_Click(object sender, EventArgs e)
        {
            //打开图片
            openImage(textBox2, pictureBox2);
        }

        //读取条形码-读取
        private void button8_Click(object sender, EventArgs e)
        {
            DecodingOptions decodeOption = new DecodingOptions();
            decodeOption.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.EAN_13 };
            //读取条形码
            BarcodeReader br = new BarcodeReader();
            br.Options = decodeOption;
            Result result = br.Decode(pictureBox2.Image as Bitmap);
            if (result == null)
            {
                MessageBox.Show("读取失败");
            }
            else
            {
                //读取成功
                textBox3.Text = result.Text;
            }
        }

        //生成二维码-生成
        private void button3_Click(object sender, EventArgs e)
        {
            //设置QR二维码的规格
            ZXing.QrCode.QrCodeEncodingOptions qrEncodeOption = new ZXing.QrCode.QrCodeEncodingOptions();
            //设置编码格式,否则中文乱码
            qrEncodeOption.CharacterSet = "UTF-8";
            //设置宽和高
            qrEncodeOption.Height = 200;
            qrEncodeOption.Width = 200;
            //设置周围空白边距
            qrEncodeOption.Margin = 1;
            ZXing.BarcodeWriter wr = new BarcodeWriter();
            //二维码
            wr.Format = BarcodeFormat.QR_CODE;
            wr.Options = qrEncodeOption;
            //生成二维码
            Bitmap image = wr.Write(textBox4.Text);
            //显示
            pictureBox3.Image = image;
        }

        //生成二维码-保存
        private void button7_Click(object sender, EventArgs e)
        {
            //保存图片
            saveImage(pictureBox3, textBox4.Text);
        }

        //读取二维码-选择图片
        private void button5_Click(object sender, EventArgs e)
        {
            //打开图片
            openImage(textBox6, pictureBox4);
        }

        //读取二维码-读取
        private void button9_Click(object sender, EventArgs e)
        {
            DecodingOptions decodeOption = new DecodingOptions();
            decodeOption.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE };
            //读取二维码
            ZXing.BarcodeReader br = new BarcodeReader();
            br.Options = decodeOption;
            ZXing.Result rs = br.Decode(pictureBox4.Image as Bitmap);
            if (rs == null)
            {
                MessageBox.Show("读取失败");
            }
            else
            {
                //读取成功
                textBox7.Text = rs.Text;
            }
        }

        //保存图片
        private void saveImage(PictureBox imagePb, string filename)
        {
            try
            {
                SaveFileDialog fileDialog = new SaveFileDialog();
                fileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);  //默认保存目录
                fileDialog.RestoreDirectory = true;  //对话框记忆之前打开的目录
                fileDialog.Filter = "图片文件(*.jpg)|*.jpg";  //显示的文件类型
                fileDialog.FileName = filename   ".jpg";    //默认文件名
                if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string filePath = fileDialog.FileName;
                    imagePb.Image.Save(filePath);   //保存
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(filename   " 保存失败: "   ex.Message);
            }
        }

        //打开图片
        private void openImage(TextBox textBox, PictureBox imagePb)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);  //默认打开目录
            ofd.Filter = "图片文件|*.jpg|所有文件|*.*";  //显示的文件类型
            ofd.RestoreDirectory = true;  //对话框记忆之前打开的目录
            ofd.FilterIndex = 1;  //文件类型的显示顺序
            if (ofd.ShowDialog() == DialogResult.OK)  //选中文件
            {
                textBox.Text = ofd.FileName; //选中的音乐文件的路径
                imagePb.Image = new Bitmap(ofd.FileName);
            }
        }

    }
}