Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Article

Streaming Web Images onto your Windows Application

Rate me:
Please Sign up or sign in to vote.
4.66/5 (15 votes)
14 Sep 20042 min read 76.2K   2.1K   42   10
How to display images from the web on your Windows application.

Sample Image - webimagestream.jpg

Introduction

This idea came about when I was writing an application for a client, in which the client insisted that the images not be stored in the database, but in a web repository. Although I initially resisted the idea, but the more I thought about it, the more it grew on it. For one thing, it offered an alternative to image storage to the database (which is the standard), or to the local drive (which I despise). The other advantage is that it would work really well with a web application that uses the same image.

The Application

I whipped up this simple application as a proof of the concept above. What this application does is that it streams the image given the URL, and displays it in a PictureBox control. To use the application, just unzip it and double-click the executable.

Note:

  1. The first image it attempts to stream will be a bit slow. Consequent image streams will be faster.
  2. The application assumes that you're not behind a proxy. If you are, you will have to make some minor changes to the source code, and recompile.

The Code

To stream images from the web, we're going to make use of classes from the System.Net and System.IO classes. And of course, you're gonna need an Internet access as well.

C#
private Image GetImage(string sURL)
{
    Stream str = null;
    HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(sURL);
    HttpWebResponse wRes = (HttpWebResponse)(wReq).GetResponse();
    str = wRes.GetResponseStream();

    return Image.FromStream(str);
}

The first thing that I did is to create an HttpWebRequest object from the Image URL, which is basically a class that manages the request to the URL given. If you're behind a proxy, then you will have to create a WebProxy object and assign it to wReq's Proxy property.

Calling GetResponse will get the resulting response from the request we made. What we're really interested is the data stream of the URL, which we obtain from the GetResponseStream method of the HttpWebResponse class. Once we have the stream, it's a simple matter to create an Image object from the data stream.

Possible Improvements:

  • Create a screen to specify if the web request should go through a proxy.
  • Allow the user to key in the URL, as opposed to hard-coding it.

Conclusion

Streaming images from the web onto a Windows application may not always be the ideal way to do things, but for some situations, it might actually save you some time and work. It did for me, so I hope some of you reading this might find some use out of it.

Comments and criticism are most welcome.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Malaysia Malaysia
Ahh...crap.How do you write a biography? Maybe it's easier to do a faq.Yeah that's it. The Official Nick Seng FAQ!.Updated:12/23/2003



1. Who are you
I'm Nick Seng

2. No,really, who are you?
jeez, I thought I already told you. It's Nick. You really need to pay more attention.

3. Didn't you use to go around with a different name
Who me? Nope. You must be thinking of somebody else.notorious smc

4. What do you do for a living?
I'm a programmer, though my official title is R&D Engineer. What the h*** does that mean anyway? I engineer R&D?? Who comes up with these titles?

5. What do you do in your free time?
Hah, I bet you think I'm gonna say code. Well, I don't. Not much, anyway

6. What else do you do?
Well, I play most indoor games, I like to read books/mags/comics/manga, watch the occasional anime,.....No, wait, I sound too much like a geek.....scrap that. I mean that I go clubbing, date hot supermodels, play professional sports and drive a ferarri. Yeah, that's it.



to be continued.....or not.


Comments and Discussions

 
GeneralMy vote of 5 Pin
mapick27-Mar-12 23:58
mapick27-Mar-12 23:58 
GeneralClosing ur stream Pin
worldspawn19-Mar-06 17:35
worldspawn19-Mar-06 17:35 
GeneralRe: Closing ur stream Pin
Nick Seng19-Mar-06 19:04
Nick Seng19-Mar-06 19:04 
GeneralHelp I got a problem Pin
luckylaeeq5-Mar-06 21:28
luckylaeeq5-Mar-06 21:28 
GeneralCaching Pin
Mark Focas14-Sep-04 13:11
Mark Focas14-Sep-04 13:11 
GeneralRe: Caching Pin
Nick Seng14-Sep-04 15:06
Nick Seng14-Sep-04 15:06 
GeneralVery nice Pin
notoriousvic13-Sep-04 10:43
notoriousvic13-Sep-04 10:43 
GeneralRe: Very nice Pin
Nick Seng13-Sep-04 14:56
Nick Seng13-Sep-04 14:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.