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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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
BTosonCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.