Capturing a window with WPF

后端 未结 2 2005
萌比男神i
萌比男神i 2021-02-10 19:02

With Windows Presentation Foundation, if I have an HWND, how can I capture it\'s window as an image that I can manipulate and display?

2条回答
  •  轮回少年
    2021-02-10 19:43

    While above answer is great, this would have saved a ton of time too:

    Using section:

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

    Code:

    /// 
    /// Captures screenshot using WINAPI.
    /// 
    static public class CaptureScreenshot
    {
        /// 
        /// Capture the screenshot.
        /// Area of screenshot.
        /// Bitmap source that can be used e.g. as background.
        /// 
        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
        {
            /// dest = source
            SRCCOPY = 0x00CC0020,
            /// dest = source OR dest
            SRCPAINT = 0x00EE0086,
            /// dest = source AND dest
            SRCAND = 0x008800C6,
            /// dest = source XOR dest
            SRCINVERT = 0x00660046,
            /// dest = source AND (NOT dest)
            SRCERASE = 0x00440328,
            /// dest = (NOT source)
            NOTSRCCOPY = 0x00330008,
            /// dest = (NOT src) AND (NOT dest)
            NOTSRCERASE = 0x001100A6,
            /// dest = (source AND pattern)
            MERGECOPY = 0x00C000CA,
            /// dest = (NOT source) OR dest
            MERGEPAINT = 0x00BB0226,
            /// dest = pattern
            PATCOPY = 0x00F00021,
            /// dest = DPSnoo
            PATPAINT = 0x00FB0A09,
            /// dest = pattern XOR dest
            PATINVERT = 0x005A0049,
            /// dest = (NOT dest)
            DSTINVERT = 0x00550009,
            /// dest = BLACK
            BLACKNESS = 0x00000042,
            /// dest = WHITE
            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
    }
    

    Notice that this takes screenshot of given area in the screen and not of window. It works for my purposes, you probably have to modify it for yours :)

提交回复
热议问题