[Gtk-sharp-list] Getting an image from the web

Lee Willis lee@leewillis.co.uk
Wed, 11 Feb 2004 08:32:09 +0000

On Wed, 2004-02-11 at 01:20, Ben Maurer wrote:
> Actually, the best way to do it is via the System.Net namespace. You can
> make an http request to get an image from the url that you have. Then,
> you just plug the bytes you get back into gtk however you need to (there
> is probably some way to do that, depends on exactly what api you want to
> use)

There's code in Muine (A fantastic music player written in c# -
http://people.nl.linux.org/~jorn/Muine/) that retrieves the contents of
a URL and turns it into a GdkPixbuf - would that be useful?

Pixbuf cover;
/* read the cover image */
HttpWebRequest req = (HttpWebRequest) WebRequest.Create (album_url);
req.KeepAlive = false;
req.Timeout = 30000; /* Timeout after 30 seconds */	
WebResponse resp = null;
resp = req.GetResponse ();
Stream s = resp.GetResponseStream ();
cover = new Pixbuf (s);
resp.Close ();

The above code can throw a number of exceptions which you'll have to
deal with. They aren't included in this snippet since in Muine the
desireable behaviour is to let them bubble up to the calling function

Hope that helps