1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. The forum has been upgraded to a more recent and user-friendly software, called "XenForo".
    It will take some time to get everything set up and running smoothly, so please report any bug you may find to the forum staff.
    Thank you for your patience.

[VB.NET] MemoryStream to Image?

Discussion in 'Code Snippets and Tutorials' started by JenJen, Oct 26, 2009.

Thread Status:
Not open for further replies.
  1. JenJen

    JenJen New Member

    Joined:
    Oct 20, 2009
    Messages:
    18
    Likes Received:
    1
    Ok, I normally don't pester people with questions on programming, but I have been working for a week on an ABer, and It's darn near done. Only problem is it never buys (Doh!). Upon detailed interrogation, I determined there was one area that is causing an issue, and its, obviously pretty critical that it works, and thats the Captcha Picture section.

    My problem, is that I cannot get the picture to properly go from a memorystream to Image format. It seems to do the byte conversion fine, but when I try to store it in a varible (Image) it gives me a "Parameter is not valid." Error. I'm thurally frustrated at this point, so I thought I'd see if any of you guys have thoughts or suggestions.

    My unsuccessful trys so far:

    #1
    Code (Text):
    1.  
    2. Public Function GetCaptcha(ByVal strURL As String) As System.Drawing.Image
    3.     Dim msCaptcha As MemoryStream
    4.     Dim msWebClient As WebClient
    5.     msWebClient = New WebClient()
    6.     msCaptcha = New MemoryStream(msWebClient.DownloadData(URL))
    7.     GetCaptcha = Image.FromStream(msCaptcha)
    8. End Function
    #2
    Code (Text):
    1.  
    2. Public Function GetCaptcha(ByVal strURL As String) As System.Drawing.Image
    3.     Dim memStream As New _
    4.     MemoryStream(System.Text.Encoding.Default.GetBytes(Wrapper.StripHeaders(Wrapper.Request("GET", strURL, ""))))
    5.     GetCaptcha = Image.FromStream(memStream)
    6.     Return GetCaptcha
    7. End Function
    #3
    Code (Text):
    1.  
    2. Public Function GetCaptcha(ByVal strURL As String) As System.Drawing.Image
    3.     Dim bytes() As Byte = System.Text.Encoding.Default.GetBytes(Wrapper.StripHeaders(Wrapper.Request("PIC", URL, "")))  
    4.     Dim objMS As New System.IO.MemoryStream
    5.     objMS.Write(bytes, 0, bytes.Length)
    6.     GetCaptcha = System.Drawing.Image.FromStream(objMS)
    7. End Function
    8.  
    I had a few other versions as well, but reguardless, ALL of them error at the last part, where its supposed to take the stream and store it in the GetCaptcha image varible.

    EDIT - I just realized this is in the totally wrong section. ARg...been up for 24 hours...sleep deprived LOL

    I also verified that method 1 works with some random hotlinked jpg on the interwebz, so the issue has to be what I had half thought before, and that is that there is no captcha being streamed for some reason, hence no data to make an image from. But why its not getting the captcha, IDK. It gets the right URL.
     
  2. Zer0

    Zer0 New Member

    Joined:
    Mar 2, 2008
    Messages:
    3,037
    Likes Received:
    180
    Location:
    Home sweet home
    I ran into this strange problem as well quite a while ago (using Java). I haven't really been able to find the source of the problem, but there might be some work-arounds.

    I noticed that the image is being sent to the program, but for some reason the program fails to interpret it correctly. Instead of converting the byte stream to an image, I saved the data to my hard drive and opened it up with an image processing program. And much to my surprise, the captcha image was there.

    So as a workaround, try saving the byte stream to a file, reading the file, and then convert that to an image rather than converting the original byte stream to an image. (Note: I haven't tried this myself so I dunno if it'll work or not)
     
  3. ricky92

    ricky92 Administrator
    Staff Member

    Joined:
    Nov 10, 2006
    Messages:
    1,866
    Likes Received:
    67
    Method 1 should work, if you add the wrapper cookies to the WebClient object. Neopets captcha needs to know you're logged in :p
     
  4. JenJen

    JenJen New Member

    Joined:
    Oct 20, 2009
    Messages:
    18
    Likes Received:
    1
    Thanks guys :)

    I haven't had time since I posted this to work on the project, I am overloaded at work, doing O.T. shifts as some other coworkers are sick...

    Zero - That was one thing I had pondered. I know that as long as I can get the image to either the picturebox, or as a file on the computer, I can work with it. Its worth a shot if the other methods fail.

    Ricky - I havent had time to get back on my computer, but the odd thing, is that when I use the HTML visualizer for the item page whilst debugging, the wrapper loads the haggle page, and all gifs, the only things that are not visible are the captcha, and the flash ads (naturally the flash ads wont display properly).

    I am not sure if its a cookie issue (with method one, it would be, as webclient current isnt passed cookies), but with the wrapper loading the haggle page, I would want to think that using the HTML visualizer, the jpg captcha should display, considering that gif images display.

    THe only thing I can conclude is that perhaps the type definitions for the wrapper are incorrect for jpgs.

    None the less, I did not think about passing cookies to the webclient model I used in method one. THat is something I will try. As long as I dont have to log in it would work great, as obviously constantly logging in would perk TNTs eyes.

    THanks again guys, I'll see what I can come up with when I have time, and if I am still stuck, I'll advice from there.
     
Thread Status:
Not open for further replies.

Share This Page