web client to download image to picture box control

I need to code a simple web client so that I can give it a url to a image on the internet then have it download it and display it in a picture box control.   How do I go about doing this?  Sample code... general ideas.

Thanks for your help
ccole6Asked:
Who is Participating?
 
BTosonConnect With a Mentor Commented:
Homer Simpson moment!  Just noticed that I had doubled up the write to file line. Apologies!
private void button1_Click(object sender, EventArgs e)
{
	 WebRequest request = HttpWebRequest.Create("http://upload.wikimedia.org/wikipedia/commons/b/b3/Billard_p1150796.jpg");
	 request.Timeout = 1000;  //Timeout in milliseconds
	 try
	 {
			HttpWebResponse response = (HttpWebResponse)request.GetResponse();
			if (response.StatusCode == HttpStatusCode.OK && response.ContentType.StartsWith("image/"))
			{
				 Stream responsestream = response.GetResponseStream();
				 File.WriteAllBytes("Billard_p1150796.jpg", new BinaryReader(response.GetResponseStream()).ReadBytes(Convert.ToInt32(response.ContentLength)));
				 pictureBox1.ImageLocation = "Billard_p1150796.jpg";
			}
			else
			{
				 //Not an image or status is not 200
				 MessageBox.Show(response.StatusDescription);
			}
	 }
	 catch (Exception ex)
	 {
			//Timeout or other error
			MessageBox.Show(ex.Message);
	 }
	}

Open in new window

0
 
BTosonCommented:
Do you need to store he image locally first?  Otherwise you could just do the following:
pictureBox1.ImageLocation = url;

Open in new window

0
 
ccole6Author Commented:
YES I need to store it locally first. Also,  I need a webclient with a GOOD way to deal with timeout issues without locking it up.  For example, if image does not finish downloading in x seconds stop and then try the download again or something like that.

Thanks for your help
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
BTosonCommented:
How about something like this?
WebRequest request = HttpWebRequest.Create("http://url/image.gif");
request.Timeout = 1000;  //Timeout in milliseconds
try
{
	var response = (HttpWebResponse)request.GetResponse();
	if (response.StatusCode == HttpStatusCode.OK && response.ContentType.StartsWith("image/"))
	{
		var responsestream = response.GetResponseStream();
		File.WriteAllBytes("image.gif", new BinaryReader(response.GetResponseStream()).ReadBytes(Convert.ToInt32(response.ContentLength)));
		pictureBox1.ImageLocation = "image.gif";
	} else {
		//Not an image or status is not 200
		MessageBox.Show(ex.Message);
	}
}catch( Exception ex){
	//Timeout or other error
}

Open in new window

0
 
ccole6Author Commented:
ok I see where you are going but there are a few syntax issues that I do not get.  Please help me out.

Thanks again!
Error	1	The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)	C:\Documents and Settings\Joseph\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.cs	31	17	WindowsApplication1
Error	2	The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)	C:\Documents and Settings\Joseph\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.cs	34	21	WindowsApplication1
Error	3	The name 'ex' does not exist in the current context	C:\Documents and Settings\Joseph\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.cs	41	37	WindowsApplication1
 
 
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
 
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            WebRequest request = HttpWebRequest.Create("http://en.wikipedia.org/wiki/Image:Billard_p1150796.jpg");
            request.Timeout = 1000;  //Timeout in milliseconds
            try
            {
                var response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK && response.ContentType.StartsWith("image/"))
                {
                    var responsestream = response.GetResponseStream();
                    File.WriteAllBytes("Billard_p1150796.jpg", new BinaryReader(response.GetResponseStream()).ReadBytes(Convert.ToInt32(response.ContentLength)));
                    pictureBox1.ImageLocation = "Billard_p1150796.jpg";
                }
                else
                {
                    //Not an image or status is not 200
                    MessageBox.Show(ex.Message);
                }
            }
            catch (Exception ex)
            {
                //Timeout or other error
            }
        }
 
 
    }
}

Open in new window

0
 
BTosonCommented:
Apologies - I pasted the messagebox section in the wrong place!
Please see amended code below...

(By the way, the URL to your image was wrong)
private void button1_Click(object sender, EventArgs e)
{
WebRequest request = HttpWebRequest.Create("http://upload.wikimedia.org/wikipedia/commons/b/b3/Billard_p1150796.jpg");
request.Timeout = 1000;  //Timeout in milliseconds
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK && response.ContentType.StartsWith("image/"))
{
Stream responsestream = response.GetResponseStream();
File.WriteAllBytes("Billard_p1150796.jpg", new BinaryReader(response.GetResponseStream()).ReadBytes(Convert.ToInt32(response.ContentLength)));																   File.WriteAllBytes("Billard_p1150796.jpg", new BinaryReader(response.GetResponseStream()).ReadBytes(Convert.ToInt32(response.ContentLength)));
pictureBox1.ImageLocation = "Billard_p1150796.jpg";
}
else
{
//Not an image or status is not 200
}
}
catch (Exception ex)
{
//Timeout or other error
MessageBox.Show(ex.Message);
}
}

Open in new window

0
 
ccole6Author Commented:
ok it works and I see how it functions... however I keep on getting an x when I try to download a jpg image if I download a gif image I do not though.  Why is this?
0
 
ccole6Author Commented:
thanks for the help man! I have been wondering how to do this for a while... I might have more questions in this general area again soon!
0
All Courses

From novice to tech pro — start learning today.