GrabWindowBitmap() публичный статический Метод

Captures a screenshot of the specified window at the specified bitmap size. NOTE: This method will not accurately capture controls that are hidden or obstructed (partially or completely) by another control (e.g. hidden tabs, or MDI child windows that are obstructed by other child windows/forms).
public static GrabWindowBitmap ( IntPtr windowHandle, System bitmapSize ) : Bitmap
windowHandle System.IntPtr The window handle.
bitmapSize System The requested bitmap size.
Результат System.Drawing.Bitmap
Пример #1
0
        /// <summary>
        /// Helper function to capture a bitmap for a given window handle or incase of WPF app,
        /// an UIElement.
        /// </summary>
        /// <param name="taskbarWindow">The proxy window for which a bitmap needs to be created</param>
        /// <param name="requestedSize">Size for the requested bitmap image</param>
        /// <returns>Bitmap captured from the window handle or UIElement. Null if the window is hidden or it's size is zero.</returns>
        private static IntPtr GrabBitmap(TaskbarWindow taskbarWindow, System.Drawing.Size requestedSize)
        {
            IntPtr hBitmap = IntPtr.Zero;

            if (taskbarWindow.TabbedThumbnail.WindowHandle != IntPtr.Zero)
            { //TabbedThumbnail is linked to WinformsControl
                if (taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero)
                {
                    using (Bitmap bmp = TabbedThumbnailScreenCapture.GrabWindowBitmap(
                               taskbarWindow.TabbedThumbnail.WindowHandle, requestedSize))
                    {
                        hBitmap = bmp.GetHbitmap();
                    }
                }
                else
                {
                    using (Image img = Image.FromHbitmap(taskbarWindow.TabbedThumbnail.CurrentHBitmap))
                    {
                        using (Bitmap bmp = new Bitmap(img, requestedSize))
                        {
                            hBitmap = bmp != null?bmp.GetHbitmap() : IntPtr.Zero;
                        }
                    }
                }
            }
//            else if (taskbarWindow.TabbedThumbnail.WindowsControl != null)
//            { //TabbedThumbnail is linked to a WPF UIElement
//                if (taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero)
//                {
//                    Bitmap bmp = TabbedThumbnailScreenCapture.GrabWindowBitmap(
//                        taskbarWindow.TabbedThumbnail.WindowsControl,
//                        96, 96, requestedSize.Width, requestedSize.Height);
//
//                    if (bmp != null)
//                    {
//                        hBitmap = bmp.GetHbitmap();
//                        bmp.Dispose();
//                    }
//                }
//                else
//                {
//                    using (Image img = Image.FromHbitmap(taskbarWindow.TabbedThumbnail.CurrentHBitmap))
//                    {
//                        using (Bitmap bmp = new Bitmap(img, requestedSize))
//                        {
//
//                            hBitmap = bmp != null ? bmp.GetHbitmap() : IntPtr.Zero;
//                        }
//                    }
//                }
//            }

            return(hBitmap);
        }
        /// <summary>
        /// Helper function to capture a bitmap for a given window handle or incase of WPF app,
        /// an UIElement.
        /// </summary>
        /// <param name="taskbarWindow">The proxy window for which a bitmap needs to be created</param>
        /// <param name="requestedSize">Size for the requested bitmap image</param>
        /// <returns>Bitmap captured from the window handle or UIElement. Null if the window is hidden or it's size is zero.</returns>
        private static IntPtr GrabBitmap(TaskbarWindow taskbarWindow, System.Drawing.Size requestedSize)
        {
            IntPtr hBitmap = IntPtr.Zero;

            if (taskbarWindow.TabbedThumbnail.WindowHandle != IntPtr.Zero)

                //TabbedThumbnail is linked to WinformsControl
                if (taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero)

                    using (Bitmap bmp = TabbedThumbnailScreenCapture.GrabWindowBitmap(
                        taskbarWindow.TabbedThumbnail.WindowHandle, requestedSize))

                        hBitmap = bmp.GetHbitmap();

                else

                    using (Image img = Image.FromHbitmap(taskbarWindow.TabbedThumbnail.CurrentHBitmap))

                    using (var bmp = new Bitmap(img, requestedSize))

                        hBitmap = bmp != null ? bmp.GetHbitmap() : IntPtr.Zero;

            else if (taskbarWindow.TabbedThumbnail.WindowsControl != null)

                //TabbedThumbnail is linked to a WPF UIElement
                if (taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero)
                {
                    Bitmap bmp = TabbedThumbnailScreenCapture.GrabWindowBitmap(
                        taskbarWindow.TabbedThumbnail.WindowsControl,
                        96, 96, requestedSize.Width, requestedSize.Height);

                    if (bmp != null)
                    {
                        hBitmap = bmp.GetHbitmap();
                        bmp.Dispose();
                    }
                }

                else

                    using (Image img = Image.FromHbitmap(taskbarWindow.TabbedThumbnail.CurrentHBitmap))

                    using (var bmp = new Bitmap(img, requestedSize))

                        hBitmap = bmp != null ? bmp.GetHbitmap() : IntPtr.Zero;

            return hBitmap;
        }
        /// <summary>
        /// Helper function to capture a bitmap for a given window handle or incase of WPF app,
        /// an UIElement.
        /// </summary>
        /// <param name="taskbarWindow">The proxy window for which a bitmap needs to be created</param>
        /// <param name="requestedSize">Size for the requested bitmap image</param>
        /// <returns>Bitmap captured from the window handle or UIElement. Null if the window is hidden or it's size is zero.</returns>
        private IntPtr GrabBitmap(TaskbarWindow taskbarWindow, System.Drawing.Size requestedSize)
        {
            IntPtr hBitmap = IntPtr.Zero;

            if (taskbarWindow.TabbedThumbnail.WindowHandle != IntPtr.Zero)
            {
                if (taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero)
                {
                    Bitmap bmp = TabbedThumbnailScreenCapture.GrabWindowBitmap(taskbarWindow.TabbedThumbnail.WindowHandle, requestedSize);
                    hBitmap = bmp != null?bmp.GetHbitmap() : IntPtr.Zero;

                    if (bmp != null)
                    {
                        bmp.Dispose();
                        bmp = null;
                    }
                }
                else
                {
                    System.Drawing.Image  img = System.Drawing.Image.FromHbitmap(taskbarWindow.TabbedThumbnail.CurrentHBitmap);
                    System.Drawing.Bitmap bmp = new Bitmap(img, requestedSize);

                    hBitmap = bmp != null?bmp.GetHbitmap() : IntPtr.Zero;

                    // Dipose the image
                    bmp.Dispose();
                    bmp = null;

                    img.Dispose();
                    img = null;
                }
            }
            else if (taskbarWindow.TabbedThumbnail.WindowsControl != null)
            {
                if (taskbarWindow.TabbedThumbnail.CurrentHBitmap == IntPtr.Zero)
                {
                    Bitmap bmp = TabbedThumbnailScreenCapture.GrabWindowBitmap(taskbarWindow.TabbedThumbnail.WindowsControl, 96, 96, requestedSize.Width, requestedSize.Height);
                    hBitmap = bmp != null?bmp.GetHbitmap() : IntPtr.Zero;

                    if (bmp != null)
                    {
                        bmp.Dispose();
                        bmp = null;
                    }
                }
                else
                {
                    System.Drawing.Image  img = System.Drawing.Image.FromHbitmap(taskbarWindow.TabbedThumbnail.CurrentHBitmap);
                    System.Drawing.Bitmap bmp = new Bitmap(img, requestedSize);

                    hBitmap = bmp != null?bmp.GetHbitmap() : IntPtr.Zero;

                    // Dipose the image
                    bmp.Dispose();
                    bmp = null;

                    img.Dispose();
                    img = null;
                }
            }

            return(hBitmap);
        }