private void BackgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { int ndx = data.IndexOf("\"ou\"", StringComparison.Ordinal); while (ndx >= 0) { ndx = data.IndexOf("\"", ndx + 4, StringComparison.Ordinal); ndx++; int ndx2 = data.IndexOf("\"", ndx, StringComparison.Ordinal); string urlUn = data.Substring(ndx, ndx2 - ndx); ndx = data.IndexOf("\"ou\"", ndx2, StringComparison.Ordinal); try { var webClient = new WebClientMia(); webClient.Headers.Add("User-Agent: Other"); webClient.Timeout = 10000; byte[] imageBytes = webClient.DownloadData(urlUn); webClient.Dispose(); if (imageBytes != null) { using (var ms = new MemoryStream(imageBytes)) { Console.WriteLine(urlUn); bmp = new Bitmap(ms); if (ms != null) { imageList1.Images.Add(urlUn, bmp); lvImagenes.Items.Add(urlUn, "", i); i++; } } } } catch (Exception we) { Console.WriteLine("Error en imagen # " + i + ", " + we.ToString()); splash.Hide(); } if (i == 6) { break; } } lblCantidadImagenes.Text = lvImagenes.Items.Count.ToString(); lblInfo.Visible = false; timer1.Stop(); timer1.Dispose(); splash.Hide(); }
private string GetHtmlCode() { try { string url = "https://www.google.com/search?q=" + elemento + "&tbm=isch"; var webClient = new WebClientMia(); webClient.Headers.Add("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"); webClient.Timeout = 10 * 60 * 1000; data = webClient.DownloadString(url); webClient.Dispose(); } catch (Exception ex) { MessageBox.Show("Revisa tu conexión a internet, " + ex.Message); } return(data); }
public static Bitmap GetImageFromUrl(string url) { try { var webClient = new WebClientMia(); webClient.Headers.Add("User-Agent: Other"); webClient.Timeout = 4000; byte[] imageBytes = webClient.DownloadData(url); webClient.Dispose(); using (var ms = new MemoryStream(imageBytes)) { return(new Bitmap(ms)); } } catch (WebException we) { Console.WriteLine(we.ToString()); } return(null); }