Capture a Screen Shot
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
namespace ScreenShotDemo
{
/// <summary>
/// Provides functions to capture the entire screen, or a particular window, and save it to a file.
/// </summary>
public class ScreenCapture
{
/// <summary>
/// Creates an Image object containing a screen shot of the entire desktop
/// </summary>
/// <returns></returns>
public Image CaptureScreen()
{
return CaptureWindow( User32.GetDesktopWindow() );
}
/// <summary>
/// Creates an Image object containing a screen shot of a specific window
/// </summary>
/// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
/// <returns></returns>
public Image CaptureWindow(IntPtr handle)
{
// get te hDC of the target window
IntPtr hdcSrc = User32.GetWindowDC(handle);
// get the size
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle,ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
// create a device context we can copy to
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
// create a bitmap we can copy it to,
// using GetDeviceCaps to get the width/height
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,width,height);
// select the bitmap object
IntPtr hOld = GDI32.SelectObject(hdcDest,hBitmap);
// bitblt over
GDI32.BitBlt(hdcDest,,,width,height,hdcSrc,,,GDI32.SRCCOPY);
// restore selection
GDI32.SelectObject(hdcDest,hOld);
// clean up
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle,hdcSrc);
// get a .NET image object for it
Image img = Image.FromHbitmap(hBitmap);
// free up the Bitmap object
GDI32.DeleteObject(hBitmap);
return img;
}
/// <summary>
/// Captures a screen shot of a specific window, and saves it to a file
/// </summary>
/// <param name="handle"></param>
/// <param name="filename"></param>
/// <param name="format"></param>
public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format)
{
Image img = CaptureWindow(handle);
img.Save(filename,format);
}
/// <summary>
/// Captures a screen shot of the entire desktop, and saves it to a file
/// </summary>
/// <param name="filename"></param>
/// <param name="format"></param>
public void CaptureScreenToFile(string filename, ImageFormat format)
{
Image img = CaptureScreen();
img.Save(filename,format);
} /// <summary>
/// Helper class containing Gdi32 API functions
/// </summary>
private class GDI32
{ public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject,int nXDest,int nYDest,
int nWidth,int nHeight,IntPtr hObjectSource,
int nXSrc,int nYSrc,int dwRop);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC,int nWidth,
int nHeight);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hDC,IntPtr hObject);
} /// <summary>
/// Helper class containing User32 API functions
/// </summary>
private class User32
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDC);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd,ref RECT rect);
}
}
}
Capture a Screen Shot的更多相关文章
- Taking a screen shot of a window using Delphi code is rather easy.
Taking a screen shot of a window using Delphi code is rather easy. A screen shot (screen capture) is ...
- ROS常用三維機器人仿真工具Gazebo教程匯總
參考網址: 1. http://gazebosim.org/tutorials 2. http://gazebosim.org/tutorials/browse Gazebo Tutorials Ga ...
- macOS 不用任何第三方工具 简单两步使用 Automator 将截图转成@1x
制作 Automator 脚本 打开 Automator -> 选择服务,左侧搜索 shell,双击打开,右侧粘贴以下内容,将上部 服务收到... 改成 没有输入,CMD+S保存,名称就叫 屏幕 ...
- Mac Screen Capture Shortcuts
Here's How: To capture the entire desktop, press Command-Shift-3. The screen shot will be automati ...
- FastStone Capture的使用
FastStone Capture的使用 FastStone Capture是一款精简而优秀的图像处理软件,在工作中会经常用到.我在本地安装了FastStone Capture 8.4版本 (提取码: ...
- Various methods for capturing the screen
Explains techniques for capturing the screen programmatically. Download GDI source code - 72.1 Kb Do ...
- Expression Tree Basics 表达式树原理
variable point to code variable expression tree data structure lamda expression anonymous function 原 ...
- [GE]手动截取当前活动窗口,并且按规则命名(1/2)
Function Take-ScreenShot { <# .SYNOPSIS Used to take a screenshot of the desktop or the active wi ...
- [APAC]手动截取当前活动窗口,并且按规则命名(1/2)
Function Take-ScreenShot { <# .SYNOPSIS Used to take a screenshot of the desktop or the active wi ...
随机推荐
- String课后作业
请查看String.equals()方法的实现代码,注意学习其实现方法. public class StringEquals { @param args the command line argume ...
- GitHub Pages 搭建流程-基于jekyll-bootstrap
我写这篇文章的目的是记录本博客的搭建过程,自己从零开始逐步搭建起来了GitHub Pages,其中借鉴了很多的博客和模版,稍后会在后面列出,也为没有用过gihub和jekyll的童鞋提供一点帮助. 学 ...
- web前端基础篇⑤
1.雪碧图技术(精灵图)sprite cpmpass-合并2.兼容性:1.reset充值技术,normalize技术2.加前缀-webkit —mom -ms— -o-3.<!Doctype&g ...
- ios网络学习------6 json格式数据的请求处理
ios网络学习------6 json格式数据的请求处理 分类: IOS2014-06-30 20:33 471人阅读 评论(3) 收藏 举报 #import "MainViewContro ...
- github常见操作和常见错误!错误提示:fatal: remote origin already exists.
如果输入$ git remote add origin git@github.com:djqiang(github帐号名)/gitdemo(项目名).git 提示出错信息:fatal: remote ...
- iOS 根据UIImage 修改UIImageView Frame (包括截取图片中间部分)
iOS UIImageView 根据需求调整frame 1.图片的宽和高不相等,截取图片的中间部分,截取的部分Size明确 2.图片的宽度要等于其父视图的类的宽度,然后根据宽度计算高度,保证 图片不变 ...
- MYSQL数据表操作语句
1.查看某数据库中的表 SHOW [FULL] TABLES [FROM db_name] [LIKE 'pattern'] SHOW TABLES列举了给定数据库中的非TEMPORARY表.也可以使 ...
- supervisor安装配置与使用
supervisor:C/S架构的进程控制系统,可使用户在类UNIX系统中监控.管理进程.常用于管理与某个用户或项目相关的进程. 组成部分supervisord:服务守护进程supervisorctl ...
- git-quick-start 动画讲解Git命令行
来源:http://git.oschina.net/wzw/git-quick-start#git-quick-start git-quick-start 这是一个git的快速入门项目,使用一些gif ...
- java中的final关键词
参考资料: http://www.cnblogs.com/dolphin0520/p/3736238.html final是个修饰词,可以修饰类.方法.变量. 1. 修饰类 修饰类,就表示这个类不能被 ...