熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

用C#實現全屏幕截圖

2022-06-13   來源: .NET編程 

  今天一位同事想寫一個全屏幕截圖的代碼當然要實現的第一步是能夠獲取整個屏幕的位圖記得Win API的CreateDC BitBlt等函數可以使用於是上網查了下果然屏幕截圖用這些函數但winform已經可以把API都忘記了所以得尋找一個無Win API的實現方式綜合了網上的實現以及自己的一些設計實現思路如下 開始截圖時創建一個與屏幕大小一樣的位圖然後用GraphicsCopyFromScreen()把屏幕位圖拷貝到該位圖上這是很關鍵的一步這樣所有的操作就都可以在該位圖上進行了而無實際屏幕無關了

  int width = ScreenPrimaryScreenBoundsWidth;

  int height = ScreenPrimaryScreenBoundsHeight;

  Bitmap bmp = new Bitmap(width height);

  using (Graphics g = GraphicsFromImage(bmp)) {

  gCopyFromScreen( new Size(width height));

  }

   接下來為了方便在這之上進行截圖有一個很重要的設計實現方式用全屏幕窗體代替現有真實屏幕這樣就可以把截圖過程的所有操作都在那個窗體上實現(該窗體設置成無邊框高寬等於屏幕大小即可)另外為了顯示掩蔽效果(只能正常顯示選擇的部分屏幕內容而其實部分用一個如半透明層覆蓋)就添加一層半透明位置位圖具體代碼如下

  public partial class FullScreenForm : Form {

  private Rectangle rectSelected = RectangleEmpty;

  private bool isClipping = false;

  private Bitmap screen;

  private Bitmap coverLayer = null;

  private Color coverColor;

  private Brush rectBrush = null;

  private Bitmap resultBmp = null;

  public FullScreenForm(Bitmap screen) {

  InitializeComponent();

  int width = ScreenPrimaryScreenBoundsWidth;

  int height = ScreenPrimaryScreenBoundsHeight;

  coverLayer = new Bitmap(width height);

  coverColor = ColorFromArgb( );

  rectBrush = new SolidBrush(coverColor);

  using (Graphics g = GraphicsFromImage(coverLayer)) {

  gClear(coverColor);

  }

  thisBounds = new Rectangle( width height);

  thisscreen = screen;

  thisDoubleBuffered = true;

  }

  protected override void OnMouseDown(MouseEventArgs e) {

  if (eButton == MouseButtonsLeft) {

  isClipping = true;

  rectSelectedLocation = eLocation;

  }

  else if (eButton == MouseButtonsRight) {

  thisDialogResult = DialogResultOK;

  }

  }

  protected override void OnMouseMove(MouseEventArgs e) {

  if (eButton == MouseButtonsLeft && isClipping) {

  rectSelectedWidth = eX rectSelectedX;

  rectSelectedHeight = eY rectSelectedY;

  thisInvalidate();

  }

  }

  protected override void OnMouseUp(MouseEventArgs e) {

  if (eButton == MouseButtonsLeft && isClipping) {

  rectSelectedWidth = eX rectSelectedX;

  rectSelectedHeight = eY rectSelectedY;

  thisInvalidate();

  resultBmp = new Bitmap(rectSelectedWidth rectSelectedHeight);

  using (Graphics g = GraphicsFromImage(resultBmp)) {

  gDrawImage(screennew Rectangle( rectSelectedWidth rectSelectedHeight) rectSelected GraphicsUnitPixel);

  }

  thisDialogResult = DialogResultOK;

  }

  }

  protected override void OnPaint(PaintEventArgs e) {

  Graphics g = eGraphics;

  gDrawImage(screen );

  gDrawImage(coverLayer );

  PaintRectangle();

  }

  protected override void OnPaintBackground(PaintEventArgs e) {

  }

  protected override void OnKeyDown(KeyEventArgs e) {

  if (eKeyCode == KeysEscape) {

  thisDialogResult = DialogResultCancel;

  }

  }

  private void PaintRectangle() {

  using (Graphics g = GraphicsFromImage(coverLayer)) {

  gClear(coverColor);

  GraphicsPath path = new GraphicsPath();

  pathAddRectangle(thisBounds);

  pathAddRectangle(rectSelected);

  gFillPath(rectBrush path);

  gDrawRectangle(PensBlue rectSelected);

  }

  }

  public Bitmap ResultBitmap {

  get { return resultBmp; }

  }

  }

  上面的代碼都很容易看明白這裡有一個技巧就是GraphicsPath它自動會形成一個中空的區域上面的實現很容易擴展多區域截圖多裁判截圖等都很容易實現


From:http://tw.wingwit.com/Article/program/net/201311/12191.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.