C#實(shí)現(xiàn)小截屏軟件功能
第一次寫博客,文筆較差,將就看吧
日常生活中會(huì)經(jīng)常使用到截屏功能,使用最多的無(wú)非就是Windows自帶的截圖工具、QQ截圖和PrintScreen鍵,但要達(dá)到截圖到word或保存到文件,需要鼠標(biāo)選擇多次。比如我們想截圖并將圖插入到Word中,不需要保存圖片,我們希望直接點(diǎn)擊截圖按鈕,選擇截圖區(qū)域,Ctrl+V簡(jiǎn)單三步就行了(感覺(jué)說(shuō)了好多廢話),切入正題。
1.基本功能
選擇屏幕區(qū)域后提醒你保存所截的圖片,或直接將所截圖片放到剪切板里(以便用Ctrl+V粘貼)。
2.界面設(shè)計(jì)
界面很簡(jiǎn)單,無(wú)非就是可實(shí)現(xiàn)以上功能的兩個(gè)按鈕和其他文字,見(jiàn)圖:
界面的屬性需要注意幾個(gè)問(wèn)題:
1)窗口設(shè)為固定大小,并禁用窗口最大化(因?yàn)槲覀儾幌M翱诖笮?huì)變)
2)窗口最好設(shè)為頂置
3)把兩個(gè)文字label和兩個(gè)按鈕都放到一個(gè)panel里,以便于后面程序?qū)丶傩缘牟僮?/p>
4)那么大的按鈕,最好改變一下它的樣式,還可以設(shè)置背景為gif動(dòng)圖
3.功能實(shí)現(xiàn)
那么關(guān)鍵問(wèn)題來(lái)了,怎么截圖呢?見(jiàn)下圖
原理其實(shí)很簡(jiǎn)單,就是在點(diǎn)擊按鈕后,窗口變?yōu)槿粮采w在屏幕最上方,并變?yōu)榘胪该?,使你能看到窗口下面的屏幕?nèi)容,然后拖動(dòng)鼠標(biāo)(此時(shí)其實(shí)是在軟件的主窗口上拖動(dòng),這樣就方便程序捕捉鼠標(biāo)坐標(biāo)了),根據(jù)坐標(biāo)在屏幕上繪制選框,接著松開(kāi)鼠標(biāo)后,根據(jù)鼠標(biāo)落下和松開(kāi)的坐標(biāo),截取屏幕,最后保存或復(fù)制到剪切板。
4.敲代碼吧
using System; using System.Windows.Forms; using System.Drawing;//繪圖要用 using System.Threading;//延時(shí)函數(shù)要用 namespace 截屏 { public partial class Form1 : Form { bool mouseDown = false, havePainted = false; Point start, end; Point start1, end1; Size size = new Size(0, 0); bool saveFile = true; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ReadyToCaptrue(); saveFile = true; } private void Form1_MouseDown(object sender, MouseEventArgs e) { start = e.Location; mouseDown = true; } private void Form1_MouseUp(object sender, MouseEventArgs e) { if (size.Width != 0 && size.Height != 0) { ControlPaint.DrawReversibleFrame(new Rectangle(start1, size), Color.Transparent, FrameStyle.Dashed); havePainted = false; } end = e.Location; if (start.X > end.X) { int temp = end.X; end.X = start.X; start.X = temp; } if (start.Y > end.Y) { int temp = end.Y; end.Y = start.Y; start.Y = temp; } this.Opacity = 0.0; Thread.Sleep(200); if (end.X - start.X > 0 && end.Y - start.Y > 0) { Bitmap bit = new Bitmap(end.X - start.X, end.Y - start.Y); Graphics g = Graphics.FromImage(bit); g.CopyFromScreen(start, new Point(0, 0), bit.Size); if (saveFile) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "png|*.png|bmp|*.bmp|jpg|*.jpg|gif|*.gif"; if (saveFileDialog.ShowDialog() != DialogResult.Cancel) { bit.Save(saveFileDialog.FileName); } } else { Clipboard.SetImage(bit); } g.Dispose(); } this.WindowState = FormWindowState.Normal; this.FormBorderStyle = FormBorderStyle.FixedSingle; panel1.Visible = true; this.Opacity = 1; mouseDown = false; } private void Form1_MouseMove(object sender, MouseEventArgs e) { if (mouseDown) { if (size.Width != 0 && size.Height != 0 && havePainted) { ControlPaint.DrawReversibleFrame(new Rectangle(start1, size), Color.Transparent, FrameStyle.Dashed); } end1 = e.Location; size.Width = Math.Abs(end1.X - start.X); size.Height = Math.Abs(end1.Y - start.Y); start1.X = (start.X > end1.X) ? end1.X : start.X; start1.Y = (start.Y > end1.Y) ? end1.Y : start.Y; if (size.Width != 0 && size.Height != 0) { ControlPaint.DrawReversibleFrame(new Rectangle(start1, size), Color.Transparent, FrameStyle.Dashed); havePainted = true; } } } private void button2_Click(object sender, EventArgs e) { ReadyToCaptrue(); saveFile = false; } private void Form1_Load(object sender, EventArgs e) { } private void ReadyToCaptrue() { this.Opacity = 0.1; panel1.Visible = false; this.FormBorderStyle = FormBorderStyle.None; this.WindowState = FormWindowState.Maximized; } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#截圖程序類似騰訊QQ截圖實(shí)現(xiàn)代碼
- 解決C# 截取當(dāng)前程序窗口指定位置截圖的實(shí)現(xiàn)方法
- c#實(shí)現(xiàn)winform屏幕截圖并保存的示例
- 解決C#全屏幕截圖的實(shí)現(xiàn)方法
- C#實(shí)現(xiàn)網(wǎng)頁(yè)截圖功能
- 對(duì)指定的網(wǎng)頁(yè)進(jìn)行截圖的效果 C#版
- c# 控件截圖的簡(jiǎn)單實(shí)例
- C#實(shí)現(xiàn)類似qq的屏幕截圖程序
- C#實(shí)現(xiàn)通過(guò)ffmpeg從flv視頻文件中截圖的方法
- C#實(shí)現(xiàn)屬于自己的QQ截圖工具
相關(guān)文章
C#利用FluentFTP實(shí)現(xiàn)FTP上傳下載功能詳解
FTP作為日常工作學(xué)習(xí)中,非常重要的一個(gè)文件傳輸存儲(chǔ)空間,想必大家都非常的熟悉了,那么如何快速的實(shí)現(xiàn)文件的上傳下載功能呢,本文以一個(gè)簡(jiǎn)單的小例子,簡(jiǎn)述如何通過(guò)FluentFTP實(shí)現(xiàn)文件的上傳和下載功能2023-02-02C#實(shí)現(xiàn)一鍵換IP、重置DNS、網(wǎng)關(guān)及掩碼的方法
這篇文章主要介紹了C#實(shí)現(xiàn)一鍵換IP、重置DNS、網(wǎng)關(guān)及掩碼的方法,很實(shí)用的功能,需要的朋友可以參考下2014-07-07C#自動(dòng)生成漂亮的水晶效果頭像的實(shí)現(xiàn)代碼
這篇文章主要介紹了C#自動(dòng)生成漂亮的水晶效果頭像的實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2013-12-12C#實(shí)現(xiàn)Access通用訪問(wèn)類OleDbHelper完整實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)Access通用訪問(wèn)類OleDbHelper,結(jié)合完整實(shí)例形式分析了C#針對(duì)access數(shù)據(jù)庫(kù)的連接、查詢、遍歷、分頁(yè)顯示等相關(guān)操作技巧,需要的朋友可以參考下2017-02-02