这个截图模仿QQ截图,左键单击开始截图,鼠标移动出现方框确定截图尺寸,放开时为最终尺寸,双击鼠标弹出保存对话框进行保存。

还有一点就是,如果截图尺寸方框已经确定,移动鼠标到所选区域内时,鼠标会变手型,可以拖动该方框到任意地方进行截图。

建立ScreenCutter解决方案,在其下建立两个windows窗体,一个为MainForm,一个为ScreenBody。

在MainForm中放一个按钮,点击按钮时,获取整个桌面为背景图片,调用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(); }}
}

然后设计ScreenBody,设置FormBorderStyle属性为None,再调用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;          //判断鼠标是否按下 private bool RectReady;         //矩形是否绘制完成 private Image baseImage;        //基本图形(原来的画面) private Rectangle Rect;         //就是要保存的矩形 private Point downPoint;        //鼠标按下的点 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();}}//左击开始截图或移动,右击撤销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;}}//鼠标移动,画框或者拖动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;//记录现在的位置 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);}}//移动矩形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();}}
}

转载于:https://www.cnblogs.com/jliuwork/p/4084172.html

winform实现截图相关推荐

  1. C#Winform+WindowsAPI做个剪贴板无缝自动保存器(视频截图利器)

    C#Winform+WindowsAPI做个剪贴板无缝自动保存器(视频截图利器) (本文最新代码已上传到GitHub,地址在(https://github.com/bitzhuwei/Clipboar ...

  2. 【WinForm】关于截图识别数字并计算的桌面程序实现方案

    在计算机办公场景中,处理非电子表的统计表文件时,通常需要手动输入数字,用计算器计算,这种操作无需自己效劳,可以拍照上图,或者截图下来,用现有OCR功能识别图中的数字,最后自动完成计算,这些是可以交给计 ...

  3. Winform截图小程序

    今天闲时做的一个Demo,做得并不好,只是实现了最基本的截图功能 主要的思路就是 先打开一个主窗体,点击"截图按钮" 会出现一个半透明的小窗体(可以拉伸放大缩小) 然后利用Grap ...

  4. 【 WinForm】全屏截图,控件截图,句柄截图

    代码 #region 全屏截图private Bitmap ScreenshotFull() {Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds. ...

  5. 我的WCF之旅(6):在Winform Application中调用Duplex Service出现TimeoutException的原因和解决方案...

    几个星期之前写了一篇关于如何通过WCF进行 双向通信的文章([原创]我的WCF之旅(3):在WCF中实现双向通信(Bi-directional Communication) ),在文章中我提供了一个如 ...

  6. Winform下的地图开发控件(GMap.NET)使用心得之二

    在上篇<Winform下的地图开发控件(GMap.NET)使用心得>中简单介绍了GMap.NET的控件基本情况,本篇开始介绍一下相关的代码操作. 其实目前GMap.NET提供的功能还不是很 ...

  7. 分享在winform下实现左右布局多窗口界面

    在web页面上我们可以通过frameset,iframe嵌套框架很容易实现各种导航+内容的布局界面,而在winform.WPF中实现其实也很容易,我这里就分享一个:在winform下实现左右布局多窗口 ...

  8. C#的winform矩阵简单运算

    C#的winform矩阵简单运算 程序截图 关键代码 using System; using System.Collections.Generic; using System.ComponentMod ...

  9. 使用 .NET WinForm 开发所见即所得的 IDE 开发环境,实现不写代码直接生成应用程序...

    GitHub:https://github.com/iccb1013/Sheng.Winform.IDE SailingEase WinForm Framework WinForm开发框架开发手册:h ...

最新文章

  1. 二分查找的循环实现和递归实现
  2. android本地存储SharedPreferences
  3. bash 的进站欢迎提示
  4. 【译】ES2018 新特性: 正则表达式的 s (dotAll) 标志
  5. 与smart_近视激光手术之smart篇
  6. 云+X案例展 | 民生类:中国电信天翼云携手国家天文台打造“大国重器”
  7. GitHub 标星 1.6w+,我发现了一个宝藏项目,推荐大家学习
  8. (转)随机数生成工具
  9. 98.TCP通信传输文件
  10. Centos系统设置
  11. Cisco Webex share screen 显示黑屏解决方法
  12. 小数分频器vhdl实现_verilog 实现小数分频(小数分频器)代码
  13. 洛谷试炼场---新手村
  14. SPSS典型相关分析案例
  15. Android虚拟机多开检测
  16. 帮助海外游客规划从成田机场开始日本旅行的新网站上线
  17. FairyGUI个人使用手册 只有重点(1)
  18. php 根号2计算过程,根号换算(开方计算器在线)
  19. 《2019全球仓储自动化50强企业排行榜》
  20. 远程工作的五个层次 – 以及为什么您可能处于第2层

热门文章

  1. 设置linux初始root密码
  2. java入门 慕路径,Java入门基础知识总结学习教程大全【必看经典】
  3. pytorch生成对抗示例
  4. 音频编解码器以50%的功耗提供两倍的音频质量
  5. 编译器设计-符号表-中间代码生成
  6. TensorRT 加速性能分析
  7. Java的File类
  8. 2021年大数据Flink(四十八):扩展阅读  Streaming File Sink
  9. Android 自定义 —— View moveTo与 rMoveTo 的区别
  10. The Android Gradle plugin supports only Kotlin Gradle plugin version 1.3.0 and higher.