c#生成圖片、生成二維碼、讀取excel、自動匹配路徑

以下是個園林樹牌信息生成程序,是使用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);
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章