Click here to Skip to main content
Click here to Skip to main content

Streaming Web Images onto your Windows Application

, 14 Sep 2004
Rate this:
Please Sign up or sign in to vote.
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.

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

Share

About the Author

Nick Seng
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 Pinmembermapick28-Mar-12 0:58 
GeneralClosing ur stream Pinmemberworldspawn19-Mar-06 18:35 
GeneralRe: Closing ur stream PinmemberNick Seng19-Mar-06 20:04 
GeneralHelp I got a problem Pinmemberluckylaeeq5-Mar-06 22:28 
GeneralCaching PinmemberMark Focas14-Sep-04 14:11 
GeneralRe: Caching PinmemberNick Seng14-Sep-04 16:06 
GeneralVery nice Pinmembernotoriousvic13-Sep-04 11:43 
GeneralRe: Very nice PinmemberNick Seng13-Sep-04 15:56 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 14 Sep 2004
Article Copyright 2004 by Nick Seng
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid