| 
參考 題型範例 - 全國高級中等學校技藝競賽平台 工業類 題目:框選圖中物件程式 ● 請寫一隻程式,能依 LabelData.txt 檔內的座標來框選指定檔名中的物件並存檔。 ● 提供的檔案說明:本題提供 5 個圖檔及 1 個座標資訊檔 LabelData.txt 供測試。 LabelData.txt 檔中的第 1 列內容為 "ap10.jpg 1 109 133 358 373",意旨在圖檔名為 ap10.jpg 中標示 1 個矩形,其左上角座標為 (109,133)、右下角座標為 (358,373),結果如下左圖。以下解答使用 SkiaSharp 套件,請先依照下面步驟安裝套件。 
 
/// 教育部工科技藝競賽 109 年第二題 框選圖中物件程式
/// 漆家豪 於 海青工商 2024/2/18
using System;
using System.IO;
using SkiaSharp;
class Program
{
    static void Main(string[] args)
    {
        string labelDataPath = "LabelData.txt";
        string imageFolderPath = "../../../"; // 圖片所在的資料夾
        string outputFolderPath = "imageOUT";
        // 如果輸出資料夾不存在,則建立一個
        if (!Directory.Exists(outputFolderPath))
        {
            Directory.CreateDirectory(outputFolderPath);
        }
        string[] lines = File.ReadAllLines(imageFolderPath + labelDataPath);
        foreach (string line in lines)
        {
            string[] parts = line.Split(' ');
            string fileName = parts[0];
            int numRectangles = int.Parse(parts[1]);
            string imagePath = Path.Combine(imageFolderPath, fileName);
            // 創建一個新的 SKBitmap 對象
            using (var bitmap = SKBitmap.Decode(imagePath))
            {
                // 在 SKCanvas 上繪製圖形
                using (var canvas = new SKCanvas(bitmap))
                {
                    for (int i = 0; i < numRectangles; i++)
                    {
                        int startX = int.Parse(parts[2 + i * 4]);
                        int startY = int.Parse(parts[3 + i * 4]);
                        int endX = int.Parse(parts[4 + i * 4]);
                        int endY = int.Parse(parts[5 + i * 4]);
                        // 繪製矩形
                        using (var paint = new SKPaint { Color = SKColors.Red, IsAntialias = true })
                        {
                            paint.Style = SKPaintStyle.Stroke;
                            paint.StrokeWidth = 3;
                            SKRect rect = SKRect.Create(startX, startY, endX - startX, endY - startY);
                            canvas.DrawRect(rect, paint);
                        }
                    }
                }
                // 儲存加框後的圖片
                using (var output = File.Create(imageFolderPath + outputFolderPath + "/" +fileName))
                {
                    // 將 SKBitmap 保存為 PNG 檔案
                    bitmap.Encode(output, SKEncodedImageFormat.Png, 100);
                }
                Console.WriteLine($"在 {imagePath} 圖檔中加框,以相同檔名存入 {outputFolderPath} 資料夾中");
            }
        }
    }
}
 |