雙螢幕環境下,Graphics.CopyFromScreen 僅能捕獲主螢幕,需將所有螢幕內容合成到一張位圖。
using System.Windows.Forms;
// 發送全局 PrintScreen,系統自動將所有螢幕合成放入剪貼板
SendKeys.SendWait("{PRTSC}");
// 從剪貼板讀取合成圖像
if (Clipboard.ContainsImage()) {
Image fullScreenshot = Clipboard.GetImage();
fullScreenshot.Save("AllScreens.png", ImageFormat.Png);
}
優點:零依賴、自動處理 DPI 縮放、多螢幕座標拼接。
缺點:依賴剪貼板,若其他程序佔用可能失敗;無法精確控制捕獲區域。
Rectangle totalBounds = Rectangle.Empty;
foreach (Screen screen in Screen.AllScreens) {
totalBounds = Rectangle.Union(totalBounds, screen.Bounds);
}
using (Bitmap bmp = new Bitmap(totalBounds.Width, totalBounds.Height))
using (Graphics g = Graphics.FromImage(bmp)) {
foreach (Screen screen in Screen.AllScreens) {
// 將每個螢幕內容繪製到總位圖對應位置
g.CopyFromScreen(screen.Bounds.Location,
new Point(screen.Bounds.Left - totalBounds.Left,
screen.Bounds.Top - totalBounds.Top),
screen.Bounds.Size);
}
bmp.Save("AllScreens_Manual.png", ImageFormat.Png);
}
此法完全可控,適合無 UI 服務或需指定像素格式的場景。
快速原型用 SendKeys;生產級工具建議手動枚舉 Screen.AllScreens 拼接,穩健可控。