C#實現截圖工具小項目_第1頁
C#實現截圖工具小項目_第2頁
C#實現截圖工具小項目_第3頁
C#實現截圖工具小項目_第4頁
C#實現截圖工具小項目_第5頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第C#實現截圖工具小項目//窗體關閉-取消熱鍵

privatevoidForm1_FormClosing(objectsender,FormClosingEventArgse){

HotKey.UnregisterHotKey(Handle,100);

//快捷鍵按下執行的事件

privatevoidGlobalKeyProcess(){

this.WindowState=FormWindowState.Minimized;

Thread.Sleep(200);

button1.PerformClick();

//重寫。監視系統消息,調用對應方法

protectedoverridevoidWndProc(refMessagem){

constintWM_HOTKEY=0x0312;

//如果m.Msg的值為0x0312(我也不知道為什么是0x0312)那么表示用戶按下了熱鍵

switch(m.Msg){

caseWM_HOTKEY:

if(m.WParam.ToString().Equals("100")){

GlobalKeyProcess();

//todo其它熱鍵

break;

//將系統消息傳遞自父類的WndProc

base.WndProc(refm);

}

截圖窗體-核心Cutter.cs

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

usingSystem.Windows.Forms;

namespacetest{

publicpartialclassCutter:Form{

BitmapscreenBtmp=null;//電腦屏幕的截圖

publicCutter(Bitmapbtm){

InitializeComponent();

screenBtmp=btm;

//鼠標右鍵退出

privatevoidCutter_MouseClick(objectsender,MouseEventArgse){

if(e.Button==MouseButtons.Right){

this.DialogResult=DialogResult.OK;

this.Close();

boolCatchStart=false;//自由截圖開始

PointdownPoint;//初始點

//鼠標左鍵按下-開始自由截圖

privatevoidCutter_MouseDown(objectsender,MouseEventArgse){

if(e.Button==MouseButtons.Left){

if(!CatchStart){

CatchStart=true;

downPoint=newPoint(e.X,e.Y);//初始點

RectanglecatchRec;//存放截取范圍

//鼠標移動-繪制自由截圖路徑

privatevoidCutter_MouseMove(objectsender,MouseEventArgse){//路徑繪制,核心

if(CatchStart){

//二次緩沖

//不是直接在控件的背景畫板上進行繪制鼠標移動路徑,那樣會造成繪制很多路徑,因為前面繪制的路徑還在

//而是在內存中每移動一次鼠標就創建一張和屏幕截圖一樣的新BImtap,在這個Bitmap中繪制鼠標移動路徑

//然后在窗體背景畫板上,繪制這個新的Bitmap,這樣就不會造成繪制很多路徑,因為每次都繪制了全新的Bitmao

//但是這樣做的話,因為鼠標移動的次數是大量的,所以在內存中會創建大量的Bitmap會造成內存消耗嚴重,所以每次移動繪制完后,

//需要釋放Dispose()畫板,畫筆,Bitmap資源。

BitmapcopyBtmp=(Bitmap)screenBtmp.Clone();//創建新的,在其上繪制路徑

//左上角

PointfirstP=newPoint(downPoint.X,downPoint.Y);

//新建畫板,畫筆

Graphicsg=Graphics.FromImage(copyBtmp);

Penp=newPen(Color.Red,1);

//計算路徑范圍

intwidth=Math.Abs(e.X-downPoint.X);

intheight=Math.Abs(e.Y-downPoint.Y);

if(e.XdownPoint.X){

firstP.X=e.X;

if(e.YdownPoint.Y){

firstP.Y=e.Y;

//繪制路徑

catchRec=newRectangle(firstP,newSize(width,height));

//將路徑繪制在新的BItmap上,之后要釋放

g.DrawRectangle(p,catchRec);

g.Dispose();

p.Dispose();

//窗體背景畫板

Graphicsgf=this.CreateGraphics();

//將新圖繪制在窗體的畫板上--自由截圖-路徑繪制處,其實還是一張和屏幕同樣大小的圖片,只不過上面有紅色的選擇路徑

gf.DrawImage(copyBtmp,newPoint(0,0));

gf.Dispose();

//釋放內存Bimtap

copyBtmp.Dispose();

boolcatchFinished=false;//自由截圖結束標志

//鼠標左鍵彈起-結束自由截圖

privatevoidCutter_MouseUp(objectsender,MouseEventArgse){

if(e.Button==MouseButtons.Left){

if(CatchStart){

CatchStart=false;

catchFinished=true;

//鼠標左鍵雙擊,保存自由截取的圖片

privatevoidCutter_MouseDoubleClick(objectsender,MouseEventArgse){

if((e.Button==MouseButtons.Left)catchFinished){

//創建用戶截取的范圍大小的空圖

BitmapcatchBtmp=newBitmap(catchRec.Width,catchRec.Height);

Graphicsg=Graphics.FromImage(catchBtmp);

//在原始的屏幕截圖ScreenBitmap上截取用戶選擇范圍大小的區域繪制到上面的空圖

//繪制完后,這個空圖就是我們想要的截取的圖片

//參數1原圖

//參數2在空圖上繪制的范圍區域

//參數3原圖的截取范圍

//參數4度量單位

g.DrawImage(screenBtmp,newRectangle(0,0,catchRec.Width,catchRec.Height),catchRec,GraphicsUnit.Pixel);

//將自由截取的圖片保存到剪切板中

Clipboard.Clear();

Clipboard.SetImage(catchBtmp);

g.Dispose();

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論