locked
the error "Value does not fall within the expected range." can be found in wm 6 RRS feed

  • Question

  •   public Image GetMap(string url)
            {
                try
                {
                    WebRequest request = WebRequest.Create(url);
                    request.Credentials = CredentialCache.DefaultCredentials;
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream dataStream = response.GetResponseStream();
                    Bitmap bmp = new Bitmap(dataStream);
                    dataStream.Close();
                    response.Close();
                    return bmp;
                }
                catch (Exception ex) { throw ex; }


    the code is above
    when the picture is small, it is right.
    but when larger, the error can be found that Value does not fall within the expected range.
    it is only found in  windows mobile(i run the code in windows forum with the same url,and there is no problem.)
    what is wrong?
    thanks.
    edwin.dai
    Wednesday, March 11, 2009 2:55 PM

Answers

  • Hi edwin1986,

    I hope you are getting this error in Emulator, some time its return if you hard reset Emulator then might be do your code work. apart from that you need to add your code one more property like.

    request.Timeout = 120000;

    Try above code snippets in your code.

    Instant of that If you get error then try to download image with specific size of chunk by chunk.

    Hope this may be help.

    Pavan Pareta
    • Marked as answer by warrentang Tuesday, March 17, 2009 3:55 AM
    Thursday, March 12, 2009 6:51 AM
  • If the image is large, I am wondering if you are not getting all the info.  In between your lines:

                   Stream dataStream = response.GetResponseStream();
                   Bitmap bmp = new Bitmap(dataStream);

    try inserting code to read the data into a buffer...you may need to make multiple reads to get all the data.  Then once you are sure you have all the data, you can try to create the bmp.

                   Stream dataStream = response.GetResponseStream();

                    // Read data into buffer.
                    while (dataAvailable)
                    {
                          read data into buffer;
                    }

                   Bitmap bmp = new Bitmap();

    While this may not be the problem, it should at least eliminate one possible cause.


    • Marked as answer by warrentang Tuesday, March 17, 2009 3:55 AM
    Friday, March 13, 2009 4:45 PM

All replies

  • Hi edwin1986,

    I hope you are getting this error in Emulator, some time its return if you hard reset Emulator then might be do your code work. apart from that you need to add your code one more property like.

    request.Timeout = 120000;

    Try above code snippets in your code.

    Instant of that If you get error then try to download image with specific size of chunk by chunk.

    Hope this may be help.

    Pavan Pareta
    • Marked as answer by warrentang Tuesday, March 17, 2009 3:55 AM
    Thursday, March 12, 2009 6:51 AM
  • If the image is large, I am wondering if you are not getting all the info.  In between your lines:

                   Stream dataStream = response.GetResponseStream();
                   Bitmap bmp = new Bitmap(dataStream);

    try inserting code to read the data into a buffer...you may need to make multiple reads to get all the data.  Then once you are sure you have all the data, you can try to create the bmp.

                   Stream dataStream = response.GetResponseStream();

                    // Read data into buffer.
                    while (dataAvailable)
                    {
                          read data into buffer;
                    }

                   Bitmap bmp = new Bitmap();

    While this may not be the problem, it should at least eliminate one possible cause.


    • Marked as answer by warrentang Tuesday, March 17, 2009 3:55 AM
    Friday, March 13, 2009 4:45 PM