C# 如何呈现web浏览器并获取html标记(如“img”、“div”等)的x-y坐标,并在C中截屏?
我想通过使用C#获得一些屏幕截图(作为图像),这是许多HTML元素中的一个。我只需要知道如何迈出每一步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
您坚持哪些步骤?您的代码到底出了什么问题?即,哪一步操作不正确?您可以使用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;
}
}
}