原本還想給你們介紹介紹畫板中筆的屬性(線條粗細和虛線實線來着),後來由於時間的關係就再也不細說了,想學的朋友能夠本身看看屬性,vs都有解釋的或者你們去這個網址:http://www.cnblogs.com/stg609/archive/2008/03/16/1108407.html 學習一下,我的感受仍是不錯的,由於我就是照着他的學的。html
今天在這主要是說一說QQ截圖的模擬實現,都是模仿。不過我也是作了幾遍的。ide
一、在這裏須要見一個項目,在項目中新建連個窗體(Main 和 Catch)學習
二、把命名爲Catch.而後設置這個窗體的FormBorderStyle爲None,WindowState爲Maximized.(做爲截圖使用)this
三、對其catch窗體進行代碼編輯(事件引用我就不詳細說了) spa
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading; 9 using System.Threading.Tasks; 10 using System.Windows.Forms; 11 12 namespace FromPaint3 13 { 14 public partial class Catch : Form 15 { 16 17 #region 用戶變量 18 private Point DownPoint = Point.Empty;//記錄鼠標按下座標,用來肯定繪圖起點 19 private bool CatchFinished = false;//用來表示是否截圖完成 20 private bool CatchStart = false;//表示截圖開始 21 private Bitmap originBmp;//用來保存原始圖像 22 private Rectangle CatchRect;//用來保存截圖的矩形 23 #endregion 24 25 public Catch() 26 { 27 InitializeComponent(); 28 29 //全屏顯示 30 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; 31 this.WindowState = System.Windows.Forms.FormWindowState.Maximized; 32 } 33 /// <summary> 34 /// 窗體初始化 35 /// </summary> 36 /// <param name="sender"></param> 37 /// <param name="e"></param> 38 private void Catch_Load(object sender, EventArgs e) 39 { 40 this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true); 41 this.UpdateStyles(); 42 //以上兩句是爲了設置控件樣式爲雙緩衝,這能夠有效減小圖片閃爍的問題,關於這個你們能夠本身去搜索下 43 originBmp = new Bitmap(this.BackgroundImage);//BackgroundImage爲全屏圖片,咱們另用變量來保存全屏圖片 44 } 45 46 //鼠標右鍵點擊結束截圖 47 private void Catch_MouseClick(object sender, MouseEventArgs e) 48 { 49 if (e.Button == MouseButtons.Right) 50 { 51 this.DialogResult = DialogResult.OK; 52 this.Close(); 53 } 54 } 55 56 //鼠標左鍵按下時動做 57 private void Catch_MouseDown(object sender, MouseEventArgs e) 58 { 59 if (e.Button == MouseButtons.Left) 60 { 61 if (!CatchStart) 62 {//若是捕捉沒有開始 63 CatchStart = true; 64 DownPoint = new Point(e.X, e.Y);//保存鼠標按下座標 65 } 66 } 67 } 68 69 private void Catch_MouseMove(object sender, MouseEventArgs e) 70 { 71 if (CatchStart) 72 {//若是捕捉開始 73 Bitmap destBmp = (Bitmap)originBmp.Clone();//新建一個圖片對象,並讓它與原始圖片相同 74 Point newPoint = new Point(DownPoint.X, DownPoint.Y);//獲取鼠標的座標 75 Graphics g = Graphics.FromImage(destBmp);//在剛纔新建的圖片上新建一個畫板 76 Pen p = new Pen(Color.Blue, 1); 77 int width = Math.Abs(e.X - DownPoint.X), height = Math.Abs(e.Y - DownPoint.Y);//獲取矩形的長和寬 78 if (e.X < DownPoint.X) 79 { 80 newPoint.X = e.X; 81 } 82 if (e.Y < DownPoint.Y) 83 { 84 newPoint.Y = e.Y; 85 } 86 CatchRect = new Rectangle(newPoint, new Size(width, height));//保存矩形 87 g.DrawRectangle(p, CatchRect);//將矩形畫在這個畫板上 88 g.Dispose();//釋放目前的這個畫板 89 p.Dispose(); 90 Graphics g1 = this.CreateGraphics();//從新新建一個Graphics類 91 //若是以前那個畫板不釋放,而直接g=this.CreateGraphics()這樣的話沒法釋放掉第一次建立的g,由於只是把地址轉到新的g了.如同string同樣 92 g1 = this.CreateGraphics();//在整個全屏窗體上新建畫板 93 g1.DrawImage(destBmp, new Point(0, 0));//將剛纔所畫的圖片畫到這個窗體上 94 //這個也能夠屬於二次緩衝技術,若是直接將矩形畫在窗體上,會形成圖片抖動而且會有無數個矩形. 95 g1.Dispose(); 96 destBmp.Dispose();//要及時釋放,否則內存將會被大量消耗 97 98 } 99 } 100 101 private void Catch_MouseUp(object sender, MouseEventArgs e) 102 { 103 if (e.Button == MouseButtons.Left) 104 { 105 if (CatchStart) 106 { 107 CatchStart = false; 108 CatchFinished = true; 109 110 } 111 } 112 } 113 114 private void Catch_MouseDoubleClick(object sender, MouseEventArgs e) 115 { 116 if (e.Button == MouseButtons.Left && CatchFinished) 117 { 118 if (CatchRect.Contains(new Point(e.X, e.Y))) 119 { 120 Bitmap CatchedBmp = new Bitmap(CatchRect.Width, CatchRect.Height);//新建一個於矩形等大的空白圖片 121 Graphics g = Graphics.FromImage(CatchedBmp); 122 g.DrawImage(originBmp, new Rectangle(0, 0, CatchRect.Width, CatchRect.Height), CatchRect, GraphicsUnit.Pixel); 123 //把orginBmp中的指定部分按照指定大小畫在畫板上 124 Clipboard.SetImage(CatchedBmp);//將圖片保存到剪貼板 125 g.Dispose(); 126 CatchFinished = false; 127 this.BackgroundImage = originBmp; 128 CatchedBmp.Dispose(); 129 this.DialogResult = DialogResult.OK; 130 this.Close(); 131 } 132 } 133 } 134 135 } 136 }
四、既然有截圖了,咱們還須要有截圖功能的使用開關,因此剛開始建的main窗體就有用了(代碼以下)線程
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace FromPaint3 { public partial class main : Form { #region 用戶變量 private Point DownPoint = Point.Empty;//記錄鼠標按下座標,用來肯定繪圖起點 #endregion public main() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void 截屏ToolStripMenuItem_Click(object sender, EventArgs e) { this.Hide();//隱藏當前窗體 Thread.Sleep(50);//讓線程睡眠一段時間,窗體消失須要一點時間 Catch CatchForm = new Catch(); Bitmap CatchBmp = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);//新建一個和屏幕大小相同的圖片 Graphics g = Graphics.FromImage(CatchBmp); g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height));//保存全屏圖片 CatchForm.BackgroundImage = CatchBmp;//將Catch窗體的背景設爲全屏時的圖片 if (CatchForm.ShowDialog() == DialogResult.OK) { //若是Catch窗體結束,就將剪貼板中的圖片放到信息發送框中 IDataObject iData = Clipboard.GetDataObject(); DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Bitmap); if (iData.GetDataPresent(DataFormats.Bitmap)) { richtextbox1.Paste(myFormat); Clipboard.Clear();//清除剪貼板中的對象 } this.Show();//從新顯示窗體 } } } }
這是我main窗體的截圖code
截屏特效我在這用的是右鍵菜單orm
五、如今你就能夠運行程序,看看效果了htm
但願對你們有幫助吧對象
若是沒有運行出來你們能夠去http://www.cnblogs.com/stg609/archive/2008/03/19/1113694.html 看一看
版權聲明:本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。
做者:黯淡の青春 博客地址:http://www.cnblogs.com/wuwangqianxun/