StretchBlt() private method

private StretchBlt ( IntPtr hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest, IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, uint dwRop ) : bool
hdcDest IntPtr
nXOriginDest int
nYOriginDest int
nWidthDest int
nHeightDest int
hdcSrc IntPtr
nXOriginSrc int
nYOriginSrc int
nWidthSrc int
nHeightSrc int
dwRop uint
return bool
        /// <summary>
        /// Captures a screenshot of the specified window at the specified
        /// bitmap size. <para/>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).
        /// </summary>
        /// <param name="windowHandle">The window handle.</param>
        /// <param name="bitmapSize">The requested bitmap size.</param>
        /// <returns>A screen capture of the window.</returns>
        public static Bitmap GrabWindowBitmap(IntPtr windowHandle, System.Drawing.Size bitmapSize)
        {
            if (bitmapSize.Height <= 0 || bitmapSize.Width <= 0)
            {
                return(null);
            }

            IntPtr windowDC = IntPtr.Zero;

            try
            {
                windowDC = HandlerNativeMethods.GetWindowDC(windowHandle);

                _ = HandlerNativeMethods.GetClientSize(windowHandle, out System.Drawing.Size realWindowSize);

                if (realWindowSize == System.Drawing.Size.Empty)
                {
                    realWindowSize = new System.Drawing.Size(200, 200);
                }

                System.Drawing.Size size = (bitmapSize == System.Drawing.Size.Empty) ?
                                           realWindowSize : bitmapSize;

                Bitmap targetBitmap = null;

                try
                {
                    targetBitmap = new Bitmap(size.Width, size.Height);

                    using (var targetGr = Graphics.FromImage(targetBitmap))
                    {
                        IntPtr targetDC  = targetGr.GetHdc();
                        uint   operation = 0x00CC0020 /*SRCCOPY*/;

                        System.Drawing.Size ncArea = Shell.DesktopWindowManager.GetNonClientArea(windowHandle);

                        bool success = GDI.StretchBlt(
                            targetDC, 0, 0, targetBitmap.Width, targetBitmap.Height,
                            windowDC, ncArea.Width, ncArea.Height, realWindowSize.Width,
                            realWindowSize.Height, operation);

                        targetGr.ReleaseHdc(targetDC);

                        return(success ? targetBitmap : null);
                    }
                }
                catch
                {
                    targetBitmap?.Dispose();
                    throw;
                }
            }
            finally
            {
                if (windowDC != IntPtr.Zero)
                {
                    _ = HandlerNativeMethods.ReleaseDC(windowHandle, windowDC);
                }
            }
        }
コード例 #2
0
 public void StretchPrintDrawBitmap(RECT rect, Bitmap bitmap)
 {
     using (Graphics srcgraphics = Graphics.FromImage(bitmap))
     {
         IntPtr vImageHDC = srcgraphics.GetHdc();
         try
         {
             IntPtr vMemDC   = (IntPtr)GDI.CreateCompatibleDC(vImageHDC);
             IntPtr vHbitmap = bitmap.GetHbitmap();// (IntPtr)GDI.CreateCompatibleBitmap(vImageHDC, FImage.Width, FImage.Height);
             GDI.SelectObject(vMemDC, vHbitmap);
             //GDI.BitBlt(aCanvas.Handle, aDrawRect.Left, aDrawRect.Top, aDrawRect.Width, aDrawRect.Height, vMemDC, 0, 0, GDI.SRCCOPY);
             GDI.StretchBlt(FHandle, rect.Left, rect.Top, rect.Width, rect.Height, vMemDC, 0, 0, bitmap.Width, bitmap.Height, GDI.SRCCOPY);
             GDI.DeleteDC(vMemDC);
             GDI.DeleteObject(vHbitmap);
         }
         finally
         {
             srcgraphics.ReleaseHdc(vImageHDC);
         }
     }
 }