예제 #1
0
        /// <summary>
        /// Fast Capturing screen and return the image, uses WinAPI capture if Variables.Background is false.
        /// </summary>
        public static ScreenshotData ImageCapture([CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null)
        {
            if (!ScriptRun.Run)
            {
                return(null);
            }
            if (Variables.WinApiCapt && !Instance.captureerror)
            {
                if (Variables.ProchWnd != null)
                {
                    return(ImageCapture(Variables.ProchWnd, Variables.WinApiCaptCropStart, Variables.WinApiCaptCropEnd, lineNumber, caller));
                }
                else
                {
                    return(ImageCapture(Variables.Proc.MainWindowHandle, Variables.WinApiCaptCropStart, Variables.WinApiCaptCropEnd, lineNumber, caller));
                }
            }
            Instance.captureerror = false;
            try
            {
                Stopwatch s = Stopwatch.StartNew();
                if (!Directory.Exists(Variables.SharedPath))
                {
                    Variables.ScriptLog("Warning, unable to find shared folder! Trying to use WinAPI!", Color.Red);
                    Variables.WinApiCapt = true;
                    return(ImageCapture(Variables.Proc.MainWindowHandle, Variables.WinApiCaptCropStart, Variables.WinApiCaptCropEnd, lineNumber, caller));
                }
                if (AdbInstance.Instance.pcimagepath == "" || AdbInstance.Instance.androidimagepath == "")
                {
                    var tempname = Encryption.SHA256(DateTime.Now.ToString());
                    AdbInstance.Instance.pcimagepath = (Variables.SharedPath + "\\" + tempname + ".rgba").Replace("\\\\", "\\");
                    if (Variables.AndroidSharedPath.Contains("|"))
                    {
                        foreach (var path in Variables.AndroidSharedPath.Split('|'))
                        {
                            if (EmulatorLoader.AndroidDirectoryExist(path))
                            {
                                string temppath = path;
                                if (temppath.Last() != '/')
                                {
                                    temppath += "/";
                                }
                                AdbInstance.Instance.androidimagepath = (temppath + tempname + ".rgba");
                                Variables.AdvanceLog("Multiple Android Path settes, selected " + AdbInstance.Instance.androidimagepath);
                                break;
                            }
                        }
                    }
                    else
                    {
                        AdbInstance.Instance.androidimagepath = (Variables.AndroidSharedPath + tempname + ".rgba");
                    }
                }
                byte[] raw = null;

                if (Variables.Controlled_Device == null)
                {
                    Variables.AdvanceLog("No device connected!", lineNumber, caller);
                    EmulatorLoader.ConnectAndroidEmulator();
                    return(null);
                }
                if ((Variables.Controlled_Device as DeviceData).State == SharpAdbClient.DeviceState.Offline || !ScriptRun.Run)
                {
                    return(null);
                }
                ConsoleOutputReceiver receiver = new ConsoleOutputReceiver();
                AdbInstance.Instance.client.ExecuteRemoteCommand("screencap " + AdbInstance.Instance.androidimagepath, (Variables.Controlled_Device as DeviceData), receiver);
                if (Variables.NeedPull)
                {
                    if (File.Exists(AdbInstance.Instance.pcimagepath))
                    {
                        File.Delete(AdbInstance.Instance.pcimagepath);
                    }
                    BotCore.Pull(AdbInstance.Instance.androidimagepath, AdbInstance.Instance.pcimagepath);
                }
                if (!File.Exists(AdbInstance.Instance.pcimagepath))
                {
                    Variables.AdvanceLog("Unable to read rgba file because of file not exist!", lineNumber, caller);
                    return(null);
                }
                raw = File.ReadAllBytes(AdbInstance.Instance.pcimagepath);
                int expectedsize = (Variables.EmulatorHeight * Variables.EmulatorWidth * 4) + 12;
                if (raw.Length != expectedsize || raw.Length > int.MaxValue || raw.Length < 1)
                {
                    //Image is not in same size, resize emulator
                    EmulatorLoader.ResizeEmulator();
                    return(null);
                }
                byte[] img = new byte[raw.Length - 12]; //remove header
                Array.Copy(raw, 12, img, 0, img.Length);
                Image <Rgba, byte> image = new Image <Rgba, byte>(Variables.EmulatorWidth, Variables.EmulatorHeight);
                image.Bytes = img;
                if (Variables.ImageDebug)
                {
                    image.Save("Profiles\\Logs\\" + Encryption.SHA256(DateTime.Now.ToString()) + ".bmp");
                }
                Variables.AdvanceLog("Screenshot saved to memory used " + s.ElapsedMilliseconds + " ms", lineNumber, caller);
                s.Stop();
                return(Compress(image.Bitmap));
            }
            catch (IOException)
            {
            }
            catch (ArgumentOutOfRangeException)
            {
            }
            return(null);
        }