设置指定信息生成二维码和下载实例
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 MessagingToolkit.QRCode.Codec;
using System.Drawing.Drawing2D;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeBackgroundColor = Color.Transparent;
//qrCodeEncoder.QRCodeForegroundColor = Color.Orange;
//qrCodeEncoder.CharacterSet = CharacterSets[cboCharacterSet.Text];
qrCodeEncoder.QRCodeScale = int.Parse(textBox2.Text);
Bitmap image = qrCodeEncoder.Encode(textBox1.Text);
pictureBox1.Image = CombinImage(image,System.IO.Directory.GetCurrentDirectory()+ "\\logo.png", textBox3.Text);
pictureBox1.Height = pictureBox1.Image.Height;
pictureBox1.Width = pictureBox1.Image.Width;
image.Save("./" + textBox3.Text + ".png");
}
/// <summary>
/// 调用此函数后使此两种图片合并,类似相册,有个
/// 背景图,中间贴自己的目标图片
/// </summary>
/// <param name="imgBack">粘贴的源图片</param>
/// <param name="destImg">Logo图片</param>
public static Image CombinImage(Image imageCode, string logoImagePath, string words)
{
Image imageLogo = Image.FromFile(logoImagePath); //照片图片
if (imageLogo.Height != 65 || imageLogo.Width != 65)
{
imageLogo = KiResizeImage(imageLogo, 65, 65, 0);
}
Graphics g = Graphics.FromImage(imageCode);
g.DrawImage(imageCode, 0, 0, imageCode.Width, imageCode.Height);
//g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一层黑色边框
//g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);
int x = imageCode.Width / 2 - imageLogo.Width / 2;
int y = imageCode.Width / 2 - imageLogo.Width / 2;
g.DrawImage(imageLogo, x, y, imageLogo.Width, imageLogo.Height);
//添加文字
Font font = new System.Drawing.Font("微软雅黑", 30F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
x = x - (int)(font.Size * words.Length) / 4;
g.DrawString(words, font, Brushes.Orange, new Point(x, y + imageLogo.Height));
g.Dispose();
return imageCode;
}
/// <summary>
/// Resize图片
/// </summary>
/// <param name="bmp">原始Bitmap</param>
/// <param name="newW">新的宽度</param>
/// <param name="newH">新的高度</param>
/// <param name="Mode">保留着,暂时未用</param>
/// <returns>处理以后的图片</returns>
public static Image KiResizeImage(Image bmp, int newW, int newH, int Mode)
{
try
{
Image b = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(b);
// 插值算法的质量
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch
{
return null;
}
}
}
}
http://download.csdn.net/detail/fuyifang/7792287