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 转译而颜色异常的原因. 注意,由 ...
随机推荐
- httpclient cookie使用介绍
COOKIE的处理 session的保持是通过cookie来维持的,所以如果用户有勾选X天免登陆,这个session就X天内一直有效,就是通过这个cookie来维持. 如果没有选中x天免登陆,基本上就 ...
- simulate mdns message
use dns-sd command simulate mdns message: dns-sd -R test _test._tcp local 1234 a=1111111111111111111 ...
- json 与字符串相互转换,
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...
- docker官方windows安装
https://docs.docker.com/engine/installation/windows/
- Oracle/MySQL decimal/int/number 转字符串
有时客户需要流水数据,当导出为excel的时候,客户编号等很长数字的栏位,被excel变成科学记数法,无法正常查看. 因此,需要将Oracle/MySQL中的decimal/int 转 varchar ...
- android开源项目集合
ZXing http://code.google.com/p/zxing/ 条形码.二维码 K-9 Mail http://code.google.com/p/k9mail/ 邮件客户端 Sipdro ...
- 玩转X-CTR100 l STM32F4 l 红外遥控接收
我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] X-CTR100控制器具有红外接收头,例程 ...
- 用MyEclipse JPA创建项目(四)
MyEclipse 3.15 Style——在线购买低至75折!火爆开抢>> [MyEclipse最新版下载] 本教程介绍了MyEclipse中的一些基于PA的功能. 阅读本教程时,了解J ...
- JAVA第5次作业
将指定目录下的所有文件显示到列表框(JList)组件中,如下图所示: import java.awt.BorderLayout; import java.awt.Color; import java. ...
- SharePoint SSS(Security Store Service)服务-PowerShell
1. 获取SSS应用程序的标识 Get-SPServiceApplication 2. 获取指定的SSS应用程序实例 $sss = Get-SPServiceApplication -Identity ...