Пример #1
0
        /// <summary>
        /// Used to capture and save the screenshot, NOTICE that it can only be called by a STA thread.
        /// </summary>
        /// <param name="outPath"></param>
        private static void SaveAsPNG(string outPath)
        {
            KeyboardUtils.PressScreenshot();
            Thread.Sleep(1000);
            if (!Clipboard.ContainsImage())
            {
                return;
            }
            int width  = (int)(SystemParameters.WorkArea.Width);
            int height = (int)(SystemParameters.WorkArea.Height);

            using (MemoryStream ms = new MemoryStream())
            {
                BitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(Clipboard.GetImage()));
                enc.Save(ms);
                Bitmap img = null;
                try
                {
                    img = (new Bitmap(ms));
                    img.Save(outPath, ImageFormat.Png);
                }
                finally
                {
                    if (null != img)
                    {
                        img.Dispose();
                    }
                }
            }
        }
Пример #2
0
        private static bool LoadActions(ref bool shouldLoop, ref Action <string> action)
        {
            if (!File.Exists(actionFileName))
            {
                return(false);
            }

            XDocument xDoc = XDocument.Load(actionFileName);

            bool.TryParse(xDoc.Element("snipActions").Element("repeat").Value, out shouldLoop);
            action = null;
            foreach (var step in xDoc.Descendants("step"))
            {
                int  time     = 0;
                bool needWait = false;
                if (step.HasAttributes)
                {
                    needWait = int.TryParse(step.Attribute("time").Value, out time);
                    if (!needWait)
                    {
                        return(false);
                    }
                }
                var             stepStr     = step.Value.ToUpper().Split(' ');
                Action <string> innerAction = null;
                switch (stepStr[0])
                {
                case "PRESS":
                    KeyboardUtils.Key key;
                    if (Enum.TryParse <KeyboardUtils.Key>(stepStr[1].ToUpper(), out key))
                    {
                        innerAction = (str) =>
                        {
                            KeyboardUtils.Press(key);
                            Thread.Sleep(300);
                        };
                    }
                    break;

                case "SNIP":
                    innerAction = (str) =>
                    {
                        string fileName = (string.IsNullOrWhiteSpace(str) ? "" : str + "_") + stepStr[1] + ".png";
                        SnipUtils.CaptureScreen(fileName);
                        Thread.Sleep(300);
                    };
                    break;

                case "WAIT":
                    int wait;
                    if (int.TryParse(stepStr[1], out wait))
                    {
                        innerAction = (str) =>
                        {
                            Thread.Sleep(wait);
                        };
                    }
                    break;

                default:
                    return(false);
                }
                if (needWait)
                {
                    action += (str) =>
                    {
                        Thread td = new Thread(() => Thread.Sleep(time));
                        td.Start();
                        innerAction?.Invoke(str);
                        td.Join();
                    };
                }
                else
                {
                    action += innerAction;
                }
            }
            return(true);
        }