geting image from http
-
Hi! I'm trying to load an image from a web site into Image object.. i tried something like this:
HttpWebRequest hwr = HttpWebRequest.Create("http://www.onet.pl/_m/32006b80b5eb4c237bb11e1a25895e83.jpg") as HttpWebRequest; hwr.ContentType = "image/jpg"; hwr.Method = "POST"; hwr.BeginGetRequestStream(new AsyncCallback(Callback), hwr); void Callback(IAsyncResult result) { HttpWebRequest request = (HttpWebRequest)result.AsyncState; Stream stream = request.EndGetRequestStream(result); StreamReader sr = new StreamReader(stream); // <- exception thrown pictureBox1.Image = Image.FromStream(sr.BaseStream); }
but i get an exception that the stream is not readable.. i don't get it - why the EndGetRequestStream returns a stream if it's not readable.. anybody knows how to make this work or some other way to load jpgs from http? thanks for any help!!life is study!!!
-
Hi! I'm trying to load an image from a web site into Image object.. i tried something like this:
HttpWebRequest hwr = HttpWebRequest.Create("http://www.onet.pl/_m/32006b80b5eb4c237bb11e1a25895e83.jpg") as HttpWebRequest; hwr.ContentType = "image/jpg"; hwr.Method = "POST"; hwr.BeginGetRequestStream(new AsyncCallback(Callback), hwr); void Callback(IAsyncResult result) { HttpWebRequest request = (HttpWebRequest)result.AsyncState; Stream stream = request.EndGetRequestStream(result); StreamReader sr = new StreamReader(stream); // <- exception thrown pictureBox1.Image = Image.FromStream(sr.BaseStream); }
but i get an exception that the stream is not readable.. i don't get it - why the EndGetRequestStream returns a stream if it's not readable.. anybody knows how to make this work or some other way to load jpgs from http? thanks for any help!!life is study!!!
Seishin# wrote:
i don't get it - why the EndGetRequestStream returns a stream if it's not readable..
Since the request is what is sent to the web resource it is expected to be filled with information. You're interested in the response, so switch to the GetResponse or BeginGetResponse method.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Seishin# wrote:
i don't get it - why the EndGetRequestStream returns a stream if it's not readable..
Since the request is what is sent to the web resource it is expected to be filled with information. You're interested in the response, so switch to the GetResponse or BeginGetResponse method.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Hi! I'm trying to load an image from a web site into Image object.. i tried something like this:
HttpWebRequest hwr = HttpWebRequest.Create("http://www.onet.pl/_m/32006b80b5eb4c237bb11e1a25895e83.jpg") as HttpWebRequest; hwr.ContentType = "image/jpg"; hwr.Method = "POST"; hwr.BeginGetRequestStream(new AsyncCallback(Callback), hwr); void Callback(IAsyncResult result) { HttpWebRequest request = (HttpWebRequest)result.AsyncState; Stream stream = request.EndGetRequestStream(result); StreamReader sr = new StreamReader(stream); // <- exception thrown pictureBox1.Image = Image.FromStream(sr.BaseStream); }
but i get an exception that the stream is not readable.. i don't get it - why the EndGetRequestStream returns a stream if it's not readable.. anybody knows how to make this work or some other way to load jpgs from http? thanks for any help!!life is study!!!
Hi, basically getting data via Http contains two things: 1. the request 2. the response The strean object of the Request object (HttpWebRequest) in your case is to send data to the web. Getting the answer is to get the response and reading it's stream. So use something like this:
HttpWebRequest hwr = HttpWebRequest.Create("http://www.onet.pl/_m/32006b80b5eb4c237bb11e1a25895e83.jpg") as HttpWebRequest; HttpWebResponse resp = (HttpWebResponse) hwr.GetResponse(); pictureBox1.Image = Image.FromStream(resp.GetResponseStream()); resp.Close(); resp = null; hwr = null;
Don't forget to close the repsonse! If not, you might run into exceptions after getting data from the web multiple times. -sa