.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);
}