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. Downloading an image from web

Downloading an image from web

Scheduled Pinned Locked Moved C#
helpquestion
8 Posts 7 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.
  • M Offline
    M Offline
    Muammar
    wrote on last edited by
    #1

    Hi, Can you kindly shed some light on what's wrong with the below code? Many thanks,

        private Image DownloadImage(string \_URL)
        {            
            try 
            {
                WebClient client = new WebClient();
                Stream stream = client.OpenRead(\_URL); //The problem seems to be here
                Image img = Image.FromStream(stream);
                stream.Flush();
                stream.Close();
                return img;
            }
            catch (Exception e) 
            {
                MessageBox.Show(e.Message);
                return null;
            }
            
        }
    
    L A L V 4 Replies Last reply
    0
    • M Muammar

      Hi, Can you kindly shed some light on what's wrong with the below code? Many thanks,

          private Image DownloadImage(string \_URL)
          {            
              try 
              {
                  WebClient client = new WebClient();
                  Stream stream = client.OpenRead(\_URL); //The problem seems to be here
                  Image img = Image.FromStream(stream);
                  stream.Flush();
                  stream.Close();
                  return img;
              }
              catch (Exception e) 
              {
                  MessageBox.Show(e.Message);
                  return null;
              }
              
          }
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Muammar© wrote:

      The problem seems to be here

      what problem?

      Muammar© wrote:

      The problem seems to be here

      looking at the full exception, not just its messge, would tell you exactly where it happens :(

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      1 Reply Last reply
      0
      • M Muammar

        Hi, Can you kindly shed some light on what's wrong with the below code? Many thanks,

            private Image DownloadImage(string \_URL)
            {            
                try 
                {
                    WebClient client = new WebClient();
                    Stream stream = client.OpenRead(\_URL); //The problem seems to be here
                    Image img = Image.FromStream(stream);
                    stream.Flush();
                    stream.Close();
                    return img;
                }
                catch (Exception e) 
                {
                    MessageBox.Show(e.Message);
                    return null;
                }
                
            }
        
        A Offline
        A Offline
        Abhinav S
        wrote on last edited by
        #3

        Too little information - however, if you are getting a WebException, either your URL is not valid or there is some error while downloading data. Check that the URL you are passing is correct.

        J 1 Reply Last reply
        0
        • A Abhinav S

          Too little information - however, if you are getting a WebException, either your URL is not valid or there is some error while downloading data. Check that the URL you are passing is correct.

          J Offline
          J Offline
          Jack Li
          wrote on last edited by
          #4

          MessageBox.Show(e.Message); what's message? if the _url is valid, open(_url)'ll back e.Message

          1 Reply Last reply
          0
          • M Muammar

            Hi, Can you kindly shed some light on what's wrong with the below code? Many thanks,

                private Image DownloadImage(string \_URL)
                {            
                    try 
                    {
                        WebClient client = new WebClient();
                        Stream stream = client.OpenRead(\_URL); //The problem seems to be here
                        Image img = Image.FromStream(stream);
                        stream.Flush();
                        stream.Close();
                        return img;
                    }
                    catch (Exception e) 
                    {
                        MessageBox.Show(e.Message);
                        return null;
                    }
                    
                }
            
            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            private void button1_Click(object sender, EventArgs e) { WebClient wc = new WebClient(); wc.DownloadFileAsync(new Uri("http://www.xxxx.com/xx.gif"), "D:\\xx.gif"); wc.DownloadFileCompleted+=new AsyncCompletedEventHandler(wc_DownloadFileCompleted); } private void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { MessageBox.Show("完成!"); }

            M P 2 Replies Last reply
            0
            • M Muammar

              Hi, Can you kindly shed some light on what's wrong with the below code? Many thanks,

                  private Image DownloadImage(string \_URL)
                  {            
                      try 
                      {
                          WebClient client = new WebClient();
                          Stream stream = client.OpenRead(\_URL); //The problem seems to be here
                          Image img = Image.FromStream(stream);
                          stream.Flush();
                          stream.Close();
                          return img;
                      }
                      catch (Exception e) 
                      {
                          MessageBox.Show(e.Message);
                          return null;
                      }
                      
                  }
              
              V Offline
              V Offline
              V 0
              wrote on last edited by
              #6

              Did you check the OpenRead explanation on MSDN[^]? ArgumentNullException The address parameter is Nothing. WebException The URI formed by combining BaseAddress, address is invalid. -or- An error occurred while downloading data.

              V.

              1 Reply Last reply
              0
              • L Lost User

                private void button1_Click(object sender, EventArgs e) { WebClient wc = new WebClient(); wc.DownloadFileAsync(new Uri("http://www.xxxx.com/xx.gif"), "D:\\xx.gif"); wc.DownloadFileCompleted+=new AsyncCompletedEventHandler(wc_DownloadFileCompleted); } private void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { MessageBox.Show("完成!"); }

                M Offline
                M Offline
                Muammar
                wrote on last edited by
                #7

                Works like a charm :) Thank you!

                1 Reply Last reply
                0
                • L Lost User

                  private void button1_Click(object sender, EventArgs e) { WebClient wc = new WebClient(); wc.DownloadFileAsync(new Uri("http://www.xxxx.com/xx.gif"), "D:\\xx.gif"); wc.DownloadFileCompleted+=new AsyncCompletedEventHandler(wc_DownloadFileCompleted); } private void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { MessageBox.Show("完成!"); }

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  Ahem. You forgot to reallocate the reference to your event handler.

                  *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                  "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                  My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                  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