You probably don't need a random search string. You probably need random themes. Here is some code to help you. First create a list of topics:
private readonly List<string> _topics = new List<string> {"dog", "car", "truck", "cat", "florida"};
Of course, you can change, add and delete as many as you want. Then we will create a function to extract the HTML code from the Google Image Search, randomly choosing one of our themes to search:
private string GetHtmlCode() { var rnd = new Random(); int topic = rnd.Next(0, _topics.Count - 1); string url = "https://www.google.com/search?q=" + _topics[topic] + "&tbm=isch"; string data = ""; var request = (HttpWebRequest)WebRequest.Create(url); var response = (HttpWebResponse)request.GetResponse(); using (Stream dataStream = response.GetResponseStream()) { if (dataStream == null) return ""; using (var sr = new StreamReader(dataStream)) { data = sr.ReadToEnd(); } } return data; }
Once we have the HTML code, we need to images_table img tags located below the images_table and save the URL of the images in a list:
private List<string> GetUrls(string html) { var urls = new List<string>(); int ndx = html.IndexOf("class=\"images_table\"", StringComparison.Ordinal); ndx = html.IndexOf("<img", ndx, StringComparison.Ordinal); while (ndx >= 0) { ndx = html.IndexOf("src=\"", ndx, StringComparison.Ordinal); ndx = ndx + 5; int ndx2 = html.IndexOf("\"", ndx, StringComparison.Ordinal); string url = html.Substring(ndx, ndx2 - ndx); urls.Add(url); ndx = html.IndexOf("<img", ndx, StringComparison.Ordinal); } return urls; }
Our last function we need is to take the URL and load the image bytes into an array of bytes:
private byte[] GetImage(string url) { var request = (HttpWebRequest)WebRequest.Create(url); var response = (HttpWebResponse)request.GetResponse(); using (Stream dataStream = response.GetResponseStream()) { if (dataStream == null) return null; using (var sr = new BinaryReader(dataStream)) { byte[] bytes = sr.ReadBytes(100000); return bytes; } } return null; }
Finally, we just need to tie it all together:
string html = GetHtmlCode(); List<string> urls = GetUrls(html); var rnd = new Random(); int randomUrl = rnd.Next(0, urls.Count - 1); string luckyUrl = urls[randomUrl]; byte[] image = GetImage(luckyUrl); using (var ms = new MemoryStream(image)) { pictureBox1.Image = Image.FromStream(ms); }
UPDATE
I had a few requests for this answer asking me to change it so that it loads the actual full-size image, and not a thumbnail. I changed my source code to now download full size images instead of thumbnails.
First, as before, create a list of topics:
private readonly List<string> _topics = new List<string> { "dog", "car", "truck", "cat", "florida" };
Of course, you can change, add and delete as many as you like. Then we will create a function to extract the HTML code from the Google Image Search by randomly choosing one of our themes to search. GetHtmlCode() here differs from GetHtmlCode() in the miniature version in that we must add the Accept request and UserAgent to the request, otherwise Google will not give us the full-size URL:
private string GetHtmlCode() { var rnd = new Random(); int topic = rnd.Next(0, _topics.Count - 1); string url = "https://www.google.com/search?q=" + _topics[topic] + "&tbm=isch"; string data = ""; var request = (HttpWebRequest)WebRequest.Create(url); request.Accept = "text/html, application/xhtml+xml, */*"; request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"; var response = (HttpWebResponse)request.GetResponse(); using (Stream dataStream = response.GetResponseStream()) { if (dataStream == null) return ""; using (var sr = new StreamReader(dataStream)) { data = sr.ReadToEnd(); } } return data; }
The GetUrls() method GetUrls() also been rewritten because the HTML that we get back is different from the HTML that we returned in our thumbnail version. It still parses the urls from the HTML code:
private List<string> GetUrls(string html) { var urls = new List<string>(); int ndx = html.IndexOf("\"ou\"", StringComparison.Ordinal); while (ndx >= 0) { ndx = html.IndexOf("\"", ndx + 4, StringComparison.Ordinal); ndx++; int ndx2 = html.IndexOf("\"", ndx, StringComparison.Ordinal); string url = html.Substring(ndx, ndx2 - ndx); urls.Add(url); ndx = html.IndexOf("\"ou\"", ndx2, StringComparison.Ordinal); } return urls; }
Our last function is to take the URL and load the image bytes into an array of bytes. There is only one minor change in this function, different from the thumbnail version. We had to change the number in ReadBytes() , since now our images will be larger:
private byte[] GetImage(string url) { var request = (HttpWebRequest)WebRequest.Create(url); var response = (HttpWebResponse)request.GetResponse(); using (Stream dataStream = response.GetResponseStream()) { if (dataStream == null) return null; using (var sr = new BinaryReader(dataStream)) { byte[] bytes = sr.ReadBytes(100000000); return bytes; } } return null; }
Finally, we just need to tie it all together, as before:
string html = GetHtmlCode(); List<string> urls = GetUrls(html); var rnd = new Random(); int randomUrl = rnd.Next(0, urls.Count - 1); string luckyUrl = urls[randomUrl]; byte[] image = GetImage(luckyUrl); using (var ms = new MemoryStream(image)) { pictureBox1.Image = Image.FromStream(ms); }