1. 程式人生 > >C#完整的截圖原始碼及註釋

C#完整的截圖原始碼及註釋

               

/*這是一個完整的截圖程式,除錯通過,你可以根據昨天的,視窗殺手程式,也給這個截圖加上熱鍵,和視窗放到系統托盤上,這樣,截圖就不會將程式本身也給顯示在截出來的圖片上了。過兩天,我會寫上這兩個功能的。不過,最好,你能自己也試試。可以在這兒下載http://metababy.chinay.com/sp/metababy/08383052696.rar原始檔的打包*/using System;using System.Drawing;using System.Drawing.Imaging;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;

namespace 截圖{ /// <summary> /// Form1 的摘要說明。 /// </summary> public class Form1 : System.Windows.Forms.Form {  private System.Windows.Forms.Button button1;  private System.Windows.Forms.TextBox textBox1;  private System.Windows.Forms.Label label1;  private System.Windows.Forms.LinkLabel linkLabel1;  private System.Windows.Forms.LinkLabel linkLabel2;  private System.Windows.Forms.LinkLabel linkLabel3;  private System.Windows.Forms.LinkLabel linkLabel4;  /// <summary>  /// 必需的設計器變數。  /// </summary>  private System.ComponentModel.Container components = null;

  public Form1()  {   //   // Windows 窗體設計器支援所必需的   //   InitializeComponent();

   //   // TODO: 在 InitializeComponent 呼叫後新增任何建構函式程式碼   //  }

  /// <summary>  /// 清理所有正在使用的資源。  /// </summary>  protected override void Dispose( bool disposing )  {   if( disposing )   {    if (components != null)     {     components.Dispose();    }   }   base.Dispose( disposing );  }[ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]  private static extern bool BitBlt ( IntPtr hdcDest , // 目標 DC的控制代碼 int nXDest ,  int nYDest ,   int nWidth ,  int nHeight ,  IntPtr hdcSrc ,  // 源DC的控制代碼 int nXSrc ,  int nYSrc ,  System.Int32 dwRop  // 光柵的處理數值 ) ;//這兒,我也沒搞清楚,知其然不知所以然,這些引數的具體意義,我還得找找資料,有朋友知道的,請回覆在評論裡,謝謝  #region Windows 窗體設計器生成的程式碼  /// <summary>  /// 設計器支援所需的方法 - 不要使用程式碼編輯器修改  /// 此方法的內容。  /// </summary>  private void InitializeComponent()  {   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));   this.button1 = new System.Windows.Forms.Button();   this.textBox1 = new System.Windows.Forms.TextBox();   this.label1 = new System.Windows.Forms.Label();   this.linkLabel1 = new System.Windows.Forms.LinkLabel();   this.linkLabel2 = new System.Windows.Forms.LinkLabel();   this.linkLabel3 = new System.Windows.Forms.LinkLabel();   this.linkLabel4 = new System.Windows.Forms.LinkLabel();   this.SuspendLayout();   //    // button1   //    this.button1.Location = new System.Drawing.Point(24, 120);   this.button1.Name = "button1";   this.button1.TabIndex = 0;   this.button1.Text = "截圖";   this.button1.Click += new System.EventHandler(this.button1_Click);   //    // textBox1   //    this.textBox1.Location = new System.Drawing.Point(24, 64);   this.textBox1.Name = "textBox1";   this.textBox1.Size = new System.Drawing.Size(184, 21);   this.textBox1.TabIndex = 1;   this.textBox1.Text = "c://花純春.jpg";   //    // label1   //    this.label1.Location = new System.Drawing.Point(24, 40);   this.label1.Name = "label1";   this.label1.Size = new System.Drawing.Size(168, 23);   this.label1.TabIndex = 2;   this.label1.Text = "請輸入圖片存放位置及名稱";   //    // linkLabel1   //    this.linkLabel1.Location = new System.Drawing.Point(24, 176);   this.linkLabel1.Name = "linkLabel1";   this.linkLabel1.TabIndex = 3;   this.linkLabel1.TabStop = true;   this.linkLabel1.Text = "我的小店";   this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);   //    // linkLabel2   //    this.linkLabel2.Location = new System.Drawing.Point(136, 176);   this.linkLabel2.Name = "linkLabel2";   this.linkLabel2.TabIndex = 4;   this.linkLabel2.TabStop = true;   this.linkLabel2.Text = "我的blog主力站";   this.linkLabel2.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel2_LinkClicked);   //    // linkLabel3   //    this.linkLabel3.Location = new System.Drawing.Point(24, 224);   this.linkLabel3.Name = "linkLabel3";   this.linkLabel3.TabIndex = 5;   this.linkLabel3.TabStop = true;   this.linkLabel3.Text = "我的blog備用站";   this.linkLabel3.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel3_LinkClicked);   //    // linkLabel4   //    this.linkLabel4.Location = new System.Drawing.Point(136, 224);   this.linkLabel4.Name = "linkLabel4";   this.linkLabel4.TabIndex = 6;   this.linkLabel4.TabStop = true;   this.linkLabel4.Text = "我的MSN空間";   this.linkLabel4.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel4_LinkClicked);   //    // Form1   //    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);   this.ClientSize = new System.Drawing.Size(292, 266);   this.Controls.Add(this.linkLabel4);   this.Controls.Add(this.linkLabel3);   this.Controls.Add(this.linkLabel2);   this.Controls.Add(this.linkLabel1);   this.Controls.Add(this.label1);   this.Controls.Add(this.textBox1);   this.Controls.Add(this.button1);   this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));   this.Name = "Form1";   this.Text = "花純春寫的截圖練習程式";   this.ResumeLayout(false);

  }  #endregion

  /// <summary>  /// 應用程式的主入口點。  /// </summary>  [STAThread]  static void Main()   {   Application.Run(new Form1());  }

  private void button1_Click(object sender, System.EventArgs e)  {      try   {

      //獲得當前螢幕的大小    Rectangle rect = new Rectangle ( ) ;    rect = Screen.GetWorkingArea ( this ) ;    //建立一個以當前螢幕為模板的圖象    Graphics g1 = this.CreateGraphics ( ) ;    //建立以螢幕大小為標準的點陣圖     Image MyImage = new Bitmap ( rect.Width , rect.Height , g1 ) ;    Graphics g2 = Graphics.FromImage ( MyImage ) ;    //得到螢幕的DC    IntPtr dc1 = g1.GetHdc ( ) ;    //得到Bitmap的DC     IntPtr dc2 = g2.GetHdc ( ) ;    //呼叫此API函式,實現螢幕捕獲    BitBlt ( dc2 , 0 , 0 , rect.Width , rect.Height , dc1 , 0 , 0 , 13369376 ) ;    //釋放掉螢幕的DC    g1.ReleaseHdc ( dc1 ) ;    //釋放掉Bitmap的DC     g2.ReleaseHdc ( dc2 ) ;    //以JPG檔案格式來儲存    MyImage.Save ( textBox1.Text ,ImageFormat.Jpeg);    string a="當前螢幕已經儲存到"+textBox1.Text;    MessageBox.Show ( a ) ;   }   catch{MessageBox.Show("出錯!");}  }//以下是四個超連結的程式碼,點選就開啟IE視窗,瀏覽以下網頁

  private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)  {   System.Diagnostics.Process.Start("http://shop1471977.taobao.com");

  }

  private void linkLabel2_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)  {   System.Diagnostics.Process.Start("http://riji.163.com/weblog/page/metababy");

  }

  private void linkLabel3_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)  {   System.Diagnostics.Process.Start("http://blog.csdn.net/metababy");

  }

  private void linkLabel4_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)  {   System.Diagnostics.Process.Start("http://spaces.msn.com/members/metababy/");

  } }}