-1

I'm trying to make a Google Images search in windows forms, and my code is returning null even though if I manually search the xPath it takes me to the images. I'm rather new to API's and NuGet packs so go easy on me LOL! The error specifically is 'Object reference not set to an instance of an object. But when I search the xPath it shows 48 results. Here is my code

private void searchButton_Click(object sender, EventArgs e)
    {
        string ImageQuery = titleTextBox.Text;
        var htmlDocument = new HtmlWeb().Load("https://www.google.com/search?q=+" + ImageQuery + "&tbm=isch");
        var imageNode = htmlDocument.DocumentNode.SelectSingleNode("//div[@class='bRMDJf islir']/img");
        string imagePath = imageNode.Attributes["src"].Value;

        var imageStream = HttpWebRequest.Create(imagePath).GetResponse().GetResponseStream();
        this.optionOnePicture.Image = Image.FromStream(imageStream);
    }
TheBluePrint
  • 13
  • 1
  • 6
  • Just checked Google and from what I can see the src contains the image itself in base64 format. What you need to do is convert that into image – Tom Jul 07 '21 at 22:51
  • What you're doing is not querying an API; you are scraping HTML content, which, setting aside whether or not that's legal, is going to give you massive headaches since 1) they probably made it pretty hard to do, 2) any time they change how the frontend renders your functionality will break. – Lawrence Johnson Jul 07 '21 at 23:00
  • 1
    Duplicate: [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Jul 07 '21 at 23:41
  • should be 2 questions: 1. How do I get images from google 2. display images into a pictureBox? – Lei Yang Jul 08 '21 at 02:28
  • @TheBluePrint, Is any update? Please check if my answer works for you. – Jack J Jun Jul 12 '21 at 01:52
  • @JackJJun-MSFT it works for me but I've decided to try and query an API instead, but its also giving me some trouble. – TheBluePrint Jul 12 '21 at 01:58
  • @TheBluePrint, so now have you given up the initial method to show the message in the picturebox? If you don't mind, you could accept it as answer and ask a new question about your API method. It will also help others to solve the similar issue. – Jack J Jun Jul 12 '21 at 02:03

1 Answers1

0

You could try the following code to get images from google and show it in the picturebox.

 private void button1_Click(object sender, EventArgs e)
        {
            string ImageQuery = textBox1.Text;
            var htmlDocument = new HtmlWeb().Load("https://www.google.com/search?q=+" + ImageQuery + "&tbm=isch");
            var list=GetImagesInHTMLString(htmlDocument.Text);
            var str = list[0];
            string pattern = @"(https://.*);";
            Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
            Match matches = rgx.Match(str);
            string path = matches.Groups[1].Value;
            var imageStream = HttpWebRequest.Create(path).GetResponse().GetResponseStream();
            this.pictureBox1.Image = Image.FromStream(imageStream);
        }

        private List<string> GetImagesInHTMLString(string htmlString)
        {
            List<string> images = new List<string>();
            string pattern = @"<(img)\b[^>]*>";
            Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
            MatchCollection matches = rgx.Matches(htmlString);

            for (int i = 0, l = matches.Count; i < l; i++)
            {
                images.Add(matches[i].Value);
            }
            return images;
        }

In order to test, I selected the first picture after the search.

Result:

enter image description here

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27