Exemple #1
0
        private void OnHotKeyPush(object sender, EventArgs e)
        {
            timer.Enabled = false;
            WindowModel.ClearRect();

            var mouse = new MouseModel();
            int x     = mouse.Position.X;
            int y     = mouse.Position.Y;

            // Capture window where cursor points
            WindowModel w   = WindowModel.FindByPosition(x, y);
            Bitmap      bmp = w.CaptureWindow();

            // Send capture image to server
            using (var stream = new MemoryStream())
            {
                // Make http content object from capture image
                bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                stream.Position = 0;
                HttpContent content = new StreamContent(stream);

                using (var client = new HttpClient())
                    using (var formData = new MultipartFormDataContent())
                    {
                        formData.Add(content, "screenshot", "screenshot.png");
                        formData.Add(new StringContent(w.WindowTitle), "title");

                        var response = client.PostAsync(this.CaptureUploadUrl, formData).Result;
                        if (!response.IsSuccessStatusCode)
                        {
                            throw new Exception("Sending captured image failed");
                        }
                    }
            }

            Finish();
        }
        private void FindWindowAndTryMatch(Bitmap imageToMatch, out WindowModel window, out Rectangle matchingRect)
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            while (true)
            {
                try
                {
                    // Capture target window
                    window = WindowModel.FindByTitle(WindowTitle);

                    Bitmap capture = window.CaptureWindow();

                    // Find matching area in target window
                    matchingRect = FindMatchingRect(capture, imageToMatch);
                }
                catch (Exception)
                {
                    stopwatch.Stop();
                    if (stopwatch.Elapsed.TotalSeconds > Timeout)
                    {
                        throw;
                    }

                    Thread.Sleep(500);

                    // Retry
                    stopwatch.Start();
                    continue;
                }

                break;
            }
        }