Example #1
0
        public Image GetStaticThumbnail(IntPtr source)
        {
            var sourceContext = User32NativeMethods.GetDC(source);

            User32NativeMethods.GetClientRect(source, out RECT windowRect);

            var width  = windowRect.Right - windowRect.Left;
            var height = windowRect.Bottom - windowRect.Top;

            // Check if there is anything to make thumbnail of
            if ((width < WINDOW_SIZE_THRESHOLD) || (height < WINDOW_SIZE_THRESHOLD))
            {
                return(null);
            }

            var destContext = Gdi32NativeMethods.CreateCompatibleDC(sourceContext);
            var bitmap      = Gdi32NativeMethods.CreateCompatibleBitmap(sourceContext, width, height);

            var oldBitmap = Gdi32NativeMethods.SelectObject(destContext, bitmap);

            Gdi32NativeMethods.BitBlt(destContext, 0, 0, width, height, sourceContext, 0, 0, Gdi32NativeMethods.SRCCOPY);
            Gdi32NativeMethods.SelectObject(destContext, oldBitmap);
            Gdi32NativeMethods.DeleteDC(destContext);
            User32NativeMethods.ReleaseDC(source, sourceContext);

            Image image = Image.FromHbitmap(bitmap);

            Gdi32NativeMethods.DeleteObject(bitmap);

            return(image);
        }
Example #2
0
        public Bitmap Capture()
        {
            if (!EnableWriterBitmap)
            {
                UpdateWriterBitmap();
            }

            IntPtr windowDC = User32NativeMethods.GetWindowDC(windowHandle);
            IntPtr hDC      = writerBitmapGraphcis.GetHdc();

            Gdi32NativeMethods.BitBlt(hDC,
                                      0,
                                      0,
                                      WriterBitmap.Width, WriterBitmap.Height,
                                      windowDC,
                                      CaptureBoundsInWindow.X,
                                      CaptureBoundsInWindow.Y,
                                      (int)Gdi32NativeMethods.TernaryRasterOperations.SRCCOPY);
            writerBitmapGraphcis.ReleaseHdc(hDC);
            User32NativeMethods.ReleaseDC(windowHandle, windowDC);

            return(WriterBitmap);
        }