Image from URL

Loading an Image from a URL into a PictureBox

Page 1 of 1

2 Replies - 22501 Views - Last Post: 07 February 2009 - 05:18 PM Rate Topic: -----

#1 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Image from URL

Posted 07 February 2009 - 02:17 PM

I am trying to get the CAPTCHA Image from Digg for the Submissions, but for some reason it isn't loading. I know that you can use the Load(), LoadAsync(), and ImageLocation to open an Image from a URL, but for some reason it isn't working.

I have tried a couple ways to get the Image, but none of them seem to work.
1st Attempt:
HtmlDocument doc = webBrowser1.Document;
HtmlElement title = doc.GetElementById("title");
HtmlElement body = doc.GetElementById("body");
HtmlElement topic = doc.GetElementById("topic-504");
HtmlElement captcha = doc.GetElementsByTagName("img")[2];
HtmlElement submit = doc.GetElementById("submit_button");
title.SetAttribute("value", textBox3.Text);
body.SetAttribute("value", textBox4.Text);
topic.InvokeMember("click");
pictureBox1.Load("http://digg.com" + captcha.GetAttribute("src"));
pictureBox1.ImageLocation = "http://digg.com" + captcha.GetAttribute("src");



2nd Attempt:
HtmlDocument doc = webBrowser1.Document;
HtmlElement title = doc.GetElementById("title");
HtmlElement body = doc.GetElementById("body");
HtmlElement topic = doc.GetElementById("topic-504");
HtmlElement captcha = doc.GetElementsByTagName("img")[2];
HtmlElement submit = doc.GetElementById("submit_button");
title.SetAttribute("value", textBox3.Text);
body.SetAttribute("value", textBox4.Text);
topic.InvokeMember("click");
String source = webBrowser1.DocumentText;
int start = source.IndexOf("<img src=\"/captcha/");
source = source.Remove(0, start);
int stop = source.IndexOf("/>");
source = source.Remove(stop.ToString().Length);
int begin = source.IndexOf("<img src=\"");
source = source.Remove(0, begin.ToString().Length);
int end = source.IndexOf("\"");
source = source.Remove(end);
pictureBox1.Load(source);



Any Ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: Image from URL

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Image from URL

Posted 07 February 2009 - 03:15 PM

If this is a Windows application you can use the HttpWebRequest and HttpWebResponse class like this

public void LoadImageFromUrl(ref string url, PictureBox pb)
{
	HttpWebRequest request = (HttpWebRequest)Net.HttpWebRequest.Create(url);
	HttpWebResponse response = (HttpWebResponse)request.GetResponse;
	Image img = Image.FromStream(response.GetResponseStream());
	response.Close();
	pb.SizeMode = PictureBoxSizeMode.StretchImage;
	pb.Image = img;
}


Was This Post Helpful? 1
  • +
  • -

#3 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: Image from URL

Posted 07 February 2009 - 05:18 PM

Thanks

This post has been edited by gbertoli3: 07 February 2009 - 08:46 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1