예제 #1
0
        private void DownloadIcons(GoogleImageItems google_images)
        {
            for (int c = 0; c < imageGrid.Length; c++)
            {
                downloader.DownloadFile(
                    google_images.items[c].image.thumbnailLink,
                    string.Format(@"thumb{0}.jpg", c));
            }

            return;
        }
예제 #2
0
        // Function is called upon button press and searches for 10 images related to the title and bolded text in the description
        private async void SearchForImages(object sender, EventArgs e)
        {
            try
            {
                selected = new List <int>();
                foreach (var image in imageGrid)
                {
                    image.BackColor = SystemColors.MenuBar;
                }

                string query = textBox1.Text;

                // Logic for finding bolded words
                List <int> spaces = new List <int>()
                {
                    0
                };
                char[] space = new char[] { ' ', ',', '.' };
                int    i;

                //Loop through and find all the spaces in text
                while ((i = richTextBox1.Find(space, spaces.Last() + 1)) != -1)
                {
                    spaces.Add(i);
                }
                spaces[0] = -1;
                spaces.Add(richTextBox1.TextLength);

                // Check each area between spaces and see if it's bolded. If so add that to the query terms
                for (i = 0; i < spaces.Count - 1; i++)
                {
                    // ignore bolded spaces :)
                    if (spaces[i] == spaces[i + 1] - 1)
                    {
                        continue;
                    }

                    richTextBox1.Select(spaces[i] + 1, spaces[i + 1] - spaces[i] - 1);
                    if (richTextBox1.SelectionFont.Bold)
                    {
                        query += " " + richTextBox1.SelectedText;
                    }
                }

                // Make query uri friendly
                query = query.Replace(' ', '%');

                //ignore search if nothing is being searched
                if (query == "")
                {
                    return;
                }

                // Search online for keywords
                images = await Search(query);

                // if no results are found show a blank icon
                if (images.items == null)
                {
                    for (int c = 0; c < imageGrid.Length; c++)
                    {
                        using (var tempBitmap = new Bitmap("blank_image.jpg"))
                        {
                            imageGrid[c].BackgroundImage = new Bitmap(tempBitmap);
                        }
                    }
                }
                DownloadIcons(images);

                // Display the thumbnail images
                for (int c = 0; c < imageGrid.Length; c++)
                {
                    using (var tempBitmap = new Bitmap(string.Format("thumb{0}.jpg", c)))
                    {
                        imageGrid[c].BackgroundImage = new Bitmap(tempBitmap);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }