.net core 畫出奇門遁甲九宮圖

透過.net 做一個可以載入圖片,然後在圖片上寫字的功能。
這邊是以底圖(九宮圖.jpg)
然後依照x,y座標畫上文字的方式

程式碼

public void ProcessImage()
{
/*
* //Install-Package System.Drawing.Common
* using System.Drawing;
* using System.Drawing.Imaging;
* using System.Drawing.Text;
* using System.IO;
* using System.Drawing;
*/
// 創建一個新的 1024x768 的圖片
using (Bitmap newImage = new Bitmap(1024, 768))
using (Graphics g = Graphics.FromImage(newImage))
{
// 設置文字渲染品質
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

// 讀取原始圖片
using (Image sourceImage = Image.FromFile(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "九宮圖.jpg")))

{
// 將原始圖片繪製到新圖片上
g.DrawImage(sourceImage, 0, 0, 1024, 768);
}

// 創建字體
string fontPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "字型.ttf");

PrivateFontCollection privateFont = new PrivateFontCollection();
privateFont.AddFontFile(fontPath);
using (Font customFont = new Font(privateFont.Families[0], 25))
{
// 創建黑色畫筆
using (SolidBrush blackBrush = new SolidBrush(Color.Black))
{
// 繪製所有文字
string message;

// 巽4宮
message = "  太陰\n  休門 辛\n  天心 癸\n 巽4宮";
g.DrawString(message, customFont, blackBrush, 181, 114);

// 震3宮
message = " 六合\n  開門 庚\n  天柱 丁\n 震3宮";
g.DrawString(message, customFont, blackBrush, 181, 314);

// 艮8宮
message = " 白虎\n壬一 開門 庚\n天禽 天柱 丁\n 艮8宮";
g.DrawString(message, customFont, blackBrush, 181, 534);

// 離9宮
message = " 騰蛇\n 生門 乙\n 天蓬 戊\n 離9宮";
g.DrawString(message, customFont, blackBrush, 418, 114);

// 中5宮
message = " \n \n 壬\n 中5宮";
g.DrawString(message, customFont, blackBrush, 418, 314);

// 坎1宮
message = " 玄武\n 死門 戊\n 天英 乙\n 坎1宮";
g.DrawString(message, customFont, blackBrush, 418, 534);

// 坤2宮 (注意這裡字體大小是30)
using (Font largerFont = new Font(privateFont.Families[0], 30))
{
message = " 值符\n 傷門 己\n 天任 丙\n 坤2宮";
g.DrawString(message, largerFont, blackBrush, 648, 114);
}

// 兌7宮
message = " 九天\n 杜門 丁\n 天沖 庚\n 兌7宮";
g.DrawString(message, customFont, blackBrush, 648, 314);

// 乾6宮
message = " 九地\n 景門 癸\n 天輔 辛\n 乾6宮";
g.DrawString(message, customFont, blackBrush, 648, 534);
}
}

// 保存圖片
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
string savePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "imagemake", fileName);
newImage.Save(savePath, ImageFormat.Jpeg);
}

作者: 林壽山

目前任職於軟體公司研究開發部門,擔任專業處長,專注於.NET C# 開發,並具備豐富的POS 收銀系統與金流整合開發經驗。我精通各類支付系統的設計與開發,包含第三方支付(如綠界、藍新、歐付寶、速買配、馬來西亞 ePay/HappyPay、台新 One 碼)、行動支付(悠遊卡、一卡通、支付寶、微信支付、街口支付)、以及信用卡支付(聯合信用卡)。 熟悉多種開發技術,擅長PHP 網頁開發(CodeIgniter、Laravel 框架)、Delphi 程式設計、資料庫設計、C# WinForm/WebForm 應用開發、ASP.NET MVC、API 串接設計,並具備LINE 串接開發的豐富經驗。 除了技術開發之外,我也熱衷於技術分享,曾擔任台中學校產業學院講師 5 年,培育新一代的軟體開發人才,致力於推動軟體技術的應用與創新。 我對技術充滿熱忱,始終保持學習與探索的心態,期望透過軟體開發為企業與社會創造更大的價值。