溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#如何實現(xiàn)小截屏軟件功能

發(fā)布時間:2021-05-17 10:47:55 來源:億速云 閱讀:235 作者:小新 欄目:編程語言

這篇文章主要介紹了C#如何實現(xiàn)小截屏軟件功能,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

1.基本功能

選擇屏幕區(qū)域后提醒你保存所截的圖片,或直接將所截圖片放到剪切板里(以便用Ctrl+V粘貼)。

2.界面設計

界面很簡單,無非就是可實現(xiàn)以上功能的兩個按鈕和其他文字,見圖:

C#如何實現(xiàn)小截屏軟件功能

界面的屬性需要注意幾個問題:

1)窗口設為固定大小,并禁用窗口最大化(因為我們不希望窗口大小會變)

C#如何實現(xiàn)小截屏軟件功能

C#如何實現(xiàn)小截屏軟件功能

2)窗口最好設為頂置

C#如何實現(xiàn)小截屏軟件功能

3)把兩個文字label和兩個按鈕都放到一個panel里,以便于后面程序?qū)丶傩缘牟僮?/p>

4)那么大的按鈕,最好改變一下它的樣式,還可以設置背景為gif動圖

C#如何實現(xiàn)小截屏軟件功能

3.功能實現(xiàn)

那么關(guān)鍵問題來了,怎么截圖呢?見下圖

C#如何實現(xiàn)小截屏軟件功能

原理其實很簡單,就是在點擊按鈕后,窗口變?yōu)槿粮采w在屏幕最上方,并變?yōu)榘胪该?,使你能看到窗口下面的屏幕?nèi)容,然后拖動鼠標(此時其實是在軟件的主窗口上拖動,這樣就方便程序捕捉鼠標坐標了),根據(jù)坐標在屏幕上繪制選框,接著松開鼠標后,根據(jù)鼠標落下和松開的坐標,截取屏幕,最后保存或復制到剪切板。

4.敲代碼吧

using System;
using System.Windows.Forms;
using System.Drawing;//繪圖要用
using System.Threading;//延時函數(shù)要用

namespace 截屏
{
  public partial class Form1 : Form
  {
    bool mouseDown = false, havePainted = false;
    Point start, end;
    Point start1, end1;
    Size size = new Size(0, 0);
    bool saveFile = true;
    public Form1()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      ReadyToCaptrue();
      saveFile = true;
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
      start = e.Location;
      mouseDown = true;
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
      if (size.Width != 0 && size.Height != 0)
      {
        ControlPaint.DrawReversibleFrame(new Rectangle(start1, size), Color.Transparent, FrameStyle.Dashed);
        havePainted = false;
      }
      end = e.Location;
      if (start.X > end.X)
      {
        int temp = end.X;
        end.X = start.X;
        start.X = temp;
      }

      if (start.Y > end.Y)
      {
        int temp = end.Y;
        end.Y = start.Y;
        start.Y = temp;
      }
      this.Opacity = 0.0;
      Thread.Sleep(200);
      if (end.X - start.X > 0 && end.Y - start.Y > 0)
      {
        Bitmap bit = new Bitmap(end.X - start.X, end.Y - start.Y);
        Graphics g = Graphics.FromImage(bit);
        g.CopyFromScreen(start, new Point(0, 0), bit.Size);
        if (saveFile)
        {
          SaveFileDialog saveFileDialog = new SaveFileDialog();
          saveFileDialog.Filter = "png|*.png|bmp|*.bmp|jpg|*.jpg|gif|*.gif";
          if (saveFileDialog.ShowDialog() != DialogResult.Cancel)
          {
            bit.Save(saveFileDialog.FileName);
          }
        }
        else
        {
          Clipboard.SetImage(bit);
        }
        g.Dispose();
      }
      this.WindowState = FormWindowState.Normal;
      this.FormBorderStyle = FormBorderStyle.FixedSingle;
      panel1.Visible = true;
      this.Opacity = 1;
      mouseDown = false;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
      if (mouseDown)
      {
        if (size.Width != 0 && size.Height != 0 && havePainted)
        {
          ControlPaint.DrawReversibleFrame(new Rectangle(start1, size), Color.Transparent, FrameStyle.Dashed);
        }
        end1 = e.Location;
        size.Width = Math.Abs(end1.X - start.X);
        size.Height = Math.Abs(end1.Y - start.Y);
        start1.X = (start.X > end1.X) ? end1.X : start.X;
        start1.Y = (start.Y > end1.Y) ? end1.Y : start.Y;

        if (size.Width != 0 && size.Height != 0)
        {
          ControlPaint.DrawReversibleFrame(new Rectangle(start1, size), Color.Transparent, FrameStyle.Dashed);
          havePainted = true;
        }
      }
    }

    private void button2_Click(object sender, EventArgs e)
    {
      ReadyToCaptrue();
      saveFile = false;
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void ReadyToCaptrue()
    {
      this.Opacity = 0.1;
      panel1.Visible = false;
      this.FormBorderStyle = FormBorderStyle.None;
      this.WindowState = FormWindowState.Maximized;
    }
  }
}

C#是什么

C#是一個簡單、通用、面向?qū)ο蟮木幊陶Z言,它由微軟Microsoft開發(fā),繼承了C和C++強大功能,并且去掉了一些它們的復雜特性,C#綜合了VB簡單的可視化操作和C++的高運行效率,以其強大的操作能力、優(yōu)雅的語法風格、創(chuàng)新的語言特性和便捷的面向組件編程從而成為.NET開發(fā)的首選語言,但它不適用于編寫時間急迫或性能非常高的代碼,因為C#缺乏性能極高的應用程序所需要的關(guān)鍵功能。

感謝你能夠認真閱讀完這篇文章,希望小編分享的“C#如何實現(xiàn)小截屏軟件功能”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI