Connecting Tech Pros Worldwide Forums | Help | Site Map

Displaying an image url in c#

Kiran A K
Guest
 
Posts: n/a
#1: Mar 22 '06
Hi,

I have an image url (eg:http://myimages.com/pic1.jpg)
Now i want to display the above image in a C# windows form.
Which control should i use?
i am using VS .NET 2003.

Pls provide some example code.

Regards,
Kiran

yogeshprabhu
Guest
 
Posts: n/a
#2: Mar 22 '06

re: Displaying an image url in c#


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/M...=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" );


Marian
Guest
 
Posts: n/a
#3: Mar 22 '06

re: Displaying an image url in c#


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.

Peter Rilling
Guest
 
Posts: n/a
#4: Mar 22 '06

re: Displaying an image url in c#


The PictureBox in 2.0 supports loading from a URL.

"yogeshprabhu" <yogeshprabhu@discussions.microsoft.com> wrote in message
news:6923EF56-E1E3-4346-9963-D88AD707CE90@microsoft.com...[color=blue]
> 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/M...=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" );
>
>[/color]


Closed Thread