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

透過azure openai / openai api with vision功能,讓人工智慧看懂圖片

做了個小玩具  網址: https://shoushan.happyweb.com.tw

上傳圖片後,可以做出

一、依照圖片內容生成商品文案給社群小編行銷

二、人工智慧ai 描述辨識圖片

三、上傳餐廳、飲料店等菜單,透過ai辨識回傳json

四、行為偵測,例如上傳照片 讓ai看看有沒有犯法或違法

五、上傳一張網頁的圖片/手繪的prototyp,然後生成規格書與欄位內容,最後搞出一個前端的prototype

聊架構 – 大型網站設計中多人上線指標Transactions Per Second(TPS)定義

大型網站的架構設計中,主要都是以同一時間系統能處理大量網頁請求或處理任務的能力當標準值。假設說,五月天的演唱會訂票系統瞬間會有大量搶票、支付,或是系統同時會有幾十萬客戶上線,這種有機會讓系統癱瘓掉的都屬於「高併發」。
而高併發衡量的指標主要是以”Transactions Per Second(TPS)”就是每秒處理交易數量,就如同剛才說的五月天演唱會搶票,以2024年6月統計7-11有12000家同時有人搶票,就算是高併發的一個場景。
一般而言TPS 落在1000-5000 屬於具有一定程度的併發,而5000以上就算高併發,像50000以上就屬於超大型網站會遇到的超高併發了。
要支撐這樣的場景,需要採用許多高併發的技術方案。
一、分散式架構
將系統的負載分散到多個伺服器的節點,提高擴展與可靠性。例如:增加伺服器的數量

二、微服務架構

把系統從單體拆分成相互獨立的服務,系統各自有自己的業務功能,彼此間透過RPC HTTP 做通訊。而每個微服務可以根據流量/負載做水平擴增。微服務的好處還可以獨立部署或擴增,減少耦合。

三、Cche機制

透過減少直接連接資料庫來提高回應速度,降低伺服器與資料庫的壓力。而用最多的莫過於使用redis 集群方式來擴展性能。把會頻繁查詢的資料,像商品、會員等放在記憶體中,提高併發量。

四、Load Balance 負載平衡

把客戶端的請求分配到不同伺服器,避免單一台伺服器過載,例如像nginx、HAProxy、F5等技術。而負載平衡有三種主要的策略來做分配

  • 輪詢:依順序把請求配到每一台伺服器
  • 權重:根據伺服器效能設定不同權重,能者多勞(不是過勞喔)的意思
  • IP Hash:透過請求的IP來分配,把同一個IP分配到同一台,這樣可以讓Session保持

五、流量的削峰

這是常用到的方式,避免瞬間大量沖垮服務過載。常用的方法是透過佇列的排隊來處理,比方說放到MQ這種訊息佇列來做承受大流量。

六、限流

透過限制請求的速率,主要有Token Bucket、Leaky bucket或計數器三種演算法

可參考:[Architecture] 架構設計 – 限流策略 Rate Limiting Strategies

七、熔斷

當某服務故障、過慢,熔斷機制可臨時中斷請求,避免影響

八、降級

關閉一些非核心的服務,降低系統壓力。

C# 進階概念釐清


一、瞭解IEnumerable 以及 ICollection
不允許資料被修改操作用前者,後者可以

二、要寫低耦合可測試的程式,使用DI
透過依賴注入可以簡單管理class間的依賴關係
三、參數使用ref 與out 的差別
ref 傳給方法的參數要先初始化,方法可修改值;out不需初始,但一定要回傳值
四、透過async await 來提高程式的效能
async允許非同步執行;await透過不阻塞主thread來update ui
五、錯誤處理機制
一般使用try catch 。全局時可以透過app.UseExceptionHandler統一處理

六、appsettings.json 搭配環境來劃分

七、task與thread
thread單執行緒;taskTA是非同步的抽象,與async/await一起服用

匿名函式&委派

// 壽山測試
var f = () => Console.WriteLine(“哈囉 國興”); // 註解: 定義一個匿名函式
f += () => Console.WriteLine(“哈囉 後端”); // 註解: 加入一個匿名函式
f += () => Console.WriteLine(“哈囉 全端”); // 註解: 加入一個匿名函式
f(); // 註解: 呼叫匿名函式

原文: David Fowler

redis 的key 過期通知設定

Redis - Wikipedia

redis有expire機制,且同時有redis keyspace notification。不過預設默認的通知是關閉的~需要透過redis.conf或config set啟用。

config set notify-keyspace-events KEA

接著在c# 使用StackExchange.Redis

然後訂閱__keyevent@0__:expired 這個頻道就可以了

var redisConnection = ConnectionMultiplexer.Connect(“localhost”);
var db = redisConnection.GetDatabase();

var subscriber = redisConnection.GetSubscriber();
// 訂閱過期事件通知頻道
subscriber.Subscribe(“__keyevent@0__:expired”, (channel, key1) =>
{
// 在這裡進行過期事件的處理邏輯
// key 是過期的 Redis 鍵名
Console.WriteLine($”Key {key1} has expired.”);
});

Console.WriteLine(“Press any key to exit.”);
Console.ReadKey();

// 斷開與 Redis 的連線
redisConnection.Close();

chatGPT與line官方帳號結合 = line ai 小助理

OpenAI設計的ChatGPT問世後,很快的ai的使用人數破億。且chatgpt讓ai整個流行了起來~ 想說台灣最流行的通訊工具line,有沒有機會跟chatgpt串到一起,於是很快的用php以及.net core都串了一次。簡單明瞭~很易懂

分享關鍵的php,有問題歡迎line: superlevin 林壽山


$api_key = 'xxxxxxxxxx';
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.openai.com/v1/completions',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
],
CURLOPT_POSTFIELDS => json_encode([
'prompt' => $ask_question,
'model' => 'text-davinci-003',
'max_tokens' => 1000,
'temperature' => 0.3
])
]);
$response = curl_exec($curl);

$json = json_decode($response, true);

 

 

C#資料結構與演算法

使用對的演算法與資料結構,在程式效能上有很大的幫助。常用的資料結構有array、stack、 queue、 linked list、 tree、 graph、 heap、 hash。在c#上常見的是使用array 、arraylist、 list、 dictonary 以及queue可以滿足絕大部份的場景。

以下介紹github上二個高階資料結構與演算法的項目。

一、C Sharp Algorithms

一開始是學習資料結構,在不斷更新以及維護後,可以用在正式環境。

https://github.com/aalhour/C-Sharp-Algorithms

二、Advanced Algorithms

https://github.com/justcoding121/advanced-algorithms

.Net 7 開發增進效能的小撇步

預計在2022年11月發布的.net7目前進入RC2的版本。來談談.net7 做了那些效能上的改進~

LINQ部份

1針對max/min最佳化

2針對Average/sum

3新增order/orderDescending 取代orderby/orderbydescending

4 System.IO的改善 WriteAllText改ReadAllText

5 針對Jsonserializer NoCachedOptions提昇

6Guid的 GuidEquals提昇

7ParseBigInt提昇

8ParseBool

9 Stopwatch中的getprocessbyname與getcurrentprocessname

10 在stopwatch加上getelapsedtime取得時間戳