Beispiel #1
0
        /// <summary>
        ///     Capture screenshot of a selected element and save it as .jpeg file in specified file path.
        /// </summary>
        public static string ElementScreenshot(BaseElement element, string fileName, string filePath)
        {
            try
            {
                //element.WaitForDisplay();

                if (!element.IsDisplayed())
                {
                    throw new Exception("Couldn't capture element " + element.GetLocator());
                }
                var destinationPath = CaptureScreenshot(fileName, filePath);

                var image = Image.FromFile(destinationPath);

                var rect = new Rectangle(element.GetPointX(), element.GetPointY(), element.GetWidth(),
                                         element.GetHeight());

                var originalImage = new Bitmap(image);
                var croppedImage  = originalImage.Clone(rect, originalImage.PixelFormat);

                image.Dispose();
                originalImage.Dispose();

                croppedImage.Save(destinationPath, ImageFormat.Jpeg);

                return(destinationPath);
            }
            catch (Exception error)
            {
                throw error;
            }
        }