首页 > 代码库 > winfrom编写二维码(可上传图片)

winfrom编写二维码(可上传图片)

编写一个个人信息二维码:

首先先下载一个生成二维码的ThoughtWorks.QRCode.dll(注:下载后将其添加引用到项目中即可)

然后设计一个窗体:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThoughtWorks.QRCode.Codec;

namespace Two_dimensionCode
{
    public partial class MyselfInfo : Form
    {
        public MyselfInfo()
        {
            InitializeComponent();
        }

        /// <summary>
        /// /获取二维码
        /// </summary>
        /// <param name="write"></param>
        /// <returns></returns>
        public Bitmap GetCode(string write)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("姓名:" + this.txtName);
            QRCodeEncoder code = new QRCodeEncoder();
            code.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            code.QRCodeScale = 4;
            code.QRCodeVersion = 8;
            code.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            Bitmap image = code.Encode(write);
            return image;
        }

        ///<summary>
        ///获取logo
        ///</summary>
        ///<returns></returns>
        public Bitmap getlogo()
        {
            Bitmap newBmp = new Bitmap("logo.jpg");//获取图片对象
            Bitmap bmp = new Bitmap(newBmp, 30, 30);//缩放
            return bmp;
        }

        private object logoImg;//用于存放logo图片

        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() != "")//验证输入的生成内容是否为空
            {
                Bitmap bCode = GetCode(this.txtName.Text);//获取二维码图片
                if (logoImg == null)
                {
                    pbCode.Image = bCode;
                }
                else
                {
                    Bitmap bLogo = logoImg as Bitmap;//获取logo图片对象
                    bLogo = new Bitmap(bLogo, 30, 30);//改变图片的大小这里我们设置为30
                    int Y = bCode.Height;
                    int X = bCode.Width;
                    Point point = new Point(X / 2 - 15, Y / 2 - 15);//logo图片绘制到二维码上,这里将简单计算一下logo所在的坐标
                    Graphics g = Graphics.FromImage(bCode);//创建一个画布
                    g.DrawImage(bLogo, point);//将logo图片绘制到二维码图片上
                    pbCode.Image = bCode;
                }
            }
            else
            {
                MessageBox.Show("输入生成内容!");
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFile = new SaveFileDialog();//创建保存对话框
            saveFile.Filter = "JPEG|*.jpeg;*.jpg|位图文件|*.bmp|所有文件|*.*";//设置保存的图片格式
            if (pbCode.Image != null)
            {
                if (saveFile.ShowDialog() == DialogResult.OK)
                {
                    string sFilePathName = saveFile.FileName;
                    Image img = pbCode.Image;
                    img.Save(sFilePathName);
                }
            }
            else
            {
                MessageBox.Show("请先生成二维码!");
            }
        }

        /// <summary>
        /// 上传Logo
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogo_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);
                pbCode.Image = bmp;
                logoImg = bmp;
            }
        }
    }
}

接下来创建一个QRCodeBuilder.cs类:

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using ThoughtWorks.QRCode.Codec;

namespace TianQRCode
{
    public class QRCodeBuilder
    {
        /// <summary>
        /// 创建二维码图片
        /// </summary>
        /// <param name="codeStr">要产生二维码的字符串</param>
        /// <returns>返回生成的Image</returns>
        public Image CreateQRCode(string codeStr)
        {
            QRCodeEncoder encoder = new QRCodeEncoder();
            encoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//编码方式
            encoder.QRCodeScale = 4;//大小
            encoder.QRCodeVersion = 8;//版本
            encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            System.Drawing.Bitmap bp = encoder.Encode(codeStr, Encoding.GetEncoding("GB2312"));
            Image image = bp;

            //保存二维码图片:
            SaveFileDialog sf = new SaveFileDialog();
            sf.Title = "选择保存文件位置";
            sf.Filter = "保存图片(*.jpg) |*.jpg|所有文件(*.*) |*.*";
            //设置默认文件类型显示顺序
            sf.FilterIndex = 1;
            //保存对话框是否记忆上次打开的目录
            sf.RestoreDirectory = true;
            if (sf.ShowDialog() == DialogResult.OK)
            {
                //获得文件路径
                string localFilePath = sf.FileName.ToString();
                image.Save(localFilePath);
            }
            return image;
        }
    }
}

winfrom编写二维码(可上传图片)