如何采取一个全尺寸的窗口在C#中的截图 [英] How to take a screenshot of a Full Size window in C#

查看:105
本文介绍了如何采取一个全尺寸的窗口在C#中的截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建在C#.NET,抓住当前活动窗口的截图包括必须使用滚动bars.I发现下面的代码采取截图滚动区域的应用程序。很清楚我想要的代码,以活动窗口的截图,包括没有显示,并且仅通过使用滚动条显示的区域。

 公共类截图
{
///<总结>
///捕获整个桌面
///<的截图; /总结>
///<回报获得包含桌面<的截屏图像对象; /回报>
私人图像CaptureDesktop()
{
返回CaptureWindow(User32.GetDesktopWindow());
}

公众形象CaptureAciveWindow()
{
返回CaptureWindow(User32.GetForegroundWindow());
}

///<总结>
///一个内部方法,捕捉任何给定的应用程序窗口的截图,赋予其句柄。
///< /总结>
///< PARAM NAME =处理>您想捕捉<窗口的句柄; /参数>
///<回报获得包含活动应用程序窗口<的截屏的图像对象; /回报>
私人图像CaptureWindow(IntPtr的手柄)
{
//获取目标窗口
IntPtr的hdcSrc = User32.GetWindowDC(句柄)的TE的hDC;
//获取大小
User32.RECT​​ windowRect =新User32.RECT​​();
User32.GetWindowRect(手柄,楼盘windowRect);
INT宽度= windowRect.right - windowRect.left;
INT高度= windowRect.bottom - windowRect.top;
//创建我们可以复制到
IntPtr的hdcDest = GDI32.CreateCompatibleDC(hdcSrc)设备上下文;
//创建位图,我们可以将其复制到,
//使用GetDeviceCaps来获得宽度/高度
IntPtr的HBITMAP = GDI32.CreateCompatibleBitmap(hdcSrc,宽,高);
//选择位图对象
IntPtr的HOLD = GDI32.SelectObject(hdcDest,HBITMAP);
//的bitblt超过
GDI32.BitBlt(hdcDest,0,0,宽度,高度,hdcSrc,0,0,GDI32.SRCCOPY);
//恢复选择
GDI32.SelectObject(hdcDest,持有);
//清理
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(手柄,hdcSrc);
//获得一个.NET图像对象上,
图片IMG = Image.FromHbitmap(HBITMAP);
//释放位图对象
GDI32.DeleteObject(HBITMAP);

返回IMG;
}

///<总结>
///助手包含GDI32 API函数
///<类; /总结>
类的私有GDI32
{
公共const int的SRCCOPY = 0x00CC0020; // BitBlt的dwRop参数

函数[DllImport(GDI32.DLL)]
公共静态外部布尔的BitBlt(IntPtr的hObject,诠释nXDest,诠释nYDest,
INT nWidth,INT nHeight参数,IntPtr的hObjectSource,
INT nXSrc,诠释nYSrc,诠释dwRop);
函数[DllImport(GDI32.DLL)]
公共静态外部的IntPtr CreateCompatibleBitmap(IntPtr的的hDC,诠释nWidth,
INT nHeight参数);
函数[DllImport(GDI32.DLL)]
公共静态外部的IntPtr CreateCompatibleDC(IntPtr的HDC);
函数[DllImport(GDI32.DLL)]
公共静态的extern BOOL DeleteDC(IntPtr的HDC);
函数[DllImport(GDI32.DLL)]
公共静态的extern BOOL DeleteObject的(IntPtr的hObject);
函数[DllImport(GDI32.DLL)]
公共静态外部的IntPtr选择对象(IntPtr的的hDC,IntPtr的hObject);
}

///<总结>
///助手含USER32 API函数
///<类; /总结>
类的私有USER32
{
[StructLayout(LayoutKind.Sequential)]
公共结构RECT
{
公众诠释左右;
公众诠释榜首;
公众诠释权;
公众诠释底部;
}

函数[DllImport(user32.dll中)]
公共静态外部的IntPtr GetDesktopWindow();
函数[DllImport(user32.dll中)]
公共静态外部的IntPtr GetWindowDC(IntPtr的的hWnd);
函数[DllImport(user32.dll中)]
公共静态外部的IntPtr ReleaseDC(IntPtr的的HWND,IntPtr的HDC);
函数[DllImport(user32.dll中)]
公共静态外部的IntPtr GetWindowRect(IntPtr的的HWND,裁判RECT RECT);
///<总结>
///获取当前活动窗口
的Handle ///< /总结>
///<退货和GT;活动窗户把手与LT; /回报>
函数[DllImport(user32.dll中)]
公共静态外部的IntPtr GetForegroundWindow();
}
}


解决方案

您'重新在这里做一个假设,也不一定是真的:有有什么东西在unscrolled地区早已等候。应用程序可以建立滚动条一定高度或宽度,而不是实际渲染任何表面,直到将用户的实际拖动滚动条。你想捕捉的图像只存在于潜力。这可以用来提高性能&MDASH;想到刚刚在时间加载,或减少内存使用。这是一个相当常见的技术,还等什么你要求并没有真正意义。


I am trying to create an application in C#.net that captures the screenshot of the current active window including the area that has to be scrolled using scroll bars.I found the following code to take screenshot. To be very clear I want the code to take screenshot of the active window including the area that is not displayed and is only revealed by using scroll bars.

public class ScreenShot
{
    /// <summary>
    /// Captures the screenshot of the entire desktop
    /// </summary>
    /// <returns>Image object containing the screenshot of the desktop</returns>
    private Image CaptureDesktop()
    {
        return CaptureWindow(User32.GetDesktopWindow());
    }

    public Image CaptureAciveWindow()
    {
        return CaptureWindow(User32.GetForegroundWindow());
    }

    /// <summary>
    /// An Internal method, that captures the screenshot of any given Application window, given its Handle.
    /// </summary>
    /// <param name="handle">The handle of the window you want to Capture</param>
    /// <returns>An Image object containing the screenshot of the active application window</returns>
    private 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, 0, 0, width, height, hdcSrc, 0, 0, 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>
    /// 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);
        /// <summary>
        /// Gets the Handle for current active window
        /// </summary>
        /// <returns>Active windows Handle</returns>
        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();
    }
}

解决方案

You're making an assumption here that's not necessarily true: that there's something there already waiting in the unscrolled area. An application can build scroll bars for a certain height or width and not actually render anything to the surface until the user actual drags the scroll bar. The image you want to capture exists only in potential. This can be used to improve performance — think just-in-time loading, or reducing memory use. It's a fairly common technique, and so what you're asking for doesn't really make sense.

这篇关于如何采取一个全尺寸的窗口在C#中的截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆