使用 `pictureBox.CreateGraphics()` 獲取的 `Graphics` 物件進行繪圖 (如 `DrawEllipse` 棋子),**繪製內容僅存在於視訊記憶體 (顯示緩衝區) 中,不持久化到 `Image` 屬性**。當視窗最小化、遮擋、拖動、失去焦點重繪時,Windows 發送 `WM_PAINT`,`PictureBox` 重繪其 `Image` 屬性 (棋盤背景),**你的臨時繪圖內容隨即消失**。
// 1. 建立離屏位圖 (尺寸 = PictureBox.ClientSize)
Bitmap offscreenBmp = new Bitmap(picBox.Width, picBox.Height);
using (Graphics g = Graphics.FromImage(offscreenBmp)) {
// 2. 先畫背景 (棋盤)
g.DrawImage(Properties.Resources.Board, 0, 0);
// 3. 畫所有棋子 (從資料模型遍歷)
foreach (var piece in pieces) {
g.DrawImage(piece.Image, piece.Rect);
}
}
// 4. 一次性賦值給 PictureBox (自動觸發重繪)
picBox.Image?.Dispose(); // 釋放舊圖
picBox.Image = offscreenBmp;
優點:雙緩衝無閃爍、繪圖邏輯集中、縮放/列印支援原生、資料驅動視圖 (MVC)。
// 繼承 PictureBox 或在 Form 載入時訂閱
picBox.Paint += (sender, e) => {
Graphics g = e.Graphics;
// 1. 畫背景
g.DrawImage(Properties.Resources.Board, 0, 0);
// 2. 畫棋子 (讀取資料模型)
foreach (var piece in pieces) {
g.DrawImage(piece.Image, piece.Rect);
}
};
// 資料變更時呼叫
picBox.Invalidate(); // 觸發重繪
優點:標準 WinForms 繪圖模式、自動處理重繪。缺點:高頻 `Invalidate` 可能閃爍 (需設 `DoubleBuffered = true`)、繪圖邏輯分散在事件中。