在掌握了一些C#源代碼后,可以得到用C#做Screen Capture程序的源代碼(Capture.cs),具體如下:
usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.componentmodel;
usingSystem.Windows.Forms;
usingSystem.Data;
usingSystem.Drawing.Imaging;
usingSystem.IO;
//導入在程序中使用到的名稱空間
publicclassCapture:Form
{
privateSystem.ComponentModel.Containercomponents=null;
privateIconmNetTrayIcon=newIcon("Tray.ico");
privateBitmapMyImage=null;
privateNotifyIconTrayIcon;
privateContextMenunotifyiconMnu;
publicCapture()
{
//初始化窗體中使用到的組件
InitializeComponent();
}
protectedoverridevoidOnActivated(EventArgse)
{
this.Hide();
}
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
privatestaticexternboolBitBlt(
IntPtrhdcDest,//目標設備的句柄
intnXDest,//目標對象的左上角的X坐標
intnYDest,//目標對象的左上角的X坐標
intnWidth,//目標對象的矩形的寬度
intnHeight,//目標對象的矩形的長度
IntPtrhdcSrc,//源設備的句柄
intnXSrc,//源對象的左上角的X坐標
intnYSrc,//源對象的左上角的X坐標
System.Int32dwRop//光柵的操作值
);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
privatestaticexternIntPtrCreateDC(
stringlpszDriver,//驅動名稱
stringlpszDevice,//設備名稱
stringlpszOutput,//無用,可以設定位"NULL"
IntPtrlpInitData//任意的打印機數據
);
publicvoidcapture(objectsender,System.EventArgse)
{
this.Visible=false;
IntPtrdc1=CreateDC("DISPLAY",null,null,(IntPtr)null);
//創建顯示器的DC
Graphicsg1=Graphics.FromHdc(dc1);
//由一個指定設備的句柄創建一個新的Graphics對象
MyImage=newBitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,g1);//根據屏幕大小創建一個與之相同大小的Bitmap對象
Graphicsg2=Graphics.FromImage(MyImage);
//獲得屏幕的句柄
IntPtrdc3=g1.GetHdc();
//獲得位圖的句柄
IntPtrdc2=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;
}
publicvoidExitSelect(objectsender,System.EventArgse)
{
//隱藏托盤程序中的圖標
TrayIcon.Visible=false;
//關閉系統
this.Close();
}
//清除程序中使用過的資源
publicoverridevoidDispose()
{
base.Dispose();
if(components!=null)
components.Dispose();
}
privatevoidInitializeComponent()
{
//設定托盤程序的各個屬性
TrayIcon=newNotifyIcon();
TrayIcon.Icon=mNetTrayIcon;
TrayIcon.Text="用C#做ScreenCapture程序";
TrayIcon.Visible=true;
//定義一個MenuItem數組,并把此數組同時賦值給ContextMenu對象
MenuItem[]mnuItms=newMenuItem[3];
mnuItms[0]=newMenuItem();
mnuItms[0].Text="捕獲當前屏幕!";
mnuItms[0].Click+=newSystem.EventHandler(this.capture);
mnuItms[1]=newMenuItem("-");
mnuItms[2]=newMenuItem();
mnuItms[2].Text="退出系統";
mnuItms[2].Click+=newSystem.EventHandler(this.ExitSelect);
mnuItms[2].DefaultItem=true;
notifyiconMnu=newContextMenu(mnuItms);
TrayIcon.ContextMenu=notifyiconMnu;
//為托盤程序加入設定好的ContextMenu對象
this.SuspendLayout();
this.AutoScaleBaseSize=newSystem.Drawing.Size(5,13);
this.ClientSize=newSystem.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#做ScreenCapture程序!";
this.ResumeLayout(false);
}
staticvoidMain()
{
Application.Run(newCapture());
}
}
感謝各位的閱讀,以上就是“怎么用C#做Screen Capture程序”的內容了,經過本文的學習后,相信大家對怎么用C#做Screen Capture程序這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是本站,小編將為大家推送更多相關知識點的文章,歡迎關注!
本文由 貴州做網站公司 整理發布,部分圖文來源于互聯網,如有侵權,請聯系我們刪除,謝謝!
c語言中正確的字符常量是用一對單引號將一個字符括起表示合法的字符常量。例如‘a’。數值包括整型、浮點型。整型可用十進制,八進制,十六進制。八進制前面要加0,后面...
2022年天津專場考試原定于3月19日舉行,受疫情影響確定延期,但目前延期后的考試時間推遲。 符合報名條件的考生,須在規定時間登錄招考資訊網(www.zha...
:喜歡聽,樂意看。指很受歡迎?!巴卣官Y料”喜聞樂見:[ xǐ wén lè jiàn ]詳細解釋1. 【解釋】:喜歡聽,樂意看。指很受歡迎。2. 【示例】:這是...
三花轉債上市價格預測是多少?三花智控6月29日收盤價24.01,轉股價格21.55,當前轉股價值111.42。三花智控5月31日(申購日前一工作日)收盤價為22.32,申購日6月1日。在這過去的近一個月時間里,三花智控股價上漲7.6%,轉股價值從103.57上漲到111.42。三花轉債AA+級別,規模30億, 原始股東配售率74.08%,單賬戶頂格申購中0.094簽,溢價率由申購前一日的-3.45...
(資料圖片)最近這段時間總有小伙伴問小編李易峰主演的電視劇有哪些電視劇是什么,小編為此在網上搜尋了一些有關于李易峰主演的電視劇有哪些電視劇的知識送給大家,希望能解答各位小伙伴的疑惑。李易峰,1987年5月4日出生于四川成都,中國內地男演員、流行樂歌手、影視制片人,畢業于四川師范大學電影電視學院。2007年,參加東方衛視選秀娛樂節目《加油!好男兒》的比賽,獲得全國總決賽第八名,從而正式出道。李易峰主...
所謂價格歧視,是指壟斷廠商為了獲得超額利潤,而實行的有差別的價格政策。價格其實分為三個等級,等級數字越小,壟斷廠商賺取的超額利潤就越多。價格歧視的三種類型例子如下:一、一級價格歧視-看人定價不同的人為相同商品或服務所愿意支付的最高價格是不同的。一個旅游紀念品,經濟條件好的人可能愿意花500元去購買,經濟條件一般的人可能最多只愿意出300元。那么為了多掙錢,景區賣旅游紀念品的老板會上下打量每一個詢價...