您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么用C#做Screen Capture程序”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么用C#做Screen Capture程序”吧!
在掌握了一些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)對(duì)象的左上角的X坐標(biāo)
int nYDest , // 目標(biāo)對(duì)象的左上角的X坐標(biāo)
int nWidth , // 目標(biāo)對(duì)象的矩形的寬度
int nHeight , // 目標(biāo)對(duì)象的矩形的長度
IntPtr hdcSrc , // 源設(shè)備的句柄
int nXSrc , // 源對(duì)象的左上角的X坐標(biāo)
int nYSrc , // 源對(duì)象的左上角的X坐標(biāo)
System.Int32 dwRop // 光柵的操作值
) ;
[ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
private static extern IntPtr CreateDC (
string lpszDriver , // 驅(qū)動(dòng)名稱
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 ) ;
//由一個(gè)指定設(shè)備的句柄創(chuàng)建一個(gè)新的Graphics對(duì)象
MyImage = new Bitmap ( Screen.PrimaryScreen.Bounds.Width ,
Screen.PrimaryScreen.Bounds.Height , g1 ) ;//根據(jù)屏幕大小創(chuàng)建一個(gè)與之相同大小的Bitmap對(duì)象
Graphics g2 = Graphics.FromImage ( MyImage ) ;
//獲得屏幕的句柄
IntPtr dc3 = g1.GetHdc ( ) ;
//獲得位圖的句柄
IntPtr dc2 = g2.GetHdc ( ) ;
//把當(dāng)前屏幕捕獲到位圖對(duì)象中
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è)定托盤程序的各個(gè)屬性
TrayIcon = new NotifyIcon ( ) ;
TrayIcon.Icon = mNetTrayIcon ;
TrayIcon.Text = "用C#做Screen Capture程序" ;
TrayIcon.Visible = true ;
//定義一個(gè)MenuItem數(shù)組,并把此數(shù)組同時(shí)賦值給ContextMenu對(duì)象
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對(duì)象
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程序”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)怎么用C#做Screen Capture程序這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。