C#做Screen Capture程序
作者:佚名
這里介紹C#做Screen Capture程序,在掌握了一些C#源代碼后,可以得到用C#做Screen Capture程序的源代碼具體如下。
用C#做Screen Capture程序的代碼和運(yùn)行節(jié)目:
在掌握了一些C#源代碼后,可以得到用C#做Screen Capture程序的源代碼(Capture.cs),具體如下:
- using System ;
- using System.Drawing ;
- using System.Collections ;
- using System.ComponentModel ;
- using System.Windows.Forms ;
- using System.Data ;
- using System.Drawing.Imaging ;
- using System.IO ;
- //導(dǎo)入在程序中使用到的名稱空間
- public class Capture : Form
- {
- private System.ComponentModel.Container components = null ;
- private Icon mNetTrayIcon = new Icon ( "Tray.ico" ) ;
- private Bitmap MyImage = null ;
- private NotifyIcon TrayIcon ;
- private ContextMenu notifyiconMnu ;
- public Capture ( )
- {
- //初始化窗體中使用到的組件
- InitializeComponent ( ) ;
- }
- protected override void OnActivated ( EventArgs e )
- {
- this.Hide ( ) ;
- }
- [ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
- private static extern bool BitBlt (
- IntPtr hdcDest , //目標(biāo)設(shè)備的句柄
- int nXDest , // 目標(biāo)對象的左上角的X坐標(biāo)
- int nYDest , // 目標(biāo)對象的左上角的X坐標(biāo)
- int nWidth , // 目標(biāo)對象的矩形的寬度
- int nHeight , // 目標(biāo)對象的矩形的長度
- IntPtr hdcSrc , // 源設(shè)備的句柄
- int nXSrc , // 源對象的左上角的X坐標(biāo)
- int nYSrc , // 源對象的左上角的X坐標(biāo)
- System.Int32 dwRop // 光柵的操作值
- ) ;
- [ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
- private static extern IntPtr CreateDC (
- string lpszDriver , // 驅(qū)動名稱
- string lpszDevice , // 設(shè)備名稱
- string lpszOutput , // 無用,可以設(shè)定位"NULL"
- IntPtr lpInitData // 任意的打印機(jī)數(shù)據(jù)
- ) ;
- public void capture ( object sender , System.EventArgs e )
- {
- this.Visible = false ;
- IntPtr dc1 = CreateDC ( "DISPLAY" , null , null , ( IntPtr ) null ) ;
- //創(chuàng)建顯示器的DC
- Graphics g1 = Graphics.FromHdc ( dc1 ) ;
- //由一個指定設(shè)備的句柄創(chuàng)建一個新的Graphics對象
- MyImage = new Bitmap ( Screen.PrimaryScreen.Bounds.Width ,
Screen.PrimaryScreen.Bounds.Height , g1 ) ;- //根據(jù)屏幕大小創(chuàng)建一個與之相同大小的Bitmap對象
- Graphics g2 = Graphics.FromImage ( MyImage ) ;
- //獲得屏幕的句柄
- IntPtr dc3 = g1.GetHdc ( ) ;
- //獲得位圖的句柄
- IntPtr dc2 = g2.GetHdc ( ) ;
- //把當(dāng)前屏幕捕獲到位圖對象中
- BitBlt ( dc2 , 0 , 0 , Screen.PrimaryScreen.Bounds.Width ,
Screen.PrimaryScreen.Bounds.Height , dc3 , 0 , 0 , 13369376 ) ;- //把當(dāng)前屏幕拷貝到位圖中
- g1.ReleaseHdc ( dc3 ) ;
- //釋放屏幕句柄
- g2.ReleaseHdc ( dc2 ) ;
- //釋放位圖句柄
- MyImage.Save ( "c:\\MyJpeg.jpg" , ImageFormat.Jpeg ) ;
- MessageBox.Show ( "已經(jīng)把當(dāng)前屏幕保存到C:\\MyJpeg.jpg文件中!" ) ;
- this.Visible = true ;
- }
- public void ExitSelect ( object sender , System.EventArgs e )
- {
- //隱藏托盤程序中的圖標(biāo)
- TrayIcon.Visible = false ;
- //關(guān)閉系統(tǒng)
- this.Close ( ) ;
- }
- //清除程序中使用過的資源
- public override void Dispose ( )
- {
- base.Dispose ( ) ;
- if ( components != null )
- components.Dispose ( ) ;
- }
- private void InitializeComponent ( )
- {
- //設(shè)定托盤程序的各個屬性
- TrayIcon = new NotifyIcon ( ) ;
- TrayIcon.Icon = mNetTrayIcon ;
- TrayIcon.Text = "用C#做Screen Capture程序" ;
- TrayIcon.Visible = true ;
- //定義一個MenuItem數(shù)組,并把此數(shù)組同時賦值給ContextMenu對象
- MenuItem [ ] mnuItms = new MenuItem [ 3 ] ;
- mnuItms [ 0 ] = new MenuItem ( ) ;
- mnuItms [ 0 ] .Text = "捕獲當(dāng)前屏幕!" ;
- mnuItms [ 0 ] .Click += new System.EventHandler ( this.capture ) ;
- mnuItms [ 1 ] = new MenuItem ( "-" ) ;
- mnuItms [ 2 ] = new MenuItem ( ) ;
- mnuItms [ 2 ] .Text = "退出系統(tǒng)" ;
- mnuItms [ 2 ] .Click += new System.EventHandler ( this.ExitSelect ) ;
- mnuItms [ 2 ] .DefaultItem = true ;
- notifyiconMnu = new ContextMenu ( mnuItms ) ;
- TrayIcon.ContextMenu = notifyiconMnu ;
- //為托盤程序加入設(shè)定好的ContextMenu對象
- this.SuspendLayout ( ) ;
- this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;
- this.ClientSize = new System.Drawing.Size ( 320 , 56 ) ;
- this.ControlBox = false ;
- this.MaximizeBox = false ;
- this.MinimizeBox = false ;
- this.WindowState = System.Windows.Forms.FormWindowState.Minimized ;
- this.Name = "capture" ;
- this.ShowInTaskbar = false ;
- this.Text = "用C#做Screen Capture程序!" ;
- this.ResumeLayout ( false ) ;
- }
- static void Main ( )
- {
- Application.Run ( new Capture ( ) ) ;
- }
- }
以上介紹C#做Screen Capture程序
【編輯推薦】
責(zé)任編輯:佚名
來源:
博客園