c pound

I reject your reality and substitute my own!
posts - 46, comments - 37, trackbacks - 40

My Links

News

Archives

Image Galleries

Blog Communities

Blog is a stupid word

Lunch Hour

Resources

On the acquisition of images

So, one of my little side projects involves getting images from websites. Surprisingly, one of the easiest parts is actually requesting and receiving the Image. (note: exception handling code removed for your viewing pleasure.)

private Image GetImage(string url)
{
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

    Image image = Image.FromStream(resp.GetResponseStream());

    resp.Close();

    return image;
}

Now, you can just as easily get the source of a web-page:

private string GetText(string url)
{
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

    StreamReader sr = new StreamReader(resp.GetResponseStream());
    string text = sr.ReadToEnd();

    resp.Close();

    return
text;
}

Brilliant! Of course, getting exceptionally large images (or files of any kind) in this manner should be done asynchronously by utilizing BeginGetResponse and EndGetResponse instead of just GetResponse. You could also use a Thread to make these kinds of operations asynchronous.

Print | posted on Monday, October 10, 2005 10:50 AM |

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: