引自 http://www.cnblogs.com/aland-liu/archive/2011/07/20/Winform.html

已经注册博客好久,一直由于工作原因没有打理。今天在网上看了一个截屏的方法思想,感觉不错。就按照这个思路和网友的代码进行整理编写了一个小工具。第一次发博客不足之处,还请高手们批评指正。

废话就不多说放了,代码如下:

截取全屏代码:

  1. try
  2. {
  3. this.Hide();
  4. Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty));
  5. Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
  6. Graphics g = Graphics.FromImage(bitmap);
  7. g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
  8.  
  9. System.Threading.Thread.Sleep();
  10.  
  11. SaveFileDialog saveFileDialog = new SaveFileDialog();
  12. saveFileDialog.Filter = "bmp files (*.bmp)|*.bmp";
  13. saveFileDialog.Title = "保存文件";
  14. saveFileDialog.ShowDialog();
  15. bmpPath = saveFileDialog.FileName;
  16. if ("" != bmpPath)
  17. {
  18. bitmap.Save(bmpPath, ImageFormat.Bmp);
  19. }
  20. bitmap.Dispose();
  21. this.Show();
  22. }
  23. catch (System.Exception ex)
  24. {
  25. MessageBox.Show("抓图失败!");
  26. this.Show();
  27. }

frmChildScreen 窗体代码如下:

  1. private void frmChildScreen_Load(object sender, EventArgs e)
  2. {
  3. this.Cursor = Cursors.Cross;
  4.  
  5. this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
  6. this.UpdateStyles();
  7. originBmp = new Bitmap(this.BackgroundImage);
  8. }
  9.  
  10. private void Catch_MouseDown(object sender, MouseEventArgs e)
  11. {
  12. if (e.Button == MouseButtons.Left)
  13. {
  14. if (!catchStart)
  15. {
  16. catchStart = true;
  17. startPoint = new Point(e.X, e.Y);
  18. }
  19. }
  20. }
  21.  
  22. private void Catch_MouseClick(object sender, MouseEventArgs e)
  23. {
  24. if (e.Button == MouseButtons.Right)
  25. {
  26. this.DialogResult = DialogResult.OK;
  27. this.Close();
  28. }
  29. }
  30.  
  31. private void Catch_MouseMove(object sender, MouseEventArgs e)
  32. {
  33. if (catchStart)
  34. {
  35. Bitmap destBmp = (Bitmap)originBmp.Clone();
  36. Point newPoint = new Point(startPoint.X, startPoint.Y);
  37. Graphics g = Graphics.FromImage(destBmp);
  38. Pen p = new Pen(Color.Blue, );
  39. int width = Math.Abs(e.X - startPoint.X), height = Math.Abs(e.Y - startPoint.Y);
  40. if (e.X < startPoint.X)
  41. {
  42. newPoint.X = e.X;
  43. }
  44. if (e.Y < startPoint.Y)
  45. {
  46. newPoint.Y = e.Y;
  47. }
  48. catchRect = new Rectangle(newPoint, new Size(width, height));
  49. g.DrawRectangle(p, catchRect);
  50. g.Dispose();
  51. p.Dispose();
  52. Graphics g1 = this.CreateGraphics();
  53. g1 = this.CreateGraphics();
  54. g1.DrawImage(destBmp, new Point(, ));
  55. g1.Dispose();
  56. destBmp.Dispose();
  57. }
  58. }
  59.  
  60. private void Catch_MouseUp(object sender, MouseEventArgs e)
  61. {
  62. if (e.Button == MouseButtons.Left)
  63. {
  64. if (catchStart)
  65. {
  66. catchStart = false;
  67. catchFinish = true;
  68. }
  69. }
  70. }
  71.  
  72. private void Catch_MouseDoubleClick(object sender, MouseEventArgs e)
  73. {
  74. if (e.Button == MouseButtons.Left && catchFinish)
  75. {
  76. if (catchRect.Contains(new Point(e.X, e.Y)))
  77. {
  78. Bitmap bitmap = new Bitmap(catchRect.Width, catchRect.Height);
  79. Graphics g = Graphics.FromImage(bitmap);
  80. g.DrawImage(originBmp, new Rectangle(, , bitmap.Width, bitmap.Height), catchRect, GraphicsUnit.Pixel);
  81.  
  82. SaveFileDialog saveFileDialog = new SaveFileDialog();
  83. saveFileDialog.Filter = "bmp files (*.bmp)|*.bmp";
  84. saveFileDialog.Title = "保存文件";
  85. saveFileDialog.ShowDialog();
  86. bmpPath = saveFileDialog.FileName;
  87. if ("" != bmpPath)
  88. {
  89. bitmap.Save(bmpPath, ImageFormat.Bmp);
  90. }
  91. bitmap.Dispose();
  92. this.DialogResult = DialogResult.OK;
  93. this.Close();
  94. }
  95. }
  96. }

截取局部屏幕代码如下:

  1. try
  2. {
  3. this.Hide();
  4. Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty));
  5.  
  6. Thread.Sleep();
  7. frmChildScreen CatchForm = new frmChildScreen();
  8. Bitmap catchBmp = new Bitmap(bounds.Width, bounds.Height);
  9. Graphics g = Graphics.FromImage(catchBmp);
  10. g.CopyFromScreen(new Point(, ), new Point(, ), new Size(Screen.AllScreens[].Bounds.Width, Screen.AllScreens[].Bounds.Height));
  11. CatchForm.BackgroundImage = catchBmp;
  12. if (CatchForm.ShowDialog() == DialogResult.OK)
  13. {
  14. this.Show();
  15. }
  16.  
  17. }
  18. catch (System.Exception e)
  19. {
  20. MessageBox.Show("抓图失败!");
  21. this.Show();
  22. }

winform截屏的更多相关文章

  1. C#截屏

    本实例代码实现了WinForm截屏保存为图片,亲测可行. 界面截图: 下载:http://hovertree.com/h/bjaf/scjyuanma.htm 以下代码可以实际运行,在项目HoverT ...

  2. C#在截屏时将截屏之前需要隐藏的控件也截入

    最近我在项目中遇到一个让我十分头疼的问题,就是我在截屏时也将截屏之前隐藏的控件也截入了. 情况:我在Winform窗体有个截屏功能按钮,实现在调用WPF全屏后截屏,但在截屏WPF界面前将界面里的一个L ...

  3. 【WPF】WPF截屏

    原文:[WPF]WPF截屏 引言 .NET的截图控件在网上流传得不多啊,难得发现一个精品截图控件( 传送门),但是无奈是winform的.后来又找到一个周银辉做的WPF截图(继续传送门),发现截屏是实 ...

  4. C#使用phantomjs 进行网页整页截屏

    C#使用phantomjs 进行网页整页截屏 hantomjs 是一个基于js的webkit内核无头浏览器 也就是没有显示界面的浏览器,这样访问网页就省去了浏览器的界面绘制所消耗的系统资源,比较适合用 ...

  5. Linux系统安装MySql步骤及截屏

    ➠更多技术干货请戳:听云博客 如下是我工作中的记录,介绍的是linux系统下使用官方编译好的二进制文件进行安装MySql的安装过程和安装截屏,这种安装方式速度快,安装步骤简单! 需要的朋友可以按照如下 ...

  6. Android使用C++截屏并显示

    使用android底层自带的截屏源码进行修改后,将截取屏幕的内容再次显示在屏幕上,使屏幕呈现出暂停的效果. android自带的截屏代码在android\JB\frameworks\base\cmds ...

  7. FFmpeg 转码和截屏

    转码 (flv转码为MP4,libx264是MP4编码格式 , -b 3000k是码率,比特率) ffmpeg -i /home/ghr/mp4/mp4.flv -vcodec libx264 -b ...

  8. 使用Python中PIL图形库进行截屏

    目的:通过使用Python的一个图形库PIL(Python Image Library)对屏幕进行截图 步骤: 1.下载PIL(路径)并安装 2.新建文件“截屏.py”,右键Edit with IDL ...

  9. MonoGame 3.2 下,截屏与 Texture2D 的保存

    10月20日注:后来发现了这篇博文(英文),XNA 中的 Color 实际上是与 Alpha 值自左乘(premultiplied)的,这也解释了直接用 0xARGB 转译而颜色异常的原因. 注意,由 ...

随机推荐

  1. div在IE6中固定

    在IE6中固定一div在右下角,但是ie6不支持position:fixed属性,那么只能通过js实现,通过js判断浏览器在ie6的情况下,div的position为absoluate:right:0 ...

  2. SQL Server 调优系列基础篇 - 并行运算总结(二)

    前言 上一篇文章我们介绍了查看查询计划的并行运行方式. 本篇我们接着分析SQL Server的并行运算. 闲言少叙,直接进入本篇的正题. 技术准备 同前几篇一样,基于SQL Server2008R2版 ...

  3. Vue 就地复用策略注意事项

    ---template部分 div el-popover(ref="message", placement="top-start", title="标 ...

  4. bzoj1089

    题解: 递推 f[i]=f[i-1]^n+1 ans=f[d]-f[d-1] 代码: #include<bits/stdc++.h> using namespace std; int n, ...

  5. Activity与Service数据交互:Binder、bindService的用法

    package com.lixu.jiaohu; import com.lixu.jiaohu.MyAppService.Mybind; import android.app.Activity; im ...

  6. 软工作业No.4

    2048小游戏—设计开发 软件需求规格说明书   甜美女孩 2018年10月 ——————————————————————————— 文档修改记录 日期 版本 说明 作者 2018-10-18 V1. ...

  7. cmake 手册详解

    cmake 手册中文版,后续再相关博客的其他链接中,感谢翻译的好人! https://www.cnblogs.com/coderfenghc/archive/2012/06/16/CMake_ch_0 ...

  8. python 元组(tuple)

    面试python我想每个人都会被问一个问题,就是python中的list和tuple有什么区别? 一般情况下,我会回答,list元素可变,tuple元素不可变(书上或者其他的博客都是这么写的),一般情 ...

  9. Vue 2.0 学习路线

    「 Vue很难学吗 」 对于我这种从0.x版本就开始体验 vuejs 的人来说,当然不算难,那时候没各种脚手架和复杂搭配,仅仅是一个mvvm的解决方案库而已,解决了jq带来的繁琐操作dom痛点,所以就 ...

  10. 【linux】wifi不能使用的问题

    Worked solution (Requirements: kernel >=4.11) : Download driver directory from this repo:https:// ...