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() 返回在指定的位置 ...
随机推荐
- Javascript闭包简单理解
提到闭包,想必大家都早有耳闻,下面说下我的简单理解.平时写代码.第三方框架和组件都或多或少用到了闭包.所以,了解闭包是非常必要的.呵呵... 一.什么是闭包简而言之,就是能够读取其他函数内部变量的函数 ...
- Linux 寻找安装路径
1.whereis 语法: # whereis [-bmsu] 文件或者目录名称 参数说 明: -b : 只找二进制文件 -m: 只找在说明文件manual路径下的文件 -s : 只找sou ...
- 基于ThinkPHP+AJAX的省市区三级联动
练习,就当练习. 省市区三级联动,样式如下图所示: 1,导入两个js文件并且导入数据库文件. 两个js文件分别是jquery-2.1.4.min.js和jquery-1.js,数据库文件,见附件. 2 ...
- linux下 oracle常用命令
打开图形化窗口: 1)Database Configuration Assistant windows (添加数据库实例) $ dbca 2)Oracle Net Configuration A ...
- 3.5 linux 0.11 目标文件格式
在Linux0.11系统中,GNU gcc或gas编译输出的目标模块文件和链接程序生成的可执行文件都使用了UNIX传统的a.out格式.这是一种被称为汇编与链接输出(Assembly & li ...
- Python自动化运维之20、HTML
一.HTML相关概念 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这 ...
- [C]记录C语言中由于粗心遇到的奇葩错误.
1. 正确代码: for( word = strtok( buf, whitespace); word != NULL; word = strtok( NULL, whitespace)) { ) 错 ...
- JVM笔记-逃逸分析
参考: http://www.iteye.com/topic/473355http://blog.sina.com.cn/s/blog_4b6047bc01000avq.html 什么是逃逸分析(Es ...
- 函数部分应用Partial application
def adder(m:Int,n:Int)=m+n val add2 = adder(2,_:Int) println(add2(3)) val add3 = adder(_:Int,3) prin ...
- poj 2492A Bug's Life
http://poj.org/problem?id=2492 #include<cstdio> #include<cstring> #include<algorithm& ...