使用WPF捕获窗口

时间:2009-11-15 01:40:02

标签: c# wpf screenshot

使用Windows Presentation Foundation,如果我有HWND,如何将其窗口捕获为可以操作和显示的图像?

2 个答案:

答案 0 :(得分:9)

你可以:

  1. CreateBitmap()创建一个hBitmap
  2. 在hWnd上调用GetDC()
  3. BitBlt()内容到hBitmap
  4. ReleaseDC()
  5. 致电Imaging.CreateBitmapSourceFromHBitmap()以创建托管BitmapSource
  6. hBitmap上的
  7. DeleteObject()
  8. 根据需要使用BitmapSource
  9. 步骤1-4和6使用Win32 API(准确地说是GDI),步骤5和7使用WPF完成

答案 1 :(得分:7)

虽然上述答案很好,但这也可以节省大量时间:

使用部分:

using System;
using System.Windows;
using System.Windows.Interop;
using System.Runtime.InteropServices;
using System.Windows.Media.Imaging;

<强>代码:

/// <summary>
/// Captures screenshot using WINAPI.
/// </summary>
static public class CaptureScreenshot
{
    /// <summary>
    /// Capture the screenshot.
    /// <param name="area">Area of screenshot.</param>
    /// <returns>Bitmap source that can be used e.g. as background.</returns>
    /// </summary>
    public static BitmapSource Capture(Rect area)
    {
        IntPtr screenDC = GetDC(IntPtr.Zero);
        IntPtr memDC = CreateCompatibleDC(screenDC);
        IntPtr hBitmap = CreateCompatibleBitmap(screenDC, (int)SystemParameters.VirtualScreenWidth, (int)SystemParameters.VirtualScreenHeight);
        SelectObject(memDC, hBitmap); // Select bitmap from compatible bitmap to memDC

        // TODO: BitBlt may fail horribly
        BitBlt(memDC, 0, 0, (int)area.Width, (int)area.Height, screenDC, (int)area.X, (int)area.Y, TernaryRasterOperations.SRCCOPY);
        BitmapSource bsource = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

        DeleteObject(hBitmap);
        ReleaseDC(IntPtr.Zero, screenDC);
        ReleaseDC(IntPtr.Zero, memDC);
        return bsource;
    }

    #region WINAPI DLL Imports

    [DllImport("gdi32.dll", ExactSpelling = true, PreserveSig = true, SetLastError = true)]
    static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

    [DllImport("gdi32.dll")]
    private static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);

    [DllImport("gdi32.dll", SetLastError = true)]
    private static extern IntPtr CreateCompatibleDC(IntPtr hdc);

    [DllImport("gdi32.dll")]
    private static extern bool DeleteObject(IntPtr hObject);

    [DllImport("gdi32.dll")]
    private static extern IntPtr CreateBitmap(int nWidth, int nHeight, uint cPlanes, uint cBitsPerPel, IntPtr lpvBits);

    [DllImport("user32.dll")]
    private static extern IntPtr GetDC(IntPtr hWnd);

    [DllImport("user32.dll")]
    private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

    private enum TernaryRasterOperations : uint
    {
        /// <summary>dest = source</summary>
        SRCCOPY = 0x00CC0020,
        /// <summary>dest = source OR dest</summary>
        SRCPAINT = 0x00EE0086,
        /// <summary>dest = source AND dest</summary>
        SRCAND = 0x008800C6,
        /// <summary>dest = source XOR dest</summary>
        SRCINVERT = 0x00660046,
        /// <summary>dest = source AND (NOT dest)</summary>
        SRCERASE = 0x00440328,
        /// <summary>dest = (NOT source)</summary>
        NOTSRCCOPY = 0x00330008,
        /// <summary>dest = (NOT src) AND (NOT dest)</summary>
        NOTSRCERASE = 0x001100A6,
        /// <summary>dest = (source AND pattern)</summary>
        MERGECOPY = 0x00C000CA,
        /// <summary>dest = (NOT source) OR dest</summary>
        MERGEPAINT = 0x00BB0226,
        /// <summary>dest = pattern</summary>
        PATCOPY = 0x00F00021,
        /// <summary>dest = DPSnoo</summary>
        PATPAINT = 0x00FB0A09,
        /// <summary>dest = pattern XOR dest</summary>
        PATINVERT = 0x005A0049,
        /// <summary>dest = (NOT dest)</summary>
        DSTINVERT = 0x00550009,
        /// <summary>dest = BLACK</summary>
        BLACKNESS = 0x00000042,
        /// <summary>dest = WHITE</summary>
        WHITENESS = 0x00FF0062
    }

    [DllImport("gdi32.dll")]
    private static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);

    #endregion
}

请注意,这会截取屏幕中给定区域的屏幕截图,而不是窗口。它适用于我的目的,你可能需要为你修改它:))