使用C#實(shí)現(xiàn)對任意區(qū)域任意大小的截圖
1.目的
實(shí)現(xiàn)類似系統(tǒng)截圖工具那樣對屏幕任何區(qū)域自定義大小的截圖。
2.效果展示
點(diǎn)擊截圖
選擇需要截圖的區(qū)域:
區(qū)域選擇完成后,單擊右鍵完成截圖:
在合適的載體上粘貼截圖:
3.代碼
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.WindowState = FormWindowState.Minimized; //獲取屏幕截屏 Bitmap bcgImg = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); using (Graphics g= Graphics.FromImage(bcgImg)) { g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(bcgImg.Width, bcgImg.Height)); } //將圖片傳給截圖窗口 CaptureFrm frm = new CaptureFrm(bcgImg); frm.TopMost = true; if( frm.ShowDialog()== DialogResult.OK) { MessageBox.Show("截圖已保存至剪貼板,請選擇合適的載體進(jìn)行粘貼!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("取消截圖","提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } this.WindowState = FormWindowState.Normal; } }
public partial class CaptureFrm : Form { Bitmap bcgImg; bool drawingFlag=false; Point startPoint; Point endPoint; Graphics main_g; bool isCapture=false; public CaptureFrm(Bitmap img) { InitializeComponent(); bcgImg = img; } private void CaptureFrm_Load(object sender, EventArgs e) { //背景 this.BackgroundImage = bcgImg; this.BackgroundImageLayout = ImageLayout.Stretch; } private void CaptureFrm_KeyDown(object sender, KeyEventArgs e) { //如果按下ESC鍵則退出 if (e.KeyCode == Keys.Escape) { // this.Close(); DialogResult = DialogResult.Cancel; } } private void CaptureFrm_MouseDown(object sender, MouseEventArgs e) { if(e.Button== MouseButtons.Left) { drawingFlag = true; startPoint = e.Location; main_g = this.CreateGraphics(); } if(e.Button== MouseButtons.Right) { if (isCapture) { Bitmap map = new Bitmap(width, height); Bitmap source = new Bitmap(bcgImg, new Size(this.Width, this.Height)); using (Graphics g = Graphics.FromImage(map)) { g.DrawImage(source, new Rectangle(0, 0, map.Width, map.Height), new Rectangle(recX, recY, width, height), GraphicsUnit.Pixel); } Clipboard.SetImage(map); main_g.Dispose(); DialogResult = DialogResult.OK; isCapture = false; } } } private void CaptureFrm_MouseUp(object sender, MouseEventArgs e) { if(e.Button== MouseButtons.Left) { //進(jìn)行最終邊界確認(rèn) endPoint = e.Location; drawingFlag = false; isCapture = true; } } int recX, recY, width, height; private void CaptureFrm_MouseMove(object sender, MouseEventArgs e) { if (!drawingFlag ||main_g==null) return; width = Math.Abs(startPoint.X - e.X); height = Math.Abs(startPoint.Y - e.Y); if (startPoint.X < e.X) { recX = startPoint.X; } else { recX = startPoint.X-width; } if (startPoint.Y < e.Y) { recY = startPoint.Y; } else { recY = startPoint.Y-height; } CaptureFrm_Paint(null, null); Pen pen = new Pen(Color.Green, 2); pen.DashPattern = new float[] { 1, 2 }; pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; main_g.DrawRectangle(pen, recX, recY, width, height); string ss = $"X:{recX},Y:{recY}\n width:{width},height:{height}"; main_g.DrawString(ss, new Font("宋體", 12,FontStyle.Bold), Brushes.Red, new Point(10, 10)); } private void CaptureFrm_Paint(object sender, PaintEventArgs e) { if (main_g == null) return; // main_g.Clear(Color.WhiteSmoke); main_g.DrawImage(bcgImg, new Rectangle(0, 0, this.Width, this.Height), new Rectangle(0, 0, bcgImg.Width, bcgImg.Height), GraphicsUnit.Pixel); } private void CaptureFrm_DoubleClick(object sender, EventArgs e) { } }
到此這篇關(guān)于使用C#實(shí)現(xiàn)對任意區(qū)域任意大小的截圖的文章就介紹到這了,更多相關(guān)C#截圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于C#實(shí)現(xiàn)Windows服務(wù)的方法詳解
在實(shí)際應(yīng)用過程中,有時候我們希望開發(fā)的程序,不需要界面,直接開機(jī)就可以長時間運(yùn)行,這時候,我們可以考慮做成一個Windows服務(wù)。這篇文章跟大家介紹一下,如何基于C#實(shí)現(xiàn)Windows服務(wù)的創(chuàng)建、安裝、啟動、停止和卸載,需要的可以參考一下2022-09-09c# 獲取網(wǎng)頁中指定的字符串信息的實(shí)例代碼
c# 獲取網(wǎng)頁中指定的字符串信息的實(shí)例代碼,需要的朋友可以參考一下2013-04-04C#使用自定義的泛型節(jié)點(diǎn)類實(shí)現(xiàn)二叉樹類
這篇文章主要為大家詳細(xì)介紹了C#如何使用自定義的泛型節(jié)點(diǎn)類 Node<T>實(shí)現(xiàn)二叉樹類BinaryTree<T>及其方法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03C#使用RegNotifyChangeKeyValue監(jiān)聽注冊表更改的方法小結(jié)
RegNotifyChangeKeyValue的最后一個參數(shù)傳遞false,表示以同步的方式監(jiān)聽,這篇文章主要介紹了C#使用RegNotifyChangeKeyValue監(jiān)聽注冊表更改的方法小結(jié),需要的朋友可以參考下2024-06-06詳細(xì)介紹C#之文件校驗(yàn)工具的開發(fā)及問題
目前校驗(yàn)文件使用最多的是MD值和SHA值,不外乎有些使用CRC,前段時間微軟發(fā)布了VisualStudio正式版,win鏡像,微軟官方給出的校驗(yàn)方式都是校驗(yàn)文件的SHA值。下面詳細(xì)介紹C#之文件校驗(yàn)工具的開發(fā)及問題,需要的朋友可以參考下2015-07-07