1using System;
2using System.Drawing;
3using System.Collections;
4using System.ComponentModel;
5using System.Windows.Forms;
6using System.Data;
7using System.Runtime.InteropServices;
8
9namespace 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
posted on 2005-04-20 12:04  王员外  阅读(922)  评论(1编辑  收藏  举报