公司里不知道搞了網絡什么限制,我這個機器自古以來上午都登不上騰訊QQ客戶端,每次都是上午用WebQQ,中午吃完飯回來才能登客戶端。WebQQ用起來慢,而且卡不說,有時候截個圖都沒有客戶端來的便捷。從網上雖然下載了一些截圖軟件,用起來都不大方便,摸索着自己從網上找了些關於C#截圖方面的核心代碼(from:http://blog.csdn.net/crystal_lz/article/details/8274277),打造了一款屬於自己的Winform截圖工具。
自己做的這款截圖工具自我感覺用起來挺好的,大概介紹下使用方法,抓圖的過程和其他的一樣,抓完圖后,雙擊左鍵復制到粘貼板,也可以在菜單里面操作,按ESC退出當前截圖。
先上個主界面和截圖界面如下(截圖軟件就這么簡單,自己閑來沒事,在winform上模仿了個百度搜索,雖然畫蛇添足,放上去后感覺給界面還添加了點靈氣……):
下面分享下核心代碼:
- 左鍵按下開始繪圖代碼
1 /// <summary> 2 /// 按下左鍵開始繪制 3 /// </summary> 4 private void imageProcessBox1_MouseDown(object sender, MouseEventArgs e) { 5 if (imageProcessBox1.Cursor != Cursors.SizeAll && 6 imageProcessBox1.Cursor != Cursors.Default) 7 panel1.Visible = false; //表示改變選取大小 隱藏工具條 8 //若果在選取類點擊 並且有選擇工具 9 if (e.Button == MouseButtons.Left && imageProcessBox1.IsDrawed && HaveSelectedToolButton()) { 10 if (imageProcessBox1.SelectedRectangle.Contains(e.Location)) { 11 m_ptOriginal = e.Location; 12 if (tBtn_Text.IsSelected) { //如果選擇的是繪制文本 彈出文本框 13 textBox1.Location = e.Location; 14 textBox1.Visible = true; 15 textBox1.Focus(); 16 return; 17 } 18 m_isStartDraw = true; 19 Cursor.Clip = imageProcessBox1.SelectedRectangle; 20 } 21 } 22 }
- 移動鼠標開始截圖代碼
1 /// <summary> 2 /// 移動鼠標獲取截圖區域 3 /// </summary> 4 private void imageProcessBox1_MouseMove(object sender, MouseEventArgs e) { 5 m_ptCurrent = e.Location; 6 //根據是否選擇有工具決定 鼠標指針樣式 7 if (imageProcessBox1.SelectedRectangle.Contains(e.Location) && HaveSelectedToolButton() && imageProcessBox1.IsDrawed) 8 this.Cursor = Cursors.Cross; 9 else if (!imageProcessBox1.SelectedRectangle.Contains(e.Location)) 10 this.Cursor = Cursors.Default; 11 12 if (imageProcessBox1.IsStartDraw && panel1.Visible) //在重置選取的時候 重置工具條位置(成立於移動選取的時候) 13 this.SetToolBarLocation(); 14 15 if (m_isStartDraw) { //如果在區域內點下那么繪制相應圖形 16 using (Graphics g = Graphics.FromImage(m_bmpLayerShow)) { 17 int tempWidth = 1; 18 if (toolButton2.IsSelected) tempWidth = 3; 19 if (toolButton3.IsSelected) tempWidth = 5; 20 Pen p = new Pen(colorBox1.SelectedColor, tempWidth); 21 22 #region 繪制矩形 23 24 if (tBtn_Rect.IsSelected) { 25 int tempX = e.X - m_ptOriginal.X > 0 ? m_ptOriginal.X : e.X; 26 int tempY = e.Y - m_ptOriginal.Y > 0 ? m_ptOriginal.Y : e.Y; 27 g.Clear(Color.Transparent); 28 g.DrawRectangle(p, tempX - imageProcessBox1.SelectedRectangle.Left, tempY - imageProcessBox1.SelectedRectangle.Top, Math.Abs(e.X - m_ptOriginal.X), Math.Abs(e.Y - m_ptOriginal.Y)); 29 imageProcessBox1.Invalidate(); 30 } 31 32 #endregion 33 34 #region 繪制圓形 35 36 if (tBtn_Ellipse.IsSelected) { 37 g.DrawLine(Pens.Red, 0, 0, 200, 200); 38 g.Clear(Color.Transparent); 39 g.DrawEllipse(p, m_ptOriginal.X - imageProcessBox1.SelectedRectangle.Left, m_ptOriginal.Y - imageProcessBox1.SelectedRectangle.Top, e.X - m_ptOriginal.X, e.Y - m_ptOriginal.Y); 40 imageProcessBox1.Invalidate(); 41 } 42 43 #endregion 44 45 #region 繪制箭頭 46 47 if (tBtn_Arrow.IsSelected) { 48 g.Clear(Color.Transparent); 49 System.Drawing.Drawing2D.AdjustableArrowCap lineArrow = 50 new System.Drawing.Drawing2D.AdjustableArrowCap(4, 4, true); 51 p.CustomEndCap = lineArrow; 52 g.DrawLine(p, (Point)((Size)m_ptOriginal - (Size)imageProcessBox1.SelectedRectangle.Location), (Point)((Size)m_ptCurrent - (Size)imageProcessBox1.SelectedRectangle.Location)); 53 imageProcessBox1.Invalidate(); 54 } 55 56 #endregion 57 58 #region 繪制線條 59 60 if (tBtn_Brush.IsSelected) { 61 Point ptTemp = (Point)((Size)m_ptOriginal - (Size)imageProcessBox1.SelectedRectangle.Location); 62 p.LineJoin = System.Drawing.Drawing2D.LineJoin.Round; 63 g.DrawLine(p, ptTemp, (Point)((Size)e.Location - (Size)imageProcessBox1.SelectedRectangle.Location)); 64 m_ptOriginal = e.Location; 65 imageProcessBox1.Invalidate(); 66 } 67 68 #endregion 69 70 p.Dispose(); 71 } 72 } 73 }
-
保存到粘貼板
1 //將圖像保存到剪貼板 2 private void tBtn_Finish_Click(object sender, EventArgs e) { 3 Clipboard.SetImage(m_bmpLayerCurrent); 4 this.Close(); 5 }
-
保存圖片到硬盤
1 private void tBtn_Save_Click(object sender, EventArgs e) { 2 SaveFileDialog saveDlg = new SaveFileDialog(); 3 saveDlg.Filter = "位圖(*.bmp)|*.bmp|JPEG(*.jpg)|*.jpg"; 4 saveDlg.FilterIndex = 1; 5 saveDlg.FileName = "CAPTURE_" + GetTimeString(); 6 if (saveDlg.ShowDialog() == DialogResult.OK) { 7 switch (saveDlg.FilterIndex) { 8 case 1: 9 m_bmpLayerCurrent.Clone(new Rectangle(0, 0, m_bmpLayerCurrent.Width, m_bmpLayerCurrent.Height), 10 System.Drawing.Imaging.PixelFormat.Format24bppRgb).Save(saveDlg.FileName, 11 System.Drawing.Imaging.ImageFormat.Bmp); 12 this.Close(); 13 break; 14 case 2: 15 m_bmpLayerCurrent.Save(saveDlg.FileName, 16 System.Drawing.Imaging.ImageFormat.Jpeg); 17 this.Close(); 18 break; 19 } 20 } 21 }
技術交流QQ群:70762253正在看本人博客的這位童鞋,我看你氣度不凡,談吐間隱隱有王者之氣,日后必有一番作為!旁邊有“推薦”二字,你就順手把它點了吧,相得准,我分文不收;相不准,你也好回來找我!