locked
How to Download large image from web RRS feed

  • Question

  • Hi,
    I want to download large image from web. i am using the following code for download the image from web

           
    Dim
     Req As
     Net.HttpWebRequest = Nothing
    
    Dim
     Resp As
     Net.HttpWebResponse = Nothing
    
    
    Req = Net.HttpWebRequest.Create(sWebURL)
    Req.Method = "Get"
    
    Resp = Req.GetResponse()
    Dim
     bm As
     New
     Bitmap(Resp.GetResponseStream)
    bm.Save(sImagePath, System.Drawing.Imaging.ImageFormat.Bmp)
    bm.Dispose() Resp.Close()
    but it throws the following errors "Value does not fall in expected range." this error only occur when i try to download image or large than 200 KB.
    I am using vb.net 2005 and wm 5.0.
    Please suggest how i fixed this issue.
    Thanks

    Khatri Mitesh
    Wednesday, June 3, 2009 1:13 PM

Answers

  • I tried loading the same image files you were and I ran into the same error.

    The issue is that the JPG files that you are attempting to download are GZIP encoded.  If you look at the HTTP response that comes back from the server when requesting the JPG, it has an HTTP header which is "Content-Encoding: gzip"  This of course means that the content that you requested is encoded using GZIP and needs to be decoded before it can be used.  This is why you can browse to the images using Internet Explorer and they work fine because IE knows how to deal with the "Content-Encoding: gzip" header and automagically decodes.  The fix for this is simple, just use the GZipStream after the MemoryStream is loaded with the encoded image.  You will also want to check for the GZIP encoding on the response headers so that it only decodes images that have the GZIP.  Here is the modified code that I created to make this work correctly.

            Dim response As WebResponse = WebRequest.Create("http://imagem2.buscape.com.br/thumbs/ensopado/6424/190x190_168472_1.jpg").GetResponse
            Dim bitmap As Bitmap = Nothing
            Using memStream As MemoryStream = New MemoryStream
                Using remoteStream As Stream = response.GetResponseStream
                    Dim nTotal As Integer = 0
                    Dim inBuffer As Byte() = New Byte(1024) {}
                    Dim nRead As Integer = 0
                    Do
                        nRead = remoteStream.Read(inBuffer, 0, inBuffer.Length)
                        If (nRead > 0) Then
                            memStream.Write(inBuffer, 0, nRead)
                        End If
                        nTotal = (nTotal + nRead)
                    Loop While (nRead > 0)
                End Using
                Dim strEncoding As String = response.Headers.Get("Content-Encoding")
                If (Not strEncoding Is Nothing) Then
                    If (strEncoding.ToLower = "gzip") Then
                        memStream.Position = 0
                        Using zipStream As GZipStream = New GZipStream(memStream, CompressionMode.Decompress)
                            bitmap = New Bitmap(zipStream)
                        End Using
                    End If
                Else
                    bitmap = New Bitmap(memStream)
                End If
            End Using
    

    why do you robot?
    Monday, June 8, 2009 5:01 AM

All replies

  • Hi Mitesh,

    I guess if the image file size is large then would be better to go with Chunk by Chunk downloading.
    Refer the link below

    http://social.msdn.microsoft.com/Forums/en-US/vssmartdevicesvbcs/thread/d8c48b29-ef9d-44ea-8ff4-85e98a8d42ad

    Regards,
    Amit Rote
    Wednesday, June 3, 2009 1:57 PM
  • Hi Khatri Mitesh,

    Please try to change this and let me know.

    Req.Method = "Get"

    to

    Req.Method = "Post"


    Regards,
    Malleswar

    Wednesday, June 3, 2009 2:18 PM
  • I believe that Amit is correct, you need to chunk the stream to a local stream before converting to bitmp.  Try the following code before you save the bitmap.

        Dim response As WebResponse = WebRequest.Create("http://i.msdn.microsoft.com/cc540221.scottGu_45(en-us,MSDN.10).jpg").GetResponse
        Dim myBitmap As Bitmap = Nothing
        Using memStream As MemoryStream = New MemoryStream
            Using remoteStream As Stream = response.GetResponseStream
                Dim nTotalBytes As Integer = 0
                Dim inBuffer As Byte() = New Byte(&H400  - 1) {}
                Dim nRead As Integer = 0
                Do
                    nRead = remoteStream.Read(inBuffer, 0, inBuffer.Length)
                    memStream.Write(inBuffer, 0, nRead)
                    nTotalBytes = (nTotalBytes + nRead)
                Loop While (nRead > 0)
            End Using
            myBitmap = New Bitmap(memStream)
    

    why do you robot?
    • Proposed as answer by therobotgeek Wednesday, June 3, 2009 9:30 PM
    Wednesday, June 3, 2009 9:28 PM
  • Sorry there was an end using statement missing.  Here is the updated code.

        Dim response As WebResponse = WebRequest.Create("http://i.msdn.microsoft.com/cc540221.scottGu_45(en-us,MSDN.10).jpg").GetResponse
        Dim myBitmap As Bitmap = Nothing
        Using memStream As MemoryStream = New MemoryStream
            Using remoteStream As Stream = response.GetResponseStream
                Dim nTotalBytes As Integer = 0
                Dim inBuffer As Byte() = New Byte(1024) {}
                Dim nRead As Integer = 0
                Do
                    nRead = remoteStream.Read(inBuffer, 0, inBuffer.Length)
                    memStream.Write(inBuffer, 0, nRead)
                    nTotalBytes = (nTotalBytes + nRead)
                Loop While (nRead > 0)
            End Using
            myBitmap = New Bitmap(memStream)
        End Using
    

    why do you robot?
    Wednesday, June 3, 2009 9:36 PM
  • Hi,
    Thanx a lot for reply. but when i try to save the bitmap image in my window mobile using following code :

    myBitmap.Save(sImagePath, System.Drawing.Imaging.ImageFormat.Jpeg)
    

    it throws the following error :
    Value does not fall with in expected range.
    or
    a existing connection is focibly closed by the remot host.
    so please suggest how i solve this issue.
    Thanks

    Mitesh
    Thursday, June 4, 2009 6:29 AM
  • Hi,
    When i try to initialize a bitmap from a stream it throws the error "Value does not fall with in expected range. "
    i am using following code to initialize the bitmap object:-

    downloadedImage = New System.Drawing.Bitmap(imageStream)


    Thanks
    ~Khatri Mitesh khatrimitesh@hotmail.com Software Developer Rajasthan-Inaia http://www.youtube.com/watch?v=-oJaaPCrk3Q
    Friday, June 5, 2009 10:58 AM
  • The Bitmap class constructor can throw an ArgumentException if the stream does not contain image data or if stream contains a PNG image file with a single dimension greater than 65,535 pixels.  Have you verified that you do get a valid image stream back?  Are you getting specifically an ArgumentException or is it a different exception?

    why do you robot?
    Friday, June 5, 2009 3:10 PM
  • Stream contains the .jpg image data.
    so why error are occurred.
    Thanks

    ~Khatri Mitesh
    khatrimitesh@hotmail.com
    Software Developer
    Rajasthan-India
    http://www.youtube.com/watch?v=-oJaaPCrk3Q
    Saturday, June 6, 2009 4:49 AM
  • I've tested the code by downloading a JPG so I know that it works.  Does the image have any type of encryption on it?  Can you post a URL to a specific JPG that you are trying to download?  I can test with mine to see if it works for me.  If it does work for me then you might have another issue.
    why do you robot?
    Sunday, June 7, 2009 3:34 AM
  • I am using the following links to donwload images :

     "http://imagem2.buscape.com.br/thumbs/ensopado/6424/190x190_168472_1.jpg"
     "http://imagem2.buscape.com.br/thumbs/ensopado/6424/190x190_168472_1.jpg"
     "http://imagem2.buscape.com.br/thumbs/ensopado/93/190x190_126844_1.jpg"
     "http://imagem2.buscape.com.br/thumbs/ensopado/6424/190x190_121317_1.jpg"
     "http://imagem2.buscape.com.br/thumbs/ensopado/2852/190x190_153131_1.jpg"
     "http://imagem2.buscape.com.br/thumbs/ensopado/6424/190x190_175344_1.jpg"

    Thanks

    Mitesh
    ~Khatri Mitesh
    khatrimitesh@hotmail.com
    Software Developer
    Rajasthan-India
    http://www.youtube.com/watch?v=-oJaaPCrk3Q
    Sunday, June 7, 2009 9:32 AM
  • I tried loading the same image files you were and I ran into the same error.

    The issue is that the JPG files that you are attempting to download are GZIP encoded.  If you look at the HTTP response that comes back from the server when requesting the JPG, it has an HTTP header which is "Content-Encoding: gzip"  This of course means that the content that you requested is encoded using GZIP and needs to be decoded before it can be used.  This is why you can browse to the images using Internet Explorer and they work fine because IE knows how to deal with the "Content-Encoding: gzip" header and automagically decodes.  The fix for this is simple, just use the GZipStream after the MemoryStream is loaded with the encoded image.  You will also want to check for the GZIP encoding on the response headers so that it only decodes images that have the GZIP.  Here is the modified code that I created to make this work correctly.

            Dim response As WebResponse = WebRequest.Create("http://imagem2.buscape.com.br/thumbs/ensopado/6424/190x190_168472_1.jpg").GetResponse
            Dim bitmap As Bitmap = Nothing
            Using memStream As MemoryStream = New MemoryStream
                Using remoteStream As Stream = response.GetResponseStream
                    Dim nTotal As Integer = 0
                    Dim inBuffer As Byte() = New Byte(1024) {}
                    Dim nRead As Integer = 0
                    Do
                        nRead = remoteStream.Read(inBuffer, 0, inBuffer.Length)
                        If (nRead > 0) Then
                            memStream.Write(inBuffer, 0, nRead)
                        End If
                        nTotal = (nTotal + nRead)
                    Loop While (nRead > 0)
                End Using
                Dim strEncoding As String = response.Headers.Get("Content-Encoding")
                If (Not strEncoding Is Nothing) Then
                    If (strEncoding.ToLower = "gzip") Then
                        memStream.Position = 0
                        Using zipStream As GZipStream = New GZipStream(memStream, CompressionMode.Decompress)
                            bitmap = New Bitmap(zipStream)
                        End Using
                    End If
                Else
                    bitmap = New Bitmap(memStream)
                End If
            End Using
    

    why do you robot?
    Monday, June 8, 2009 5:01 AM
  • Thanx a lot for reply.
    but how i add the gzip reference in vb.net.
    can u tell me which file / reference i add for gzip.
    May be system.io.compression is only available on desktop not in the compact framework (window mobile)
    Please suggest
    Thanks



    ~Khatri Mitesh
    khatrimitesh@hotmail.com
    Software Developer
    Rajasthan-India
    http://www.youtube.com/watch?v=-oJaaPCrk3Q
    Monday, June 8, 2009 6:57 AM
  • Imports System.IO.Compression

    The GZipStream is supported in .NET Compact Framework 3.5.



    why do you robot?
    Monday, June 8, 2009 2:59 PM
  • Hi therobotgeek

    Thanks 4 help. your code is working for me. Your code is working fine in Cf 3.5. but i am using the CF 2.0. Can you suggest How the same task is possible in CF 2.0?
    Or any other Way to do the same task in 2.0.
    Please Suggest.
    And again Thanx for Help.


    Thanks
    Mitesh

    ~Khatri Mitesh
    khatrimitesh@hotmail.com
    Software Developer
    Rajasthan-India
    http://www.youtube.com/watch?v=-oJaaPCrk3Q
    Tuesday, June 23, 2009 11:23 AM
  • If you don't want to use CF v3.5, then you're going to need something to decompress the GZIP files.  You should look at SharpZipLib which is open source and has a version for Compact Framework.  You can also look at Xceed .NET which is a commercial product that has more advanced features than SharpZipLib.

    SharpZipLib
    http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx


    Xceed for Compact Framework
    http://xceed.com/Zip_NET_CF_Intro.html


    why do you robot?
    • Proposed as answer by therobotgeek Tuesday, June 23, 2009 6:35 PM
    Tuesday, June 23, 2009 6:35 PM