winform实现截图
生活随笔
收集整理的這篇文章主要介紹了
winform实现截图
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
這個(gè)截圖模仿QQ截圖,左鍵單擊開始截圖,鼠標(biāo)移動(dòng)出現(xiàn)方框確定截圖尺寸,放開時(shí)為最終尺寸,雙擊鼠標(biāo)彈出保存對(duì)話框進(jìn)行保存。
還有一點(diǎn)就是,如果截圖尺寸方框已經(jīng)確定,移動(dòng)鼠標(biāo)到所選區(qū)域內(nèi)時(shí),鼠標(biāo)會(huì)變手型,可以拖動(dòng)該方框到任意地方進(jìn)行截圖。
建立ScreenCutter解決方案,在其下建立兩個(gè)windows窗體,一個(gè)為MainForm,一個(gè)為ScreenBody。
在MainForm中放一個(gè)按鈕,點(diǎn)擊按鈕時(shí),獲取整個(gè)桌面為背景圖片,調(diào)用ScreenBody。
MainForm的代碼為:
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace MainForm
{public partial class MainForm : Form{public MainForm(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){Image img = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);Graphics g = Graphics.FromImage(img);g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);ScreenBody body = new ScreenBody();body.BackgroundImage = img;body.Show(); }}
} 然后設(shè)計(jì)ScreenBody,設(shè)置FormBorderStyle屬性為None,再調(diào)用load、mousedoubleclick、mousedown、mousemove和mouseup事件即可。
代碼如下:
?
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;namespace MainForm
{public partial class ScreenBody : Form{public ScreenBody(){InitializeComponent();}private Graphics MainPainter; //主畫面private Pen pen; //畫筆private bool isDowned; //判斷鼠標(biāo)是否按下 private bool RectReady; //矩形是否繪制完成 private Image baseImage; //基本圖形(原來(lái)的畫面) private Rectangle Rect; //就是要保存的矩形 private Point downPoint; //鼠標(biāo)按下的點(diǎn) int tmpx;int tmpy;//加載初始化private void ScreenBody_Load(object sender, EventArgs e){this.WindowState = FormWindowState.Maximized;MainPainter = this.CreateGraphics();pen = new Pen(Brushes.Blue);isDowned = false;baseImage = this.BackgroundImage;Rect = new Rectangle();RectReady = false;}//雙擊保存private void ScreenBody_MouseDoubleClick(object sender, MouseEventArgs e){if (e.Button == MouseButtons.Left && Rect.Contains(e.X, e.Y)){Image memory = new Bitmap(Rect.Width, Rect.Height);Graphics g = Graphics.FromImage(memory);g.CopyFromScreen(Rect.X + 1, Rect.Y + 1, 0, 0, Rect.Size);//Clipboard.SetImage(memory);string filePath = null;SaveFileDialog saveFileDialog1 = new SaveFileDialog();saveFileDialog1.RestoreDirectory = true;saveFileDialog1.Filter = "Image files (JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg;*.gif;*.bmp;*.tif; *.tiff; *.png|" +"JPeg files (*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF files (*.gif)|*.gif |BMP files (*.b" +"mp)|*.bmp|Tiff files (*.tif;*.tiff)|*.tif;*.tiff|Png files (*.png)| *.png |All f" +"iles (*.*)|*.*";if (saveFileDialog1.ShowDialog() == DialogResult.OK){filePath = saveFileDialog1.FileName.ToString();memory.Save(filePath, ImageFormat.Jpeg);} this.Close();}}//左擊開始截圖或移動(dòng),右擊撤銷private void ScreenBody_MouseDown(object sender, MouseEventArgs e){if (e.Button == MouseButtons.Left){isDowned = true;if (RectReady == false){Rect.X = e.X;Rect.Y = e.Y;downPoint = new Point(e.X, e.Y);}if (RectReady == true){tmpx = e.X;tmpy = e.Y;}}if (e.Button == MouseButtons.Right){this.Close();return;}}//左鍵放開,截圖方框完成private void ScreenBody_MouseUp(object sender, MouseEventArgs e){if (e.Button == MouseButtons.Left){isDowned = false;RectReady = true;}}//鼠標(biāo)移動(dòng),畫框或者拖動(dòng)private void ScreenBody_MouseMove(object sender, MouseEventArgs e){if (RectReady == false){if (isDowned == true){Image New = DrawScreen((Image)baseImage.Clone(), e.X, e.Y);MainPainter.DrawImage(New, 0, 0);New.Dispose();}}if (RectReady == true){if (Rect.Contains(e.X, e.Y)){this.Cursor = Cursors.Hand;if (isDowned == true){//和上一次的位置比較獲取偏移量 Rect.X = Rect.X + e.X - tmpx;Rect.Y = Rect.Y + e.Y - tmpy;//記錄現(xiàn)在的位置 tmpx = e.X;tmpy = e.Y;MoveRect((Image)baseImage.Clone(), Rect);}}else {this.Cursor = Cursors.Arrow;}}}//畫屏幕private Image DrawScreen(Image back, int Mouse_x, int Mouse_y){Graphics Painter = Graphics.FromImage(back);DrawRect(Painter, Mouse_x, Mouse_y);return back;}//畫矩形private void DrawRect(Graphics Painter, int Mouse_x, int Mouse_y){int width = 0;int heigth = 0;try{if (Mouse_y < Rect.Y){Rect.Y = Mouse_y;heigth = downPoint.Y - Mouse_y;}else{heigth = Mouse_y - downPoint.Y;}if (Mouse_x < Rect.X){Rect.X = Mouse_x;width = downPoint.X - Mouse_x;}else{width = Mouse_x - downPoint.X;}}catch (Exception ee){MessageBox.Show("cuo");}finally{Rect.Size = new Size(width, heigth);Painter.DrawRectangle(pen, Rect);}}//移動(dòng)矩形private void MoveRect(Image image, Rectangle Rect){Graphics Painter = Graphics.FromImage(image);Painter.DrawRectangle(pen, Rect.X, Rect.Y, Rect.Width, Rect.Height);MainPainter.DrawImage(image, 0, 0);image.Dispose();}}
} ?
轉(zhuǎn)載于:https://www.cnblogs.com/jliuwork/p/4084172.html
總結(jié)
以上是生活随笔為你收集整理的winform实现截图的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 乐高玩具多少钱啊?
- 下一篇: Objective-C语法简记