winform截屏
引自 http://www.cnblogs.com/aland-liu/archive/2011/07/20/Winform.html
已经注册博客好久,一直由于工作原因没有打理。今天在网上看了一个截屏的方法思想,感觉不错。就按照这个思路和网友的代码进行整理编写了一个小工具。第一次发博客不足之处,还请高手们批评指正。
废话就不多说放了,代码如下:
截取全屏代码:
- try
- {
- this.Hide();
- Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty));
- Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
- Graphics g = Graphics.FromImage(bitmap);
- g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
- System.Threading.Thread.Sleep();
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- saveFileDialog.Filter = "bmp files (*.bmp)|*.bmp";
- saveFileDialog.Title = "保存文件";
- saveFileDialog.ShowDialog();
- bmpPath = saveFileDialog.FileName;
- if ("" != bmpPath)
- {
- bitmap.Save(bmpPath, ImageFormat.Bmp);
- }
- bitmap.Dispose();
- this.Show();
- }
- catch (System.Exception ex)
- {
- MessageBox.Show("抓图失败!");
- this.Show();
- }
frmChildScreen 窗体代码如下:
- private void frmChildScreen_Load(object sender, EventArgs e)
- {
- this.Cursor = Cursors.Cross;
- this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
- this.UpdateStyles();
- originBmp = new Bitmap(this.BackgroundImage);
- }
- private void Catch_MouseDown(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- if (!catchStart)
- {
- catchStart = true;
- startPoint = new Point(e.X, e.Y);
- }
- }
- }
- private void Catch_MouseClick(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Right)
- {
- this.DialogResult = DialogResult.OK;
- this.Close();
- }
- }
- private void Catch_MouseMove(object sender, MouseEventArgs e)
- {
- if (catchStart)
- {
- Bitmap destBmp = (Bitmap)originBmp.Clone();
- Point newPoint = new Point(startPoint.X, startPoint.Y);
- Graphics g = Graphics.FromImage(destBmp);
- Pen p = new Pen(Color.Blue, );
- int width = Math.Abs(e.X - startPoint.X), height = Math.Abs(e.Y - startPoint.Y);
- if (e.X < startPoint.X)
- {
- newPoint.X = e.X;
- }
- if (e.Y < startPoint.Y)
- {
- newPoint.Y = e.Y;
- }
- catchRect = new Rectangle(newPoint, new Size(width, height));
- g.DrawRectangle(p, catchRect);
- g.Dispose();
- p.Dispose();
- Graphics g1 = this.CreateGraphics();
- g1 = this.CreateGraphics();
- g1.DrawImage(destBmp, new Point(, ));
- g1.Dispose();
- destBmp.Dispose();
- }
- }
- private void Catch_MouseUp(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- if (catchStart)
- {
- catchStart = false;
- catchFinish = true;
- }
- }
- }
- private void Catch_MouseDoubleClick(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left && catchFinish)
- {
- if (catchRect.Contains(new Point(e.X, e.Y)))
- {
- Bitmap bitmap = new Bitmap(catchRect.Width, catchRect.Height);
- Graphics g = Graphics.FromImage(bitmap);
- g.DrawImage(originBmp, new Rectangle(, , bitmap.Width, bitmap.Height), catchRect, GraphicsUnit.Pixel);
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- saveFileDialog.Filter = "bmp files (*.bmp)|*.bmp";
- saveFileDialog.Title = "保存文件";
- saveFileDialog.ShowDialog();
- bmpPath = saveFileDialog.FileName;
- if ("" != bmpPath)
- {
- bitmap.Save(bmpPath, ImageFormat.Bmp);
- }
- bitmap.Dispose();
- this.DialogResult = DialogResult.OK;
- this.Close();
- }
- }
- }
截取局部屏幕代码如下:
- try
- {
- this.Hide();
- Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty));
- Thread.Sleep();
- frmChildScreen CatchForm = new frmChildScreen();
- Bitmap catchBmp = new Bitmap(bounds.Width, bounds.Height);
- Graphics g = Graphics.FromImage(catchBmp);
- g.CopyFromScreen(new Point(, ), new Point(, ), new Size(Screen.AllScreens[].Bounds.Width, Screen.AllScreens[].Bounds.Height));
- CatchForm.BackgroundImage = catchBmp;
- if (CatchForm.ShowDialog() == DialogResult.OK)
- {
- this.Show();
- }
- }
- catch (System.Exception e)
- {
- MessageBox.Show("抓图失败!");
- this.Show();
- }
winform截屏的更多相关文章
- C#截屏
本实例代码实现了WinForm截屏保存为图片,亲测可行. 界面截图: 下载:http://hovertree.com/h/bjaf/scjyuanma.htm 以下代码可以实际运行,在项目HoverT ...
- C#在截屏时将截屏之前需要隐藏的控件也截入
最近我在项目中遇到一个让我十分头疼的问题,就是我在截屏时也将截屏之前隐藏的控件也截入了. 情况:我在Winform窗体有个截屏功能按钮,实现在调用WPF全屏后截屏,但在截屏WPF界面前将界面里的一个L ...
- 【WPF】WPF截屏
原文:[WPF]WPF截屏 引言 .NET的截图控件在网上流传得不多啊,难得发现一个精品截图控件( 传送门),但是无奈是winform的.后来又找到一个周银辉做的WPF截图(继续传送门),发现截屏是实 ...
- C#使用phantomjs 进行网页整页截屏
C#使用phantomjs 进行网页整页截屏 hantomjs 是一个基于js的webkit内核无头浏览器 也就是没有显示界面的浏览器,这样访问网页就省去了浏览器的界面绘制所消耗的系统资源,比较适合用 ...
- Linux系统安装MySql步骤及截屏
➠更多技术干货请戳:听云博客 如下是我工作中的记录,介绍的是linux系统下使用官方编译好的二进制文件进行安装MySql的安装过程和安装截屏,这种安装方式速度快,安装步骤简单! 需要的朋友可以按照如下 ...
- Android使用C++截屏并显示
使用android底层自带的截屏源码进行修改后,将截取屏幕的内容再次显示在屏幕上,使屏幕呈现出暂停的效果. android自带的截屏代码在android\JB\frameworks\base\cmds ...
- FFmpeg 转码和截屏
转码 (flv转码为MP4,libx264是MP4编码格式 , -b 3000k是码率,比特率) ffmpeg -i /home/ghr/mp4/mp4.flv -vcodec libx264 -b ...
- 使用Python中PIL图形库进行截屏
目的:通过使用Python的一个图形库PIL(Python Image Library)对屏幕进行截图 步骤: 1.下载PIL(路径)并安装 2.新建文件“截屏.py”,右键Edit with IDL ...
- MonoGame 3.2 下,截屏与 Texture2D 的保存
10月20日注:后来发现了这篇博文(英文),XNA 中的 Color 实际上是与 Alpha 值自左乘(premultiplied)的,这也解释了直接用 0xARGB 转译而颜色异常的原因. 注意,由 ...
随机推荐
- div在IE6中固定
在IE6中固定一div在右下角,但是ie6不支持position:fixed属性,那么只能通过js实现,通过js判断浏览器在ie6的情况下,div的position为absoluate:right:0 ...
- SQL Server 调优系列基础篇 - 并行运算总结(二)
前言 上一篇文章我们介绍了查看查询计划的并行运行方式. 本篇我们接着分析SQL Server的并行运算. 闲言少叙,直接进入本篇的正题. 技术准备 同前几篇一样,基于SQL Server2008R2版 ...
- Vue 就地复用策略注意事项
---template部分 div el-popover(ref="message", placement="top-start", title="标 ...
- bzoj1089
题解: 递推 f[i]=f[i-1]^n+1 ans=f[d]-f[d-1] 代码: #include<bits/stdc++.h> using namespace std; int n, ...
- Activity与Service数据交互:Binder、bindService的用法
package com.lixu.jiaohu; import com.lixu.jiaohu.MyAppService.Mybind; import android.app.Activity; im ...
- 软工作业No.4
2048小游戏—设计开发 软件需求规格说明书 甜美女孩 2018年10月 ——————————————————————————— 文档修改记录 日期 版本 说明 作者 2018-10-18 V1. ...
- cmake 手册详解
cmake 手册中文版,后续再相关博客的其他链接中,感谢翻译的好人! https://www.cnblogs.com/coderfenghc/archive/2012/06/16/CMake_ch_0 ...
- python 元组(tuple)
面试python我想每个人都会被问一个问题,就是python中的list和tuple有什么区别? 一般情况下,我会回答,list元素可变,tuple元素不可变(书上或者其他的博客都是这么写的),一般情 ...
- Vue 2.0 学习路线
「 Vue很难学吗 」 对于我这种从0.x版本就开始体验 vuejs 的人来说,当然不算难,那时候没各种脚手架和复杂搭配,仅仅是一个mvvm的解决方案库而已,解决了jq带来的繁琐操作dom痛点,所以就 ...
- 【linux】wifi不能使用的问题
Worked solution (Requirements: kernel >=4.11) : Download driver directory from this repo:https:// ...