Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. geting image from http

geting image from http

Scheduled Pinned Locked Moved C#
helptutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Seishin
    wrote on last edited by
    #1

    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!!!

    S S 2 Replies Last reply
    0
    • S Seishin

      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!!!

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      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

      www.troschuetz.de

      S 1 Reply Last reply
      0
      • S Stefan Troschuetz

        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

        www.troschuetz.de

        S Offline
        S Offline
        Seishin
        wrote on last edited by
        #3

        thanks!!! i also had to switch hwr.Method form "POST" to "GET"..

        life is study!!!

        1 Reply Last reply
        0
        • S Seishin

          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!!!

          S Offline
          S Offline
          Sascha Andres
          wrote on last edited by
          #4

          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

          -- http://www.livingit.de http://www.not2long.net

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups