程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#抓屏(截屏)

C#抓屏(截屏)

編輯:關於C#

先是給大家一個類:

class ScreenShot
  {
    public static void CaptureImage(Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle, string FilePath)
    {
      using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height))
      {
        using (Graphics g = Graphics.FromImage(bitmap))
        {
          g.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);
        }
        bitmap.Save(FilePath, ImageFormat.Bmp);
      }
    }
  }

所需添加引用如下:

using System;

using System.Drawing;

using System.Drawing.Imaging;

調用方法:

private void button1_Click(object sender, EventArgs e)
    {
      saveFileDialog1.DefaultExt = "bmp";
      saveFileDialog1.Filter = "bmp files (*.bmp)|*.bmp";
      saveFileDialog1.Title = "導出地圖為...";
      saveFileDialog1.ShowDialog();
      if (saveFileDialog1.FileName.Length > 0)
      {
        ScreenPath = saveFileDialog1.FileName;
      }
      else
      {
        return;
      }
      this.Refresh();
      //3個參數:獲得控件所在屏幕坐標,目標坐標點為(0,0),獲得控件大小。
      ScreenShot.CaptureImage(axMapControl1.PointToScreen(Point.Empty), Point.Empty, new Rectangle(axMapControl1.Location, axMapControl1.Size), ScreenPath);    
    }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved