Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何呈现web浏览器并获取html标记(如“img”、“div”等)的x-y坐标,并在C中截屏?_C#_Html_Css_Svg - Fatal编程技术网

C# 如何呈现web浏览器并获取html标记(如“img”、“div”等)的x-y坐标,并在C中截屏?

C# 如何呈现web浏览器并获取html标记(如“img”、“div”等)的x-y坐标,并在C中截屏?,c#,html,css,svg,C#,Html,Css,Svg,我想通过使用C#获得一些屏幕截图(作为图像),这是许多HTML元素中的一个。我只需要知道如何迈出每一步 加载Internet Explorer和web URL(例如stackoverflow.com等 在……上 解析DOM(文档对象模型)结构 呈现此网页 获取x-y坐标(或 位置信息)的HTML元素,我想得到(e.f。 “]*?src\s*=\s*[”“”]?([^'”>]+?)['”][^>]*?>”; MatchCollection matchesImgSrc=Regex.Matches(h

我想通过使用C#获得一些屏幕截图(作为图像),这是许多HTML元素中的一个。我只需要知道如何迈出每一步

  • 加载Internet Explorer和web URL(例如stackoverflow.com等 在……上
  • 解析DOM(文档对象模型)结构
  • 呈现此网页
  • 获取x-y坐标(或 位置信息)的HTML元素,我想得到(e.f。 “]*?src\s*=\s*[”“”]?([^'”>]+?)['”][^>]*?>”; MatchCollection matchesImgSrc=Regex.Matches(htmlSource、regexImgSrc、RegexOptions.IgnoreCase | RegexOptions.Singleline); foreach(matchesImgSrc中的匹配m) { 字符串href=m.Groups[1]。值; links.Add(新Uri(href)); } 返回链接; } 公共静态int findPosX(IHTMlement对象) { int curleft=0; if(obj.offsetParent!=null) { while(obj.offsetParent!=null) { curleft+=obj.offsetLeft; obj=obj.offsetParent; } } 返回卷曲; } 公共静态int findPosY(IHTMlement对象) { int-curtop=0; if(obj.offsetParent!=null) { while(obj.offsetParent!=null) { curtop+=对象偏移量; obj=obj.offsetParent; } } 返回curtop; } } }
    您坚持哪些步骤?您的代码到底出了什么问题?即,哪一步操作不正确?您可以使用selenium控制浏览器,而不是使用HttpWebRequest自己进行渲染,并轻松获得渲染和定位
    namespace ConsoleApplication2
    {
        class Program
        {
    
            public static void Main(string[] args)
            {
                string webPageURL = "http://www.google.com";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webPageURL);
                request.Credentials = System.Net.CredentialCache.DefaultCredentials;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                //IWebElement element = driver.FindElement(By.Id("SubmitButton"));
                //Point point = element.Location;
                //Console.WriteLine("X cordinate : " + point.X + "Y cordinate: " + point.Y);
    
                SvgPoint sp = new SvgPoint();
                //SvgRenderer sr = new SvgRenderer();
                //SvgElement se = new SvgElement;
                SvgPointCollection spc = new SvgPointCollection();
    
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                    {
                        string htmlContent = sr.ReadToEnd();
                        List<Uri> links = FetchLinksFromSource(htmlContent);
    
                        for (int i = 0; i < links.Count; i++)
                        {
                            Console.WriteLine(links[i]);
                        }
    
                        using (WebClient client = new WebClient())
                        {
                            for (int i = 0; i < links.Count; i++)
                            { 
                                WebBrowser WebBrowser1 = new WebBrowser();
                                //WebBrowser1.Document.GetElementsByTagName("img");
    
                                IHTMLElement htmldoc = (IHTMLElement)new HTMLDocument();
                                //IHTMLElement htmldoc1 = (IHTMLElement)Document.body;
    
                                WebBrowser1.Navigate(webPageURL);
    
                                sp.X = findPosX(htmldoc);
                                sp.Y = findPosY(htmldoc);
    
                            }
                        }
                    }
                }
            }
    
    
            public static List<Uri> FetchLinksFromSource(string htmlSource)
            {
                List<Uri> links = new List<Uri>();
                string regexImgSrc = @"<img[^>]*?src\s*=\s*[""']?([^'"" >]+?)[ '""][^>]*?>";
                MatchCollection matchesImgSrc = Regex.Matches(htmlSource, regexImgSrc, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                foreach (Match m in matchesImgSrc)
                {
                    string href = m.Groups[1].Value;
                    links.Add(new Uri(href));
                }
                return links;
            }
    
    
            public static int findPosX(IHTMLElement obj)
            {
                int curleft = 0;
                if (obj.offsetParent != null)
                {
                    while (obj.offsetParent != null)
                    {
                        curleft += obj.offsetLeft;
                        obj = obj.offsetParent;
                    }
                }
    
                return curleft;
            }
    
    
            public static int findPosY(IHTMLElement obj)
            {
                int curtop = 0;
                if (obj.offsetParent != null)
                {
                    while (obj.offsetParent != null)
                    {
                        curtop += obj.offsetTop;
                        obj = obj.offsetParent;
                    }
                }
    
                return curtop;
            }
        }
    }