新聞中心
這里有您想知道的互聯(lián)網營銷解決方案
C#做ScreenCapture程序
用C#做Screen Capture程序的代碼和運行節(jié)目:

成都創(chuàng)新互聯(lián)公司專注于新洲網站建設服務及定制,我們擁有豐富的企業(yè)做網站經驗。 熱誠為您提供新洲營銷型網站建設,新洲網站制作、新洲網頁設計、新洲網站官網定制、小程序設計服務,打造新洲網絡公司原創(chuàng)品牌,更為您提供新洲網站排名全網營銷落地服務。
在掌握了一些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 ;
- //導入在程序中使用到的名稱空間
- 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 , //目標設備的句柄
- int nXDest , // 目標對象的左上角的X坐標
- int nYDest , // 目標對象的左上角的X坐標
- int nWidth , // 目標對象的矩形的寬度
- int nHeight , // 目標對象的矩形的長度
- IntPtr hdcSrc , // 源設備的句柄
- int nXSrc , // 源對象的左上角的X坐標
- int nYSrc , // 源對象的左上角的X坐標
- System.Int32 dwRop // 光柵的操作值
- ) ;
- [ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
- private static extern IntPtr CreateDC (
- string lpszDriver , // 驅動名稱
- string lpszDevice , // 設備名稱
- string lpszOutput , // 無用,可以設定位"NULL"
- IntPtr lpInitData // 任意的打印機數(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 ) ;
- //由一個指定設備的句柄創(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 ( ) ;
- //把當前屏幕捕獲到位圖對象中
- BitBlt ( dc2 , 0 , 0 , Screen.PrimaryScreen.Bounds.Width ,
Screen.PrimaryScreen.Bounds.Height , dc3 , 0 , 0 , 13369376 ) ;- //把當前屏幕拷貝到位圖中
- g1.ReleaseHdc ( dc3 ) ;
- //釋放屏幕句柄
- g2.ReleaseHdc ( dc2 ) ;
- //釋放位圖句柄
- MyImage.Save ( "c:\\MyJpeg.jpg" , ImageFormat.Jpeg ) ;
- MessageBox.Show ( "已經把當前屏幕保存到C:\\MyJpeg.jpg文件中!" ) ;
- this.Visible = true ;
- }
- public void ExitSelect ( object sender , System.EventArgs e )
- {
- //隱藏托盤程序中的圖標
- TrayIcon.Visible = false ;
- //關閉系統(tǒng)
- this.Close ( ) ;
- }
- //清除程序中使用過的資源
- public override void Dispose ( )
- {
- base.Dispose ( ) ;
- if ( components != null )
- components.Dispose ( ) ;
- }
- private void InitializeComponent ( )
- {
- //設定托盤程序的各個屬性
- 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 = "捕獲當前屏幕!" ;
- 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 ;
- //為托盤程序加入設定好的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程序
分享標題:C#做ScreenCapture程序
當前路徑:http://www.dlmjj.cn/article/djcjihi.html


咨詢
建站咨詢
