Displaying an image url in c#

  • Thread starter Thread starter Kiran A K
  • Start date Start date
You can use PictureBox to show the images in Windows Forms. You can't
directly load images from URI location. You will have to download them first
using System.Net.WebRequest and System.Net.WebResponse classes, and then load
the image from the saved location and show it in PictureBox.

Following article shows some sample code on how to dowload images from URI
location:
http://www.gotdotnet.com/Community/MessageBoard/Thread.aspx?id=164268&Page=1

Once downloaded you can use following code to load the file into PictureBox:

pictureBox1.Image = System.Drawing.Image.FromFile(@"Path\YourFile.jpg");
 
Try this:

private Bitmap LoadPicture(string url)
{
HttpWebRequest wreq;
HttpWebResponse wresp;
Stream mystream;
Bitmap bmp;

bmp = null;
mystream = null;
wresp = null;
try
{
wreq = (HttpWebRequest)WebRequest.Create(url);
wreq.AllowWriteStreamBuffering = true;

wresp = (HttpWebResponse)wreq.GetResponse();

if ((mystream = wresp.GetResponseStream()) != null)
bmp = new Bitmap(mystream);
}
finally
{
if (mystream != null)
mystream.Close();

if (wresp != null)
wresp.Close();
}

return (bmp);
}

an then...
pictureDetail.Image = LoadPicture(strURL);
where pictureDetail is image control.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top