用C# 制作名片

需要动态链接库ThoughtWorks.QRCode.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TwoBar
{
    public class CardData
    {
        public string Name { get; set; }
        public string Post { get; set; }
        public string DepartMent { get; set; }
        public string Company { get; set; }
        public string MobilePhone { get; set; }
        public string TelePhone { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }

    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using ThoughtWorks.QRCode.Codec;

namespace TwoBar
{
    public class CreateQRCode
    {
        public string GetCodeInfo(CardData cardData)
        {
            StringBuilder card = new StringBuilder();
            card.Append("BEGIN:VCARD");
            card.Append("\r\nFN:" + cardData.Name);
            card.Append("\r\nTITLE:" + cardData.Post);
            card.Append("\r\nORG:" + cardData.Company + ";" + cardData.DepartMent);
            card.Append("\r\nTEL;CELL:" + cardData.MobilePhone);
            card.Append("\r\nTEL;WORK:" + cardData.TelePhone);
            card.Append("\r\nADR;WORK:" + cardData.Address);
            card.Append("\r\nEMAIL:" + cardData.Email);
            card.Append("\r\nPHOTO;ENCODING=b;TYPE=JPEG:");
            card.Append("\r\nEND:VCARD\r\n");
            return card.ToString();
        }

        public Bitmap CreatCodeImage(CardData cardData, int imageWidth, int imageHeight)
        {
            //1 根据名片内容生成字符串
            string cardString = GetCodeInfo(cardData);

            //2 创建图片对象和绘图对象
            Bitmap bmp = new Bitmap(imageWidth, imageHeight);
            Graphics graphics = Graphics.FromImage(bmp);
            graphics.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height);//填上背景

            //【3】创建二维码编码类对象(第三方的dll)并设置属性
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//设置编码方式
            qrCodeEncoder.QRCodeScale = 3;//设置二维码图片大小
            qrCodeEncoder.QRCodeVersion = 0;//设置版本
            //设置错误校验级别,因为二维码有纠错能力,所以允许加入logo图片
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;

            //【4】按照编码格式将名片信息编码成图片文件
            Image image = qrCodeEncoder.Encode(cardString, Encoding.GetEncoding("utf-8"));//image为二维码


            //【5】画出二维码图片
            int x = (imageWidth - image.Width) / 2;
            int y = (imageHeight - image.Height) / 2;
            graphics.DrawImage(image, new Point(x, y));//使用Griphics对象在背景上画出二维码图片

            //【6】画出logo图片,并添加到二维码上面
            image = Properties.Resources.logo;//通过资源文件获取图片
            x = (imageWidth - image.Width) / 2;
            y = (imageHeight - image.Height) / 2;
            graphics.DrawImage(image, new Point(x, y));

            return bmp;
        }
    }
}

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;

namespace TwoBar
{
    public partial class Form1 : Form
    {
        private CreateQRCode createQRCode = new CreateQRCode();
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            //封装数据
            CardData data = new CardData
            {
                Name = this.tbName.Text.Trim(),
                Post = this.tbPost.Text.Trim(),
                DepartMent = this.tbDepartment.Text.Trim(),
                Company = this.tbCompany.Text.Trim(),
                MobilePhone = this.tbMobilePhone.Text.Trim(),
                TelePhone = this.tbTelePhone.Text.Trim(),
                Address = this.tbAddrress.Text.Trim(),
                Email = this.tbEMail.Text.Trim()
            };
            //调用二维码生成类的方法生成图片
            this.pbCode.Image = createQRCode.CreatCodeImage(data, this.pbCode.Width, this.pbCode.Height);
        }
    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值