Beispiel #1
0
        public static Bitmap PrtWindow(IntPtr hWnd)
        {
            IntPtr    hscrdc = User32Api.GetWindowDC(hWnd);
            Rectangle rect   = Rectangle.Empty;

            User32Api.GetWindowRect(hWnd, ref rect);
            IntPtr hbitmap = Gdi32Help.CreateCompatibleBitmap(hscrdc, rect.Width - rect.Left, rect.Height - rect.Top);
            IntPtr hmemdc  = Gdi32Help.CreateCompatibleDC(hscrdc);

            Gdi32Help.SelectObject(hmemdc, hbitmap);
            User32Api.PrintWindow(hWnd, hmemdc, 0);
            Bitmap bmp = Bitmap.FromHbitmap(hbitmap);

            Gdi32Help.DeleteDC(hscrdc);
            Gdi32Help.DeleteDC(hmemdc);
            return(bmp);
        }
Beispiel #2
0
        /// <summary>
        /// 截屏1
        /// </summary>
        /// <param name="windowModel"></param>
        /// <returns></returns>
        public static Bitmap PrintWindow(WindowModel windowModel)
        {
            IntPtr hscrdc  = User32Api.GetWindowDC(windowModel.IntPtr); //返回hWnd参数所指定的窗口的设备环境。
            int    width   = windowModel.WindowRectangle.Width;
            int    height  = windowModel.WindowRectangle.Height;
            IntPtr hbitmap = Gdi32Help.CreateCompatibleBitmap(hscrdc, width, height); //该函数创建与指定的设备环境相关的设备兼容的位图
            IntPtr hmemdc  = Gdi32Help.CreateCompatibleDC(hscrdc);                    //该函数创建一个与指定设备兼容的内存设备上下文环境(DC)

            Gdi32Help.SelectObject(hmemdc, hbitmap);                                  //该函数选择一对象到指定的设备上下文环境中,该新对象替换先前的相同类型的对象
            User32Api.PrintWindow(windowModel.IntPtr, hmemdc, 0);
            Bitmap bmp = Bitmap.FromHbitmap(hbitmap);

            //Clipboard.SetImage(bmp);
            Gdi32Help.DeleteDC(hscrdc); //删除用过的对象  
            Gdi32Help.DeleteDC(hmemdc); //删除用过的对象  
            return(bmp);
        }
Beispiel #3
0
        /// <summary>
        /// Return an Image representing the Window!
        /// As GDI+ draws it, it will be without Aero borders!
        /// </summary>
        public static TBitmap PrintWindow <TBitmap>(this IInteropWindow interopWindow) where TBitmap : class
        {
            var windowRect = interopWindow.GetInfo().Bounds;
            // Start the capture
            Exception exceptionOccured = null;
            Bitmap    printWindowBitmap;

            using (var region = interopWindow.GetRegion())
            {
                var pixelFormat = PixelFormat.Format24bppRgb;
                // Only use 32 bpp ARGB when the window has a region
                if (region != null)
                {
                    pixelFormat = PixelFormat.Format32bppArgb;
                }
                printWindowBitmap = new Bitmap(windowRect.Width, windowRect.Height, pixelFormat);
                using (var graphics = Graphics.FromImage(printWindowBitmap))
                {
                    using (var graphicsDc = graphics.GetSafeDeviceContext())
                    {
                        bool printSucceeded = User32Api.PrintWindow(interopWindow.Handle, graphicsDc.DangerousGetHandle(), PrintWindowFlags.PW_COMPLETE);
                        if (!printSucceeded)
                        {
                            // something went wrong, most likely a "0x80004005" (Acess Denied) when using UAC
                            exceptionOccured = new Win32Exception();
                        }
                    }

                    // Apply the region "transparency"
                    if (region != null && !region.IsEmpty(graphics))
                    {
                        graphics.ExcludeClip(region);
                        graphics.Clear(Color.Transparent);
                    }

                    graphics.Flush();
                }
            }

            // Return null if error
            if (exceptionOccured != null)
            {
                Log.Error().WriteLine("Error calling print window: {0}", exceptionOccured.Message);
                printWindowBitmap.Dispose();
                return(default);