C# c windows窗体,在应用程序中加载第一个google图像
我想知道,是否有可能在VisualStudioWindows窗体中显示google图片搜索的第一幅图像 我认为这是可行的,一个人输入一个字符串,然后应用程序用谷歌搜索字符串,复制第一个图像,然后在应用程序中显示 多谢各位 <>编辑:请考虑一下,我是C编程的初学者,所以如果你要用一些困难的代码或建议使用一些API,你能详细解释一下怎么做吗?谢谢。 < P>简短的回答,是的。 我们知道获取图像的URL是 在表单上创建一个PictureBoxCall it pbImage,一个文本框称之为tbSearch,一个按钮称之为btnLookup 使用Nuget软件包管理器工具->Nuget..->管理..,选择浏览并搜索HtmlAlityPack。单击右侧的“您的项目”,然后单击“安装” 当我们使用System.Net.WebClient向google发送请求时,不会执行javascript,但是这可以通过winforms web浏览器的一些技巧来实现 由于没有javascript,页面的呈现方式将与您习惯的不同。在没有javascript的情况下检查页面会告诉我们页面的以下流程: 在文档体中,包含一个名为“images\u table”的类的表 其中我们可以找到几个img元素。 下面是一个代码列表:C# c windows窗体,在应用程序中加载第一个google图像,c#,image,display,C#,Image,Display,我想知道,是否有可能在VisualStudioWindows窗体中显示google图片搜索的第一幅图像 我认为这是可行的,一个人输入一个字符串,然后应用程序用谷歌搜索字符串,复制第一个图像,然后在应用程序中显示 多谢各位 编辑:请考虑一下,我是C编程的初学者,所以如果你要用一些困难的代码或建议使用一些API,你能详细解释一下怎么做吗?谢谢。 < P>简短的回答,是的。 我们知道获取图像的URL是 在表单上创建一个PictureBoxCall it pbImage,一个文本框称之为tbSearch
private void btnLookup_Click(object sender, EventArgs e)
{
string templateUrl = @"https://www.google.co.uk/search?q={0}&tbm=isch&site=imghp";
//check that we have a term to search for.
if (string.IsNullOrEmpty(tbSearch.Text))
{
MessageBox.Show("Please supply a search term"); return;
}
else
{
using (WebClient wc = new WebClient())
{
//lets pretend we are IE8 on Vista.
wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)");
string result = wc.DownloadString(String.Format(templateUrl, new object[] { tbSearch.Text }));
//we have valid markup, this will change from time to time as google updates.
if (result.Contains("images_table"))
{
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(result);
//lets create a linq query to find all the img's stored in that images_table class.
/*
* Essentially we get search for the table called images_table, and then get all images that have a valid src containing images?
* which is the string used by google
eg https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQmGxh15UUyzV_HGuGZXUxxnnc6LuqLMgHR9ssUu1uRwy0Oab9OeK1wCw
*/
var imgList = from tables in doc.DocumentNode.Descendants("table")
from img in tables.Descendants("img")
where tables.Attributes["class"] != null && tables.Attributes["class"].Value == "images_table"
&& img.Attributes["src"] != null && img.Attributes["src"].Value.Contains("images?")
select img;
byte[] downloadedData = wc.DownloadData(imgList.First().Attributes["src"].Value);
if (downloadedData != null)
{
//store the downloaded data in to a stream
System.IO.MemoryStream ms = new System.IO.MemoryStream(downloadedData, 0, downloadedData.Length);
//write to that stream the byte array
ms.Write(downloadedData, 0, downloadedData.Length);
//load an image from that stream.
pbImage.Image = Image.FromStream(ms);
}
}
}
}
}
使用System.Net.WebClient,将使用模板字符串中指定的url向google发送请求
添加头使请求看起来更真实。WebClient用于下载标记,该标记存储在结果中
HtmlAgilityPack.HtmlDocument创建一个文档对象,然后加载存储在结果中的数据
Linq查询获取img元素,取该列表中的第一个元素,我们下载数据并将其存储在字节数组中
有了这些数据,就创建了一个内存流,它应该封装在一个using中
将数据写入内存流,然后将该流加载到图片框图像中。请阅读。做一些研究,尝试一些东西。。。如果你陷入困境,回来提出你陷入困境的问题。在过去的两个小时里,我一直在想办法,寻求帮助没有坏处。这并不是说我刚刚意识到我需要这个,而不是试图去弄清楚,我自己立即在这里寻求帮助。谢谢你,这是一个完美的回答。虽然我有一个问题-它得到的图像是来自图像表中缩略图的图像,因此它的大小非常小,拉伸它会产生非常低质量的图像。有没有办法在按下图像后获取图像?是的,但是这样做会有问题。我之前的评论在我完成键入之前就结束了。。。但从本质上讲,这些问题是双重的,当你点击谷歌的“查看图像”按钮时,一些网站使用引用标题和其他东西来阻止你直接进入图像,而不是直接进入页面。您还需要使用表单WebBrowser执行此操作,并不断检查WebBrowser是否完成了所有需要执行的操作,然后提取较大的图像。我基本上没有语法,是的,你可以做,不,你不应该做,因为它不可靠。