コード例 #1
0
        /// <summary>
        /// Screenshot the emulator screen using PrintWindow. Careful here as we might get blank image if exception found
        /// </summary>
        /// <returns></returns>
        public virtual IImageData Screenshot(bool Bgr_Rgb = true, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null)
        {
            Stopwatch s = Stopwatch.StartNew();

Retry:
            try
            {
                if (!KeepBackground)
                {
                    CaptureMethod = EmulatorCaptureMethod.ForegroundCapture;
                }
                if (CaptureError >= 5)
                {
                    if (KeepBackground)
                    {
                        KeepBackground = !KeepBackground;
                    }
                    CaptureMethod = EmulatorCaptureMethod.AdbCapture;
                }
                IImageData result;
                switch (CaptureMethod)
                {
                case EmulatorCaptureMethod.AdbCapture:
                    result = adbController.Screenshot().Result;
                    break;

                case EmulatorCaptureMethod.ForegroundCapture:
                    result = ForegroundCapture(lineNumber, caller);
                    break;

                case EmulatorCaptureMethod.WinApiCapture:
                    result = WinApiCapture(lineNumber, caller);
                    break;

                default:
                    throw new ArgumentException("How the hell can you input something like this to me??");
                }
                CaptureError = 0;
                if (ResizeScreenshot)
                {
                    if (rect.HasValue)
                    {
                        return(result.Crop(rect.Value));
                    }
                    else
                    {
                        return(result.Crop(emulator.ActualSize()));
                    }
                }
                else
                {
                    return(result);
                }
            }
            catch (Exception ex)
            {
                if (ex is InvalidDataException)
                {
                    goto Retry;
                }
                Bitmap bmp;
                if (rect.HasValue)
                {
                    bmp = new Bitmap(emulator.DefaultSize().Width, emulator.DefaultSize().Height);
                }
                else
                {
                    bmp = new Bitmap(emulator.DefaultSize().Width, emulator.DefaultSize().Height);
                }
                //            logger.WritePrivateLog("Screenshot saved to memory used " + s.ElapsedMilliseconds + " ms", lineNumber, caller);
                logger.WritePrivateLog("Screenshot found exception: " + ex.Message + " at line " + new StackTrace(ex, true).GetFrame(0).GetFileLineNumber() + " used " + s.ElapsedMilliseconds + " ms", lineNumber, caller);
                return(new ScreenShot(bmp, logger, false));
            }
        }