![]() 2. 布置主窗體: 在窗體上添加一個圖片框(pictureBox)控件以及一個按鈕(button)控件即可。將主窗體的Text屬性設置為“屏幕捕捉程序”;將圖片框的Image屬性設置為一個圖形文件;將按鈕的Text屬性設置為“屏幕捕捉”即可。(當然讀者可以添加自己需要的控件來布置主窗體)圖示如下: ![]() 3. 代碼編寫: 先在代碼文件中添加上面介紹的BitBlt函數的外部聲明。 再添加按鈕的OnClick事件處理函數,該函數就是實現屏幕捕捉的主要部分了。函數如下: private void button1_Click(object sender, System.EventArgs e) { Graphics g1 = this.CreateGraphics();//獲得窗體圖形對象 Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1); Graphics g2 = Graphics.FromImage(MyImage);//創建位圖圖形對象 IntPtr dc1 = g1.GetHdc();//獲得窗體的上下文設備 IntPtr dc2 = g2.GetHdc();//獲得位圖文件的上下文設備 BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);//寫入到位圖 g1.ReleaseHdc(dc1);//釋放窗體的上下文設備 g2.ReleaseHdc(dc2);//釋放位圖文件的上下文設備 MyImage.Save(@"c:\Captured.jpg", ImageFormat.Jpeg);//保存為jpeg文件 MessageBox.Show("保存圖片結束!"); } 4.到此為止,程序已經做完了。按Ctrl+F5可以試試效果如下圖: ![]() 圖片已經保存好了,看看屏幕捕捉的結果吧(如下圖)! ![]() 不過,這個程序只是捕捉到了程序自身的客戶區,所以功能有限。當然,你也可以試著做個捕捉屏幕任何位置的程序!你只要改變源圖像的寬度和高度即可,而這個寬度和高度當然可以由用戶來選定。這樣,一個自制的屏幕捕捉程序就出爐了。 從上面的實例中,我們不難發現用C#編程實現一些基本功能還是非常容易的,真可謂是高效開發的好工具。所以,筆者希望有更多的人加入C#的行列,開發出更實用、更完善的軟件。 完整代碼: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Imaging; namespace FormCapture { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.Button button1; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); this.button1 = new System.Windows.Forms.Button(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.SuspendLayout(); // // button1 // this.button1.BackColor = System.Drawing.SystemColors.ActiveBorder; this.button1.ForeColor = System.Drawing.SystemColors.ControlDarkDark; this.button1.Location = new System.Drawing.Point(272, 19); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(72, 27); this.button1.TabIndex = 4; this.button1.Text = "屏幕捕捉"; this.button1.Click += new System.EventHandler(this.button1_Click); // // pictureBox1 // this.pictureBox1.Image = ((System.Drawing.Bitmap)(resources.GetObject("pictureBox1.Image"))); this.pictureBox1.Location = new System.Drawing.Point(16, 16); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(240, 224); this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(358, 255); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1, this.pictureBox1}); this.KeyPreview = true; this.Name = "Form1"; this.Text = "屏幕捕捉程序"; this.ResumeLayout(false); } #endregion [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")] private static extern bool BitBlt( IntPtr hdcDest, //目的DC的句柄 int nXDest, //目的圖形的左上角的x坐標 int nYDest, //目的圖形的左上角的y坐標 int nWidth, //目的圖形的矩形寬度 int nHeight, //目的圖形的矩形高度 IntPtr hdcSrc, //源DC的句柄 int nXSrc, //源圖形的左上角的x坐標 int nYSrc, //源圖形的左上角的x坐標 System.Int32 dwRop //光柵操作代碼 ); private void button1_Click(object sender, System.EventArgs e) { Graphics g1 = this.CreateGraphics();//獲得窗體圖形對象 Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1); Graphics g2 = Graphics.FromImage(MyImage);//創建位圖圖形對象 IntPtr dc1 = g1.GetHdc();//獲得窗體的上下文設備 IntPtr dc2 = g2.GetHdc();//獲得位圖文件的上下文設備 BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);//寫入到位圖 g1.ReleaseHdc(dc1);//釋放窗體的上下文設備 g2.ReleaseHdc(dc2);//釋放位圖文件的上下文設備 MyImage.Save(@"c:\Captured.jpg", ImageFormat.Jpeg);//保存為jpeg文件 MessageBox.Show("保存圖片結束!"); } /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } } } (出處:eNet硅谷動力) |
溫馨提示:喜歡本站的話,請收藏一下本站!