none
Can't download image from web RRS feed

  • Question

  • hello,

    please help me.

    i'm tryin gto download an image from a website. I tried a lot of methods and always I can download the image ti my computer, but when I try to open the image i get the folllowing error: "Can't open this icture because the file appears to damaged, corrupted or is too large"

    I used a code that I used other times and allways worked fine, I don't know what happens.

    this is the link to the image:

    http://www.orange-book.com/detail/obwfk53010/contents_images/article_cd:MS2MSD0330                    2080/is_not_main:1/imageno:2

                                               

     using (System.Net.WebClient client = new System.Net.WebClient())
                        {

                            client.DownloadFile(new Uri("http://www.orange-book.com/detail/obwfk53010/contents_images/article_cd:MS2MSD0330%202080/is_not_main:1/imageno:2"), @"c:\temp\image251.jpg");

                       }

    (OR)

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.orange-book.com/detail/obwfk53010/contents_images/article_cd:MS2MSD0330                    2080/is_not_main:1/imageno:2");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                if ((response.StatusCode == HttpStatusCode.OK ||
                    response.StatusCode == HttpStatusCode.Moved ||
                    response.StatusCode == HttpStatusCode.Redirect)) 
                {
                  
                    using (Stream inputStream = response.GetResponseStream())
                    using (Stream outputStream = File.OpenWrite(@"c:\temp\image15.png"))
                    {
                        byte[] buffer = new byte[4096];
                        int bytesRead;
                        do
                        {
                            bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                            outputStream.Write(buffer, 0, bytesRead);
                        } while (bytesRead != 0);
                    }
                }



    • Edited by Mie Mie Thursday, October 26, 2017 3:06 AM
    Thursday, October 26, 2017 3:01 AM

All replies

    • Proposed as answer by Ashidacchi Thursday, October 26, 2017 6:13 AM
    Thursday, October 26, 2017 5:59 AM
  • So thanks , Castorix31

    I try with this link,

    But the same error.
    Why?, I don't know.

    Thursday, October 26, 2017 8:00 AM
  • Now, your image doesn't exist anymore...
    Thursday, October 26, 2017 8:20 AM
  • Hi Mie Mie,

    Thank you for posting here.

    For your question, I tried many ways to download the image.

    I use the following code to get the image url from img src of the website.

                var document = new HtmlWeb().Load("http://www.orange-book.com/detail/obwfk53010/contents_images/article_cd:MS2MSD0330%202080/is_not_main:1/imageno:2");
                var urls = document.DocumentNode.Descendants("img")
                                                .Select(e => e.GetAttributeValue("src", null))
                                                .Where(s => !String.IsNullOrEmpty(s));
    
                List<string> list = urls.ToList();

    But when I try to download the image, I get the same error as yours. We could not open the image. I check the herder of the image, the data is not compressed.

      using (WebClient client = new WebClient())
                {
                    client.DownloadFile(list[2], "image35.jpg");
                }

    After that, I try to check the url which get from the <img src="" >. I find that if we open the url in the same browser which link open the website below, we could open the image in the website. I check the data it received from cache.

    This is one of the url get from <img src="">.

    http://image.trusco-sterra.com/img/daihyou_large/MS2MSD0330_2080__DAL.jpg

    This is the link which open the website.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/02702fd8-5e57-4a47-8249-74a78e1a3440/cant-download-image-from-web?forum=csharpgeneral&prof=required

    But when we open the image via url in different browser without open the link, we could not get the image.

    The website will show that a restriction is made by referring directly to an image from an external site.

    Our goal is to provide a comfortable and useful service for everyone on this website.
    Therefore, a restriction is made by referring directly to an image from an external site.
    
    In addition, depending on your environment and software,
    Images may not be displayed.
     

    Hence, we could not download the image.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, October 27, 2017 9:36 AM
    Moderator