C#实现小截屏软件功能

时间:2022-05-16 03:29:54

第一次写博客,文笔较差,将就看吧

日常生活中会经常使用到截屏功能,使用最多的无非就是windows自带的截图工具、qq截图和printscreen键,但要达到截图到word或保存到文件,需要鼠标选择多次。比如我们想截图并将图插入到word中,不需要保存图片,我们希望直接点击截图按钮,选择截图区域,ctrl+v简单三步就行了(感觉说了好多废话),切入正题。

1.基本功能

选择屏幕区域后提醒你保存所截的图片,或直接将所截图片放到剪切板里(以便用ctrl+v粘贴)。

2.界面设计

界面很简单,无非就是可实现以上功能的两个按钮和其他文字,见图:

C#实现小截屏软件功能

界面的属性需要注意几个问题:

1)窗口设为固定大小,并禁用窗口最大化(因为我们不希望窗口大小会变)

C#实现小截屏软件功能

C#实现小截屏软件功能

2)窗口最好设为顶置

C#实现小截屏软件功能

3)把两个文字label和两个按钮都放到一个panel里,以便于后面程序对控件属性的操作

4)那么大的按钮,最好改变一下它的样式,还可以设置背景为gif动图

C#实现小截屏软件功能

3.功能实现

那么关键问题来了,怎么截图呢?见下图

C#实现小截屏软件功能

原理其实很简单,就是在点击按钮后,窗口变为全屏覆盖在屏幕最上方,并变为半透明,使你能看到窗口下面的屏幕内容,然后拖动鼠标(此时其实是在软件的主窗口上拖动,这样就方便程序捕捉鼠标坐标了),根据坐标在屏幕上绘制选框,接着松开鼠标后,根据鼠标落下和松开的坐标,截取屏幕,最后保存或复制到剪切板。

4.敲代码吧

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using system;
using system.windows.forms;
using system.drawing;//绘图要用
using system.threading;//延时函数要用
 
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;
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。