首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 螢幕截圖功能實作:ScreenShot 類別與呼叫範例

     編輯:關於C語言

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)。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved