ScreenShot 類別
class ScreenShot
{
public static void CaptureImage(Point sourcePoint, Point destPoint, Rectangle selectionRect, string filePath)
{
using (Bitmap bitmap = new Bitmap(selectionRect.Width, selectionRect.Height))
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(sourcePoint, destPoint, selectionRect.Size);
bitmap.Save(filePath, ImageFormat.Bmp);
}
}
}
必要參考
using System;
using System.Drawing;
using System.Drawing.Imaging;
WinForms 呼叫範例
private void button1_Click(object sender, EventArgs e)
{
saveFileDialog1.DefaultExt = "bmp";
saveFileDialog1.Filter = "bmp files (*.bmp)|*.bmp";
saveFileDialog1.Title = "匯出地圖為...";
if (saveFileDialog1.ShowDialog() != DialogResult.OK) return;
string screenPath = saveFileDialog1.FileName;
this.Refresh();
// 截取 axMapControl1 控制項畫面
ScreenShot.CaptureImage(
axMapControl1.PointToScreen(Point.Empty),
Point.Empty,
new Rectangle(axMapControl1.Location, axMapControl1.Size),
screenPath);
}
說明
CopyFromScreen 直接從螢幕 DC 複製像素,速度快、不需處理視窗遮蓋。
- 參數:來源螢幕座標、目標圖片座標(通常
Point.Empty)、選取區域大小、儲存路徑。
- 可擴充支援 PNG/JPEG 等格式(更改
ImageFormat)。