以下是个园林树牌信息生成程序,是使用c#语言编写的控制台程序,包含了生成图片、生成二维码、读取excel、自动匹配路径等功能,可作为以上功能的demo。注意只有系统中已有的字体才可以使用,如果使用系统中没有的字体,则会显示宋体。还要注意添加引用。
using MessagingToolkit.QRCode.Codec;
using System;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using Font = System.Drawing.Font;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
try
{
System.Data.DataTable treePlateInfo = ReadExcelToTable();
for(int i = 0; i < treePlateInfo.Rows.Count; i++)
{
createRQcode(treePlateInfo.Rows[i].ItemArray[0].ToString().Trim(), treePlateInfo.Rows[i].ItemArray[2].ToString().Trim());
GetImg(treePlateInfo.Rows[i].ItemArray[0].ToString().Trim()
, treePlateInfo.Rows[i].ItemArray[1].ToString().Trim()
, treePlateInfo.Rows[i].ItemArray[2].ToString().Trim()
, treePlateInfo.Rows[i].ItemArray[3].ToString().Trim()
, treePlateInfo.Rows[i].ItemArray[4].ToString().Trim()
, treePlateInfo.Rows[i].ItemArray[5].ToString().Trim()
, treePlateInfo.Rows[i].ItemArray[6].ToString().Trim());
}
}
catch (Exception ex)
{
Console.WriteLine("error:" + ex);
}
}
//生成树牌
public static void GetImg(string id,string platename,string name,string english,string family,string time,string effect)
{
string temp = "";
int n = 290;
string RQcodePath = "C:\\Users\\Dell\\Desktop\\RQcode\\" + name + "\\" + platename; //二维码图片路径
Bitmap image = new Bitmap(800, 534); //设置图片宽高
Graphics g = Graphics.FromImage(image); //生成画布
Image RQcodeImg = Image.FromFile(RQcodePath); //读取二维码图片
Brush b = new SolidBrush(Color.Black); //设置文字颜色
Font f1 = new Font("FZWeiBei-S03S", 23, System.Drawing.FontStyle.Bold); //设置字体,大小,加粗
Font f2 = new Font("方正粗黑宋简体", 47); //设置字体,大小
Font f3 = new Font("宋体", 18, System.Drawing.FontStyle.Bold); //设置字体,大小,加粗
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //设置图片质量
g.Clear(Color.FromArgb(255, 255, 255)); //设置背景颜色
g.DrawString("测试文字", f1, b, 30, 60); //生成“测试文字”
g.DrawString(name, f2, b, 300, 100); //生成植物名称
g.DrawString("英 文: "+ english, f3, b, 100, 185); //生成植物英文名
g.DrawString("科 属: "+ family, f3, b, 100, 220); //生成植物科属
g.DrawString("花 期: "+ time, f3, b, 100, 255); //生成植物花期
g.DrawString("作 用: ", f3, b, 100, 290); //生成植物作用
g.DrawString(id,new Font("宋体",15),b,540,40); //生成植物编号
g.DrawImage(RQcodeImg, 470, 60, 200, 200); //设置二维码图片x位置,y位置,宽,高
//生成植物作用内容
for(int i = 0; i < effect.Length; i++)
{
if(temp.Length % 19 == 0 && temp.Length != 0)
{
if (effect[i] == '。'|| effect[i] == ','|| effect[i] == ';'|| effect[i] == '、'|| effect[i] == '“'|| effect[i] == '”')
{
temp += effect[i];
i++;
}
g.DrawString(temp, f3, b, 190, n);
n += 35;
temp = "";
}
if (i >= effect.Length)
break;
temp += effect[i];
}
g.DrawString(temp, f3, b, 190, n);
//没有文件夹就创建
Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\treePlate\\" + name);
//保存图片到指定文件夹
image.Save(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\treePlate\\" + name + "\\" + name + "-" + platename, ImageFormat.Wmf);
}
//查询树牌信息页中数据
public static System.Data.DataTable ReadExcelToTable()
{
string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
+ Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "/treePlateInfo.xlsx"
+ ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; //连接字符串
string firstSheetName = null; //树牌信息页名字
string sql = null; //查询字符串
using (OleDbConnection conn = new OleDbConnection(connstring))
{
conn.Open();
System.Data.DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });//得到所有sheet的名字
firstSheetName = sheetsName.Rows[1][2].ToString(); //得到树牌信息页的名字
sql = string.Format("SELECT * FROM [{0}]", firstSheetName);
OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring);
DataSet set = new DataSet();
ada.Fill(set);
return set.Tables[0];
}
}
//生成二维码
public static void createRQcode(string id,string name)
{
string text = "http://test/home.html?" + id;
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeVersion = 0;
Bitmap img = qrCodeEncoder.Encode(text, Encoding.UTF8);
Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\RQcode\\" + name);
img.Save(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\RQcode\\" + name + "\\" + id +".wmf", ImageFormat.Wmf);
}
}
}