注意修改命名空间

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# 截图类的更多相关文章

  1. 使用PhantomJS实现网页截图服务

    这是上半年遇到的一个小需求,想实现网页的抓取,并保存为图片.研究了不少工具,效果都不理想,不是显示太差了(Canvas.Html2Image.Cobra),就是性能不怎么样(如SWT的Brower). ...

  2. testng对失败时截图处理

    1.截图类: public class ScreenShot { public WebDriver driver; public ScreenShot(WebDriver driver) { this ...

  3. winform实现矩形框截图

    使用方法如下: private void button1_Click(object sender, EventArgs e) { s.GerScreenFormRectangle(); } priva ...

  4. testng 异常 截图

    testNG里有一个异常监听类,失败时会执行类里的相关方法 DriverBase 截图类 TestngListenerScreen 异常监听类 Test1 测试类1.DriverBase类 packa ...

  5. 封装selenium自动化框架中的截图功能

    对selenium自带的截图功能进行封装: 以下为封装的代码,自定义一个.py文件即可,图片路径自己设置一个. #coding:utf-8 class Screen(object): ''' 封装的截 ...

  6. Webdriver+Testng实现测试用例失败自动截图功能

    testng执行测试用例的时候,如果用例执行失败会自动截图,方便后续排查问题 1.首先定义一个截图类: package com.rrx.utils; import java.io.File;impor ...

  7. Python+Selenium框架设计之框架内封装基类和实现POM

    原文地址https://blog.csdn.net/u011541946/article/details/70269965 作者:Anthony_tester 来源:CSDN    博客地址https ...

  8. C# 图像处理:实现鼠标选择矩形截图

    使用方法如下: private void button1_Click(object sender, EventArgs e) { s.GerScreenFormRectangle(); } priva ...

  9. Selenium+java - 截图操作

    写在前面 自动化测试过程中,运行失败截图可以很好的帮我们定位问题,因此,截图操作也是我们自动化测试中的一个重要环节. 截图方法 1.通过截图类TakeScreenshout实现截图 特点:截取浏览器窗 ...

随机推荐

  1. JSF 2 textbox example

    In JSF, you can use the <h:inputText /> tag to render a HTML input of type="text", t ...

  2. thymeleaf条件表达式

    条件表达式形式:condition, then and else <tr th:class="${row.even}? 'even' : 'odd'"> ... < ...

  3. windbg命令分类与概述

    WinDBG的大多数功能是以命令方式工作的, 本系列将介绍WinDBG的三类命令, 标准命令, 元命令和扩展命令. =============== 标准命令 =============== 标准命令用 ...

  4. MFC 视图、文档、框架(通讯)

    CMainFrame * pMainWnd=(CMainFrame*)AfxGetApp()->m_pMainWnd;//主框架 CChildFrame * pChild = (CChildFr ...

  5. .net版本之间的关系

    net framework 2.0,3.0与3.5三个版本之间关系如下: .net framework 2.0 = CLR 2.0 + FCL(framework class library) .ne ...

  6. 深入浅出之Smarty模板引擎工作机制(一)

    深入浅出Smarty模板引擎工作机制,我们将对比使用smarty模板引擎和没使用smarty模板引擎的两种开发方式的区别,并动手开发一个自己的模板引擎,以便加深对smarty模板引擎工作机制的理解. ...

  7. [Android][Android Studio] *.jar 与 *.aar 的生成与*.aar导入项目方法

    主要讲解Android Studio中生成aar文件以及本地方式使用aar文件的方法. 在Android Studio中对一个自己库进行生成操作时将会同时生成*.jar与*.aar文件. 分别存储位置 ...

  8. 2015南阳CCPC E - Ba Gua Zhen 高斯消元 xor最大

    Ba Gua Zhen Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description During the Three-Kingdom perio ...

  9. C#实现汉诺塔问题

    汉诺塔的由来:汉诺塔是源自印度神话里的玩具.上帝创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上安大小顺序摞着64片黄金圆盘.上帝命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上.并且 ...

  10. 如何防止ListView控件闪烁

    如何防止ListView控件闪烁 beginupdate()和endupdate()之间写代码   ListView1.Items.BeginUpdate;ListView1.Items.Add('A ...