public List <PageElement> FindAllLinks(GeckoWebBrowser browser, Point clickPosition)
 {
     visibleLinks.Clear();
     elements.Clear();
     FindElements(browser, "A", clickPosition);
     FindElements(browser, "IMG", clickPosition);
     foreach (var image in elements)
     {
         foreach (var link in visibleLinks)
         {
             if (image.HtmlElement.Equals(link.HtmlElement))
             {
                 link.Distance = -1;
                 break;
             }
         }
     }
     foreach (var image in elements)
     {
         visibleLinks.Add(image);
     }
     visibleLinks.RemoveAll(link => link.Distance == -1);
     visibleLinks.Sort((a, b) => a.Distance.CompareTo(b.Distance));
     if (visibleLinks.Count != 0)
     {
         actualElement = visibleLinks[0];
     }
     return(visibleLinks);
 }
        public Image TakeElementScreenshot(GeckoWebBrowser browser, PageElement element)
        {
            Rectangle rectangle = new Rectangle(0, 0, 0, 0);

            if (element.RelativeX < 0)
            {
                rectangle.X     = browser.PointToScreen(Point.Empty).X;
                rectangle.Width = element.Width + element.RelativeX;
            }
            else
            {
                rectangle.X = browser.PointToScreen(Point.Empty).X + element.RelativeX;
            }

            if (element.RelativeY < 0)
            {
                rectangle.Y      = browser.PointToScreen(Point.Empty).Y;
                rectangle.Height = element.Height + element.RelativeY;
            }
            else
            {
                rectangle.Y = browser.PointToScreen(Point.Empty).Y + element.RelativeY;
            }

            if (browser.PointToScreen(Point.Empty).X + browser.Width < rectangle.X + element.Width)
            {
                rectangle.Width = browser.PointToScreen(Point.Empty).X + browser.Width - rectangle.X;
            }
            else if (rectangle.Width == 0)
            {
                rectangle.Width = element.Width;
            }

            if (browser.PointToScreen(Point.Empty).Y + browser.Height < rectangle.Y + element.Height)
            {
                rectangle.Height = browser.PointToScreen(Point.Empty).Y + browser.Height - rectangle.Y;
            }
            else if (rectangle.Height == 0)
            {
                rectangle.Height = element.Height;
            }

            Bitmap bitmap = null;

            try
            {
                bitmap = new Bitmap(rectangle.Width, rectangle.Height, PixelFormat.Format32bppArgb);
            } catch (Exception e)
            {
                bitmap = new Bitmap(5, 5, PixelFormat.Format32bppArgb);
            }
            Graphics graphics = Graphics.FromImage(bitmap);

            graphics.CopyFromScreen(rectangle.X, rectangle.Y, 0, 0, bitmap.Size, CopyPixelOperation.SourceCopy);
            return(bitmap);
        }
        private PageElement FindNextLink(GeckoWebBrowser browser, int direction)
        {
            Point p1 = new Point(), p2 = new Point(), p3 = new Point();

            if (direction == 1)
            {
                p1.X = actualElement.RelativeX;
                p2.X = p1.X + actualElement.Width / 2;
                p3.X = p1.X + actualElement.Width;
                p1.Y = actualElement.RelativeY;
                p2.Y = p1.Y;
                p3.Y = p1.Y;
            }
            else if (direction == 2)
            {
                p1.X = actualElement.RelativeX;
                p2.X = p1.X;
                p3.X = p1.X;
                p1.Y = actualElement.RelativeY;
                p2.Y = p1.Y + actualElement.Height / 2;
                p3.Y = p1.Y + actualElement.Height;
            }
            else if (direction == 3)
            {
                p1.X = actualElement.RelativeX;
                p2.X = p1.X + actualElement.Width / 2;
                p3.X = p1.X + actualElement.Width;
                p1.Y = actualElement.RelativeY + actualElement.Height;
                p2.Y = p1.Y;
                p3.Y = p1.Y;
            }
            else if (direction == 4)
            {
                p1.X = actualElement.RelativeX + actualElement.Width;
                p2.X = p1.X;
                p3.X = p1.X;
                p1.Y = actualElement.RelativeY;
                p2.Y = p1.Y + actualElement.Height / 2;
                p3.Y = p1.Y + actualElement.Height;
            }

            if (direction == 1)
            {
                while (p1.Y > 0)
                {
                    p1.Y -= 3;
                    foreach (var link in visibleLinks)
                    {
                        if (p1.Y >= link.RelativeY && p1.Y <= link.RelativeY + link.Height &&
                            ((p1.X >= link.RelativeX && p1.X <= link.RelativeX + link.Width) ||
                             (p2.X >= link.RelativeX && p2.X <= link.RelativeX + link.Width) ||
                             (p3.X >= link.RelativeX && p3.X <= link.RelativeX + link.Width)))
                        {
                            actualElement = link;
                            return(link);
                        }
                    }
                }
            }
            else if (direction == 2)
            {
                while (p1.X > 0)
                {
                    p1.X -= 3;
                    foreach (var link in visibleLinks)
                    {
                        if (p1.X >= link.RelativeX && p1.X <= link.RelativeX + link.Width &&
                            ((p1.Y >= link.RelativeY && p1.Y <= link.RelativeY + link.Height) ||
                             (p2.Y >= link.RelativeY && p2.Y <= link.RelativeY + link.Height) ||
                             (p3.Y >= link.RelativeY && p3.Y <= link.RelativeY + link.Height)))
                        {
                            actualElement = link;
                            return(link);
                        }
                    }
                }
            }
            else if (direction == 3)
            {
                while (p1.Y < browser.Height)
                {
                    p1.Y += 3;
                    foreach (var link in visibleLinks)
                    {
                        if (p1.Y >= link.RelativeY && p1.Y <= link.RelativeY + link.Height &&
                            ((p1.X >= link.RelativeX && p1.X <= link.RelativeX + link.Width) ||
                             (p2.X >= link.RelativeX && p2.X <= link.RelativeX + link.Width) ||
                             (p3.X >= link.RelativeX && p3.X <= link.RelativeX + link.Width)))
                        {
                            actualElement = link;
                            return(link);
                        }
                    }
                }
            }
            else if (direction == 4)
            {
                while (p1.X < browser.Width)
                {
                    p1.X += 3;
                    foreach (var link in visibleLinks)
                    {
                        if (p1.X >= link.RelativeX && p1.X <= link.RelativeX + link.Width &&
                            ((p1.Y >= link.RelativeY && p1.Y <= link.RelativeY + link.Height) ||
                             (p2.Y >= link.RelativeY && p2.Y <= link.RelativeY + link.Height) ||
                             (p3.Y >= link.RelativeY && p3.Y <= link.RelativeY + link.Height)))
                        {
                            actualElement = link;
                            return(link);
                        }
                    }
                }
            }

            return(actualElement);
        }