惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
罗磊的独立博客
月光博客
月光博客
IT之家
IT之家
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
小众软件
小众软件
C
Check Point Blog
B
Blog RSS Feed
H
Help Net Security
博客园 - 司徒正美
L
LangChain Blog
MongoDB | Blog
MongoDB | Blog
B
Blog
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
N
Netflix TechBlog - Medium
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - LT

用vs code写代码,spyder看变量,只能这样了 Chrome禁用software_reporter_tool 【转载】Scarbee Pre-Bass 贝司的使用教程 罗兰管弦乐音色表【中英文对照】 ----转载 博文阅读密码验证 - 博客园 火狐无法启动,如何恢复数据、书签、密码 【转】C#写的170行俄罗斯方块 [转]C#获取窗口进程ID与句柄还有读写内存类 【转】WinSocket编程——原始套接字 【转】SHarpPcap(winpcap基于c#封转的库)网络嗅探程序核心 [心得] socket 多路复用误区 UDP 异步 收发 服务端例子 【转载】 UDPClient 的奇特现象,实在搞不懂 [转载]TCP打开端口数和打开连接数限制系列文章 UDP错误10054:远程主机强迫关闭的解决方法---转载 应用程式上屏蔽flash控件的右键菜单并获得左键消息_c# AJAX分段下载/读取HTML内容(有效节省带宽加快运行速度) 使用Fiddler2录制HTTP操作脚本 Subversion for Windows 安装配置
一个好用的C#抓图类
LT · 2012-02-09 · via 博客园 - LT

The Utilities class. Call Utilities.CaptureWindow(Control.Handle) to capture a specific control:

public static class Utilities
{
    public static Image CaptureScreen()
    {
        return CaptureWindow(User32.GetDesktopWindow());
    }

    public static Image CaptureWindow(IntPtr handle)
    {

        IntPtr hdcSrc = User32.GetWindowDC(handle);

        RECT windowRect = new RECT();
        User32.GetWindowRect(handle, ref windowRect);

        int width = windowRect.right - windowRect.left;
        int height = windowRect.bottom - windowRect.top;

        IntPtr hdcDest = Gdi32.CreateCompatibleDC(hdcSrc);
        IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height);

        IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap);
        Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, ApiConstants.SRCCOPY);
        Gdi32.SelectObject(hdcDest, hOld);
        Gdi32.DeleteDC(hdcDest);
        User32.ReleaseDC(handle, hdcSrc);

        Image image = Image.FromHbitmap(hBitmap);
        Gdi32.DeleteObject(hBitmap);

        return image;
    }
}

The Gdi32 class:

public class Gdi32
{
    [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);
}

The User32 class:

public static class User32
{
    [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 GetWindowRect(IntPtr hWnd, ref RECT rect);
    [DllImport("user32.dll")]
    public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
}

The constants used:

    public const int SRCCOPY = 13369376;

The structs used:

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int left;
    public int top;
    public int right;
    public int bottom;
}

A friendly Control extension method:

public static class ControlExtensions
{
    public static Image DrawToImage(this Control control)
    {
        return Utilities.CaptureWindow(control.Handle);
    }
}

This is a code snippet from my CC.Utilities project and I specifically wrote it to take screenshots from the WebBrowser control.