Exemplo n.º 1
0
 public string Capture(string windowName = null,
     int x = 0, int y = 0, int width = -1, int height = -1)
 {
     Bitmap b = null;
     ScreenShot ss = null;
     AutomationElement windowHandle;
     try
     {
         ss = new ScreenShot();
         // capture entire screen, and save it to a file
         string path = Path.GetTempPath() +
             Path.GetRandomFileName() + ".png";
         if (windowName.Length > 0)
         {
             windowHandle = utils.GetWindowHandle(windowName);
             if (windowHandle == null)
             {
                 throw new XmlRpcFaultException(123,
                     "Unable to find window: " + windowName);
             }
             windowHandle.SetFocus();
             Rect rect = windowHandle.Current.BoundingRectangle;
             System.Drawing.Rectangle rectangle = new
                 System.Drawing.Rectangle((int)rect.X, (int)rect.Y,
                 (int)rect.Width, (int)rect.Height);
             b = ss.CaptureSize(path, rectangle);
         }
         else if (width != -1 && height != -1)
         {
             System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(
                 x, y, width, height);
             b = ss.CaptureSize(path, rectangle);
         }
         else
         {
             b = ss.Capture(path);
         }
         string encodedText = "";
         using (FileStream fs = File.Open(path, FileMode.Open,
             FileAccess.Read))
         {
             Byte[] bytesToEncode = new byte[fs.Length];
             fs.Read(bytesToEncode, 0, (int)fs.Length);
             encodedText = Convert.ToBase64String(bytesToEncode);
             fs.Close();
         }
         LogMessage(path);
         File.Delete(path);
         return encodedText;
     }
     catch (Exception ex)
     {
         LogMessage(ex);
         if (ex is XmlRpcFaultException)
             throw;
         throw new XmlRpcFaultException(123,
             "Unhandled exception: " + ex.Message);
     }
     finally
     {
         if (b != null) // To avoid compilation warning
             b = null;
         ss = null;
         windowHandle = null;
     }
 }
Exemplo n.º 2
0
        public string Capture(string windowName = null,
                              int x             = 0, int y = 0, int width = -1, int height = -1)
        {
            Bitmap            b  = null;
            ScreenShot        ss = null;
            AutomationElement windowHandle;

            try
            {
                ss = new ScreenShot();
                // capture entire screen, and save it to a file
                string path = Path.GetTempPath() +
                              Path.GetRandomFileName() + ".png";
                if (windowName.Length > 0)
                {
                    windowHandle = utils.GetWindowHandle(windowName);
                    if (windowHandle == null)
                    {
                        throw new XmlRpcFaultException(123,
                                                       "Unable to find window: " + windowName);
                    }
                    windowHandle.SetFocus();
                    Rect rect = windowHandle.Current.BoundingRectangle;
                    x      = (x != 0) ? x : (int)rect.X;
                    y      = (y != 0) ? y : (int)rect.Y;
                    width  = (width != -1) ? width : (int)rect.Width;
                    height = (height != -1) ? height : (int)rect.Height;
                    System.Drawing.Rectangle rectangle = new
                                                         System.Drawing.Rectangle(x, y, width, height);
                    b = ss.CaptureSize(path, rectangle);
                }
                else if (width != -1 && height != -1)
                {
                    System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(
                        x, y, width, height);
                    b = ss.CaptureSize(path, rectangle);
                }
                else
                {
                    b = ss.Capture(path);
                }
                string encodedText = "";
                using (FileStream fs = File.Open(path, FileMode.Open,
                                                 FileAccess.Read))
                {
                    Byte[] bytesToEncode = new byte[fs.Length];
                    fs.Read(bytesToEncode, 0, (int)fs.Length);
                    encodedText = Convert.ToBase64String(bytesToEncode);
                    fs.Close();
                }
                LogMessage(path);
                File.Delete(path);
                return(encodedText);
            }
            catch (Exception ex)
            {
                LogMessage(ex);
                if (ex is XmlRpcFaultException)
                {
                    throw;
                }
                throw new XmlRpcFaultException(123,
                                               "Unhandled exception: " + ex.Message);
            }
            finally
            {
                if (b != null) // To avoid compilation warning
                {
                    b = null;
                }
                ss           = null;
                windowHandle = null;
            }
        }