winform实现截图功能

43 篇文章 1 订阅


该程序没有使用API函数.....只是简单为了学习一些知识点...

实现原理:在窗体加载时截取整个屏幕保存为图片,在监视鼠标的事件,鼠标左键拖拉一个矩形框,获取此时

矩形框的左上角坐标和右下角坐标, 这2个坐标即可为要抠取的图片定位,然后使用Clone方法截取指定图片,

并且将该图片复制到粘贴板就OK了。 设置一个窗体,加载时将窗体边框设置为FormBorderStyle.None,并且将

窗体最大化,且不再任务栏显示图标,设置不透明程度 自定义(默认值为1.0),并且在此时截取整个图片。

然后监视鼠标按下的事件:在该事件中获取起始点的坐标,

 再监视鼠标按键放开的事件, 在该事件中获取鼠标放开的坐标,并在此时从最初截取的图片中抠取刚才2个坐

标组成的矩形框范围图片,并保存到粘贴板中即可。

 在监视鼠标的移动事件,在该事件中动态获取高和宽,并用当前屏幕的画板使用指定画刷来填充鼠标拖拉的矩

形范围框。使得用户可以看到自己的截取范围。

 

以下为详细代码:

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
using  System; 
using  System.Collections.Generic; 
using  System.ComponentModel; 
using  System.Data; 
using  System.Drawing; 
using  System.Linq; 
using  System.Text; 
using  System.Windows.Forms; 
using  System.IO; 
using  System.Drawing.Imaging; 
   
namespace  CutScreen 
     public  partial  class  Form1 : Form 
    
         public  Form1() 
        
             InitializeComponent(); 
        
   
         static  int  x = 0; 
         static  int  y = 0; 
         static  int  nowX = 0; 
         static  int  nowY = 0; 
         static  bool  isMouseClick =  false
         static  Graphics g; 
         static  int  width = 0; 
         static  int  height = 0; 
        // static Graphics gi; 
         static  Bitmap bmp; 
         static  string  filename= "1.jpg"
         static  bool  isOneDown =  true
         static  Bitmap bm; 
         private  void  btnCut_Click( object  sender, EventArgs e) 
        
              
        
   
         private  void  Form1_MouseDown( object  sender, MouseEventArgs e) 
        
             if  (isOneDown) 
            
                 x = MousePosition.X; 
                 y = MousePosition.Y; 
                 isMouseClick =  true
                 isOneDown =  false
            
            // MessageBox.Show(MousePosition.X.ToString() + "" + MousePosition.Y.ToString()); 
        
   
         private  void  Form1_MouseUp( object  sender, MouseEventArgs e) 
        
   
             if  (isMouseClick) 
            
                 // MessageBox.Show("放开后鼠标的位置:"+MousePosition.X.ToString() + "" + MousePosition.Y.ToString()); 
                 nowX = MousePosition.X + 1; 
                 nowY = MousePosition.Y + 1; 
   
                 Image newImage = Image.FromFile( "1.jpg" ); 
                 Rectangle destRect =  new  Rectangle(x, y, nowX - x, nowY - y); 
                 bmp =  new  Bitmap(nowX - x, nowY - y); 
                 bm = ((Bitmap)newImage).Clone(destRect, newImage.PixelFormat); 
                 bm.Save( "temp.jpg" ); 
                 newImage.Dispose(); 
                 isMouseClick =  false
            
   
        
   
         private  void  Form1_MouseMove( object  sender, MouseEventArgs e) 
        
             if  (isMouseClick) 
            
                 width = Math.Abs(MousePosition.X - x); 
                 height = Math.Abs(MousePosition.Y - y); 
                 g = CreateGraphics(); 
                 g.Clear(BackColor); 
                 g.FillRectangle(Brushes.Navy, x < MousePosition.X ? x : MousePosition.X, y
 < MousePosition.Y ? y : MousePosition.Y, width + 1, height + 1); 
            
        
   
         private  void  Form1_MouseDoubleClick( object  sender, MouseEventArgs e) 
        
             
             this .Close(); 
             Application.Exit(); 
   
        
   
         private  void  Form1_Load( object  sender, EventArgs e) 
        
   
              
             Size size = Screen.PrimaryScreen.Bounds.Size; 
             Bitmap bmp =  new  Bitmap(size.Width, size.Height); 
             Graphics g = Graphics.FromImage(bmp); 
             g.CopyFromScreen(0, 0, 0, 0, size); 
             //filename = DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss") + ".jpg";  
   
             //注意以下顺序。 
             this .FormBorderStyle = FormBorderStyle.None; 
             this .WindowState = FormWindowState.Maximized; 
             this .ShowInTaskbar =  false
             bmp.Save(filename, ImageFormat.Jpeg); 
             g =  this .CreateGraphics(); 
             this .Opacity = 0.5; 
              
        
         private  void  Form1_FormClosed( object  sender, FormClosedEventArgs e) 
        
               
             Clipboard.SetImage(bm); //将图片保存到粘贴板,不保存实际文件。 
             bm.Dispose(); 
             File.Delete( "temp.jpg" ); 
             File.Delete( "1.jpg" ); 
        
    
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值