一个截屏的例子
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{
16private System.Windows.Forms.Button btnOK;
17/**//// <summary>
18/// 必需的設計器變量。
19/// </summary>
20 private System.ComponentModel.Container components = null;
21
22public 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{
39if( disposing )
40{
41if (components != null)
42{
43 components.Dispose();
44 }
45 }
46base.Dispose( disposing );
47 }
48
49Windows 窗體設計器生成的代碼#region Windows 窗體設計器生成的代碼
50/**//// <summary>
51/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
52/// 此方法的內容。
53/// </summary>
54 private void InitializeComponent()
55{
56 System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmScreen));
57this.btnOK = new System.Windows.Forms.Button();
58this.SuspendLayout();
59//
60// btnOK
61//
62 this.btnOK.BackColor = System.Drawing.Color.Transparent;
63this.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
64this.btnOK.ForeColor = System.Drawing.Color.White;
65this.btnOK.Location = new System.Drawing.Point(48, 56);
66this.btnOK.Name = "btnOK";
67this.btnOK.TabIndex = 1;
68this.btnOK.Text = "截屏";
69this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
70//
71// frmScreen
72//
73 this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
74this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
75this.ClientSize = new System.Drawing.Size(180, 140);
76this.Controls.Add(this.btnOK);
77this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
78this.Name = "frmScreen";
79this.Text = "抓屏";
80this.ResumeLayout(false);
81
82 }
83#endregion
84
85/**//// <summary>
86/// 應用程序的主入口點。
87/// </summary>
88 [STAThread]
89static void Main()
90{
91 Application.Run(new frmScreen());
92 }
93
94private 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");
115this.Close();
116this.Dispose();
117
118 }
119
120 [DllImport("GDI32.Dll")]
121private 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")]
123private static extern IntPtr CreateDC( string lpszDrive, string lpszDevice, string lpszOutput, IntPtr lplnitData );
124
125 }
126}
127
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{
16private System.Windows.Forms.Button btnOK;
17/**//// <summary>
18/// 必需的設計器變量。
19/// </summary>
20 private System.ComponentModel.Container components = null;
21
22public 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{
39if( disposing )
40{
41if (components != null)
42{
43 components.Dispose();
44 }
45 }
46base.Dispose( disposing );
47 }
48
49Windows 窗體設計器生成的代碼#region Windows 窗體設計器生成的代碼
50/**//// <summary>
51/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
52/// 此方法的內容。
53/// </summary>
54 private void InitializeComponent()
55{
56 System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmScreen));
57this.btnOK = new System.Windows.Forms.Button();
58this.SuspendLayout();
59//
60// btnOK
61//
62 this.btnOK.BackColor = System.Drawing.Color.Transparent;
63this.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
64this.btnOK.ForeColor = System.Drawing.Color.White;
65this.btnOK.Location = new System.Drawing.Point(48, 56);
66this.btnOK.Name = "btnOK";
67this.btnOK.TabIndex = 1;
68this.btnOK.Text = "截屏";
69this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
70//
71// frmScreen
72//
73 this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
74this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
75this.ClientSize = new System.Drawing.Size(180, 140);
76this.Controls.Add(this.btnOK);
77this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
78this.Name = "frmScreen";
79this.Text = "抓屏";
80this.ResumeLayout(false);
81
82 }
83#endregion
84
85/**//// <summary>
86/// 應用程序的主入口點。
87/// </summary>
88 [STAThread]
89static void Main()
90{
91 Application.Run(new frmScreen());
92 }
93
94private 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");
115this.Close();
116this.Dispose();
117
118 }
119
120 [DllImport("GDI32.Dll")]
121private 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")]
123private static extern IntPtr CreateDC( string lpszDrive, string lpszDevice, string lpszOutput, IntPtr lplnitData );
124
125 }
126}
127
轉載于:https://www.cnblogs.com/bluedream/archive/2005/04/20/141198.html
總結
- 上一篇: SQL搜索转为XML搜索
- 下一篇: 两个姐姐嫁人了~~~~~~~~~~`