使用C#,WPF,和DWM保存窗口的屏幕截图 [英] Saving a screenshot of a window using C#, WPF, and DWM

查看:3125
本文介绍了使用C#,WPF,和DWM保存窗口的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个跟进问题这个问题

以上的解决方案使用DWM显示活动窗口的缩略图。如果我理解正确的话,它的工作原理是让你指定要查看应用程序的窗口句柄,然后有你提供了一个窗口句柄,并在那里有窗户应拉上目标窗口的内容,该窗口上的位置。

The solution to the above uses DWM to display a thumbnail of an active window. If I understand correctly, it works by letting you specify the window handle of the application you want to view and then having you provide a window handle and a location on that window where windows should draw the contents of the target Window.

有没有办法来渲染窗口屏幕快照直接的BitmapImage或图像,而不是直接借鉴的地方在你的窗口?

Is there a way to render the window screen shot directly to BitmapImage or Image instead of directly drawing it somewhere in your window? (Basically to just grab a screen shot of the window - even if it's covered by another window - with out using an updating thumbnail.)

感谢您的帮助(使用更新缩略图与出基本上只抢窗口的屏幕快照 - - 哪怕它是由另一个窗口覆盖。)!

Thanks for you help!

推荐答案

该Control.DrawToBitmap并不总是工作,所以我使出提供更一致的结果下面的原生API调用:

The Control.DrawToBitmap doesn't always work so I resorted to the following native API calls that provide more consistent results:

该实用程序类。呼叫Utilities.CaptureWindow(Control.Handle)来捕获特定的控制:

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;
    }
}



GDI32类:

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);
}



USER32类:

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);
    }
}

这篇关于使用C#,WPF,和DWM保存窗口的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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