C#截图操作方法大全
using System.Drawing;
using System.Windows.Forms; namespace WindowsFormsApplication1
{
class ScreenCapture
{
private Bitmap _screenshot = null; /// <summary>
/// 截取全屏
/// </summary>
/// <returns>返回值</returns>
public Bitmap CaptureScreen()
{
return Capture(Rectangle.Empty, false);
} /// <summary>
/// 截取全屏并保存
/// </summary>
/// <param name="fileName">文件路径</param>
/// <returns></returns>
public Bitmap CaptureScreen(string fileName)
{
Bitmap screenshot = Capture(Rectangle.Empty, false);
saveImage(fileName, screenshot);
return screenshot;
} /// <summary>
/// 截屏到剪切板
/// </summary>
public void CopyToClipboard()
{
if (this._screenshot != null)
Clipboard.SetImage(this._screenshot);
else if (this._screenshot == null)
MessageBox.Show("No screenshot found. Please take a screenshot first.", "Copy to Clipboard");
} /// <summary>
/// 区域截屏
/// </summary>
/// <param name="rect">区域范围</param>
/// <returns>返回值</returns>
public Bitmap CaptureRectangle(Rectangle rect)
{
return Capture(rect, true);
} /// <summary>
/// 区域截屏
/// </summary>
/// <param name="rect">区域范围</param>
/// <param name="fileName">文件路径</param>
/// <returns>返回值</returns>
public Bitmap CaptureRectangle(Rectangle rect, string fileName)
{
Bitmap screenshot = Capture(rect, true);
saveImage(fileName, screenshot);
return screenshot;
} private Bitmap Capture(Rectangle rect, bool isRect)
{
int screenWidth = Screen.PrimaryScreen.Bounds.Width;
int screenHeight = Screen.PrimaryScreen.Bounds.Height; Bitmap screenshot = null; if(!isRect)
screenshot = new Bitmap(screenWidth, screenHeight);
else if(isRect)
screenshot = new Bitmap(rect.Width, rect.Height); Graphics g = Graphics.FromImage(screenshot);
if (!isRect)
{
g.CopyFromScreen(Point.Empty, Point.Empty, screenshot.Size);
}
else if (isRect)
{
g.CopyFromScreen(new Point(rect.X, rect.Y), Point.Empty, rect.Size);
} this._screenshot = screenshot; return screenshot;
} private void saveImage(string fileName, Bitmap screenshot)
{
string ext = System.IO.Path.GetExtension(fileName);;
ext = ext.ToLower(); if (ext == ".jpg" || ext == ".jpeg")
screenshot.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
else if(ext == ".gif")
screenshot.Save(fileName, System.Drawing.Imaging.ImageFormat.Gif);
else if(ext == ".png")
screenshot.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
else if (ext == ".bmp")
screenshot.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);
else if (ext == ".tiff")
screenshot.Save(fileName, System.Drawing.Imaging.ImageFormat.Tiff);
}
}
}
C#截图操作方法大全的更多相关文章
- python文件和目录操作方法大全(含实例)
一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法.1.得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd()2.返回指定目录下的所有文件和目 ...
- 【python】python文件和目录操作方法大全(含实例)
转自:http://www.jb51.net/article/48001.htm 一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法.1.得到当前工作目录,即当前Pyt ...
- Python 字符串操作方法大全
Python 字符串操作方法大全 1.去空格及特殊符号复制代码代码如下:s.strip().lstrip().rstrip(',') 2.复制字符串复制代码代码如下:#strcpy(sStr1,sSt ...
- 【转】python文件和目录操作方法大全(含实例)
python文件和目录操作方法大全(含实例) 这篇文章主要介绍了python文件和目录的操作方法,简明总结了文件和目录操作中常用的模块.方法,并列举了一个综合实例,需要的朋友可以参考下一.python ...
- python文件和目录操作方法大全
一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法. 1.得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd()2.返回指定目录下的所有文件和 ...
- python文件和目录操作方法大全(含实例)【python】
转自:http://www.jb51.net/article/48001.htm 一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法. 1.得到当前工作目录,即当前Py ...
- OS -- (python)文件和目录操作方法大全
一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法.1.得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd()2.返回指定目录下的所有文件和目 ...
- 文件操作方法大全以及文件打开的其他一些模式sys.stdout.write()就是标准输出到你当前的屏幕 sys.stdout.flush()把内存立即显示到您当前的屏幕
read()会让你读取的光标变成最后.tell()把你现在文件的句柄的指针打印出来.文件的开头指针位置是0f.read(5)只读取5个字符串个数如果你想把光标移回去,移动到首位f.seek(0)f.d ...
- JavaScript字符串操作方法大全,包含ES6方法
一.charAt() 返回在指定位置的字符. var str="abc" console.log(str.charAt(0))//a 二.charCodeAt() 返回在指定的位置 ...
随机推荐
- Swift - 29 - 参数的默认值
// 参数设置了默认值之后, 在调用的时候, 可以写这个参数 // 在参数前面添加"_", 表示取消外部参数名, 但还是建议使用苹果默认格式 func sayHello(nickN ...
- [转]memmove函数
[FROM MSDN && 百科] 原型: void *memmove( void* dest, const void* src, size_tcount ); #include&l ...
- ejabberd,erlang,简单看了一下,总结一下,很肤浅
本来也没打算深入学习erlang,就是看一下他们的大概思路erlang每个自定义函数都能注册成进程,每个节点通过erl -name 'name@ip'.进去后,可以直接做远程调用,节点之间就靠一个连接 ...
- N3292x IBR介绍
N3292x IBR介绍 1 IBR启动流程 图1-1 IBR启动流程 CHIP_CFG[0] Mode 0 Boot from IBR Recovery Mode with crystal inpu ...
- extjs combobox 设置下拉时显示滚动条 设置显示条数
extjs在点击下拉时如果没有限制它的高度,那么它的默认最大高度是300,显示的时候就会显示300的高度,知道选项内容超过这个高度时才会自动显示滚动条,往往在有些时候我们希望让combobox显示一个 ...
- Erget 显示对象
核心显示类: 类 描述 DisplayObject 显示对象基类,所有显示对象均继承自此类 Bitmap 位图,用来显示图片 Shape 用来显示矢量图,可以使用其中的方法绘制矢量图形 TextFie ...
- Mifare S50与Mifare S70
转自http://blog.sina.com.cn/s/blog_9ed067ad0100zyjx.html Mifare S50和Mifare S70又常被称为Mifare Standard.Mif ...
- 谈谈依赖注入DI
控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心. 控制反转一般分为两种类型,依赖注入 ...
- zend framework 1 连接oracle数据库的写法
1 用Zend_Db_Adapter_Pdo_Oci方式 $config =array( 'host'=>'192.168.5.40', 'port'=>'1521', 'dbname'= ...
- poj 2886Who Gets the Most Candies?
http://poj.org/problem?id=2886 #include <cstdio> #include <cstring> #include <algorit ...