private IImageData WinApiCapture(int lineNumber, string caller)
        {
            var       handle = getCurrentAndroidHWnd();
            Rectangle rc     = new Rectangle();

            Imports.GetWindowRect(handle, ref rc);
            Bitmap bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb);

            Imports.RedrawWindow(handle, rc, IntPtr.Zero, 0x85);
            Imports.UpdateWindow(handle);
            Graphics gfxBmp    = Graphics.FromImage(bmp);
            IntPtr   hdcBitmap = gfxBmp.GetHdc();

            Imports.PrintWindow(handle, hdcBitmap, 0);
            gfxBmp.ReleaseHdc(hdcBitmap);
            gfxBmp.Dispose();
            if (bmp.AllBlack())
            {
                CaptureError++;
                bmp.Dispose();
                logger.WritePrivateLog("Screenshot get black image, retrying now!", lineNumber, caller);
                throw new InvalidDataException();
            }
            return(new ScreenShot(bmp, logger, false));
        }
        private IImageData ForegroundCapture(int lineNumber, string caller)
        {
            var       handle = getCurrentAndroidHWnd();
            Rectangle rc     = new Rectangle();

            Imports.GetWindowRect(handle, ref rc);
            var bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb);

            using (Graphics graphics = Graphics.FromImage(bmp))
            {
                Imports.SetForegroundWindow(handle);
                graphics.CopyFromScreen(rc.X, rc.Y, 0, 0, new Size(rc.Width, rc.Height), CopyPixelOperation.SourceCopy);
            }
            if (bmp.AllBlack())
            {
                CaptureError++;
                bmp.Dispose();
                logger.WritePrivateLog("Screenshot get black image, retrying now!", lineNumber, caller);
                throw new InvalidDataException();
            }
            return(new ScreenShot(bmp, logger, false));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Get screenshot image
        /// </summary>
        /// <param name="lineNumber"></param>
        /// <param name="caller"></param>
        /// <returns></returns>
        public IImageData Screenshot(bool Bgr_Rgb = true, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null)
        {
            Stopwatch s      = Stopwatch.StartNew();
            var       handle = GetIntPtr();
            Rectangle rc     = new Rectangle();

            Imports.GetWindowRect(handle, ref rc);
            Bitmap bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb);

            for (int x = 0; x < 5; x++)
            {
                if (x > 3)
                {
                    //Try use DirectX
                    CaptureMethod = CaptureMethod.GraphicCopy;
                }
                if (CaptureMethod == CaptureMethod.HDCCapture)
                {
                    Graphics gfxBmp    = Graphics.FromImage(bmp);
                    IntPtr   hdcBitmap = gfxBmp.GetHdc();
                    Imports.PrintWindow(handle, hdcBitmap, 0);
                    gfxBmp.ReleaseHdc(hdcBitmap);
                    gfxBmp.Dispose();
                }
                else if (CaptureMethod == CaptureMethod.GDIPlus)
                {
                    IntPtr desktopDc;
                    IntPtr memoryDc;
                    IntPtr bitmap;
                    IntPtr oldBitmap;
                    Imports.SetForegroundWindow(handle);
                    Imports.GetForegroundWindow();
                    Screencaptureptr = Imports.GetDesktopWindow();
                    desktopDc        = Imports.GetWindowDC(Screencaptureptr);
                    memoryDc         = Imports.CreateCompatibleDC(desktopDc);
                    bitmap           = Imports.CreateCompatibleBitmap(desktopDc, rc.Width, rc.Height);
                    oldBitmap        = Imports.SelectObject(memoryDc, bitmap);
                    if (Imports.BitBlt(memoryDc, 0, 0, rc.Width, rc.Height, desktopDc, rc.Left, rc.Top, 0x00CC0020 | 0x40000000))
                    {
                        bmp = Image.FromHbitmap(bitmap);
                    }
                    else
                    {
                        logger.WritePrivateLog("Screenshot failed on GDI+!", lineNumber, caller);
                        throw new Win32Exception("Screenshot failed on GDI+ and PrintWindow! I can't processed anymore! Sorry! TAT");
                    }
                    Imports.SelectObject(memoryDc, oldBitmap);
                    Imports.DeleteObject(bitmap);
                    Imports.DeleteDC(memoryDc);
                    Imports.ReleaseDC(handle, desktopDc);
                }
                else
                {
                }
                if (bmp.AllBlack())
                {
                    logger.WritePrivateLog("Screenshot fetched all black!", lineNumber, caller);
                    continue;
                }
                logger.WritePrivateLog("Screenshot saved to memory used " + s.ElapsedMilliseconds + " ms", lineNumber, caller);
                s.Stop();
                break;
            }
            if (rect.HasValue)
            {
                return(new ScreenShot(bmp, logger, Bgr_Rgb).Crop(rect.Value));
            }
            else
            {
                return(new ScreenShot(bmp, logger, Bgr_Rgb));
            }
        }