1
using System;
2
using System.Drawing;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Windows.Forms;
6
using System.Data;
7
using System.Runtime.InteropServices;
8
9
namespace takeScreen
10
{
11
/// <summary>
12
/// Form1 的摘要说明。
13
/// </summary>
14
public class frmScreen : System.Windows.Forms.Form
15
{
16
private System.Windows.Forms.Button btnOK;
17
/// <summary>
18
/// 必需的设计器变量。
19
/// </summary>
20
private System.ComponentModel.Container components = null;
21
22
public frmScreen()
23
{
24
//
25
// Windows 窗体设计器支持所必需的
26
//
27
InitializeComponent();
28
29
//
30
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
31
//
32
}
33
34
/// <summary>
35
/// 清理所有正在使用的资源。
36
/// </summary>
37
protected override void Dispose( bool disposing )
38
{
39
if( disposing )
40
{
41
if (components != null)
42
{
43
components.Dispose();
44
}
45
}
46
base.Dispose( disposing );
47
}
48
49
#region Windows 窗体设计器生成的代码
50
/// <summary>
51
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
52
/// 此方法的内容。
53
/// </summary>
54
private void InitializeComponent()
55
{
56
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmScreen));
57
this.btnOK = new System.Windows.Forms.Button();
58
this.SuspendLayout();
59
//
60
// btnOK
61
//
62
this.btnOK.BackColor = System.Drawing.Color.Transparent;
63
this.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
64
this.btnOK.ForeColor = System.Drawing.Color.White;
65
this.btnOK.Location = new System.Drawing.Point(48, 56);
66
this.btnOK.Name = "btnOK";
67
this.btnOK.TabIndex = 1;
68
this.btnOK.Text = "截屏";
69
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
70
//
71
// frmScreen
72
//
73
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
74
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
75
this.ClientSize = new System.Drawing.Size(180, 140);
76
this.Controls.Add(this.btnOK);
77
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
78
this.Name = "frmScreen";
79
this.Text = "抓屏";
80
this.ResumeLayout(false);
81
82
}
83
#endregion
84
85
/// <summary>
86
/// 应用程序的主入口点。
87
/// </summary>
88
[STAThread]
89
static void Main()
90
{
91
Application.Run(new frmScreen());
92
}
93
94
private void btnOK_Click(object sender, System.EventArgs e)
95
{
96
//创建当前屏幕的DC对象
97
IntPtr ptr = CreateDC("DISPLAY",null,null,(IntPtr)null );
98
Graphics currentG = Graphics.FromHdc(ptr);
99
//创建以屏幕大小为标准的位图对象
100
Image myImage = new Bitmap( Screen.PrimaryScreen.WorkingArea.Width,Screen.PrimaryScreen.WorkingArea.Height, currentG );
101
Graphics imageG = Graphics.FromImage( myImage );
102
//得到屏幕DC
103
IntPtr screenPtr = currentG.GetHdc();
104
//得到位图的DC
105
IntPtr imagePtr = imageG.GetHdc();
106
//截屏
107
BitBlt( imagePtr,0,0,Screen.PrimaryScreen.WorkingArea.Width,Screen.PrimaryScreen.WorkingArea.Height,screenPtr,0,0,13369376 );
108
109
//释放DC
110
currentG.ReleaseHdc( screenPtr );
111
imageG.ReleaseHdc( imagePtr );
112
113
myImage.Save(@"C:\screen.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
114
MessageBox.Show("ok");
115
this.Close();
116
this.Dispose();
117
118
}
119
120
[DllImport("GDI32.Dll")]
121
private static extern bool BitBlt( IntPtr hdcDest,int nXDest, int nYDest,int nWidth,int nHeight,IntPtr hdcSrc,int nXSrc,int nYSrc,Int32 dwRop);
122
[DllImport("GDI32.Dll")]
123
private static extern IntPtr CreateDC( string lpszDrive, string lpszDevice, string lpszOutput, IntPtr lplnitData );
124
125
}
126
}
127
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
124
125
126
127
浙公网安备 33010602011771号