简体   繁体   中英

VB.Net Picturebox “Parameter is not Valid” when get online images

Dim ShowImgWebClient = New WebClient
Dim ImageInBytes As Byte() = ShowImgWebClient.DownloadData("http://ex.domain.com/index.php?checksum=" & lblCheckSum.Text.ToString())
Dim ImageStream As New MemoryStream(ImageInBytes)
Dim CaptchaImg As Image = New Bitmap(ImageStream)
ptbCaptcha.Image = CaptchaImg

I'm use this code for get online captcha to my picturebox name ptbCaptcha .

but when request this image my program show error Parameter is not Valid .

Why this ? and How to make it work ?

This is not the proper way to use MemoryStream .

Here is the example taken from microsoft website :

Imports System
Imports System.IO
Imports System.Text

Module MemStream

    Sub Main()

        Dim count As Integer 
        Dim byteArray As Byte()
        Dim charArray As Char()
        Dim uniEncoding As New UnicodeEncoding()

        ' Create the data to write to the stream. 
        Dim firstString As Byte() = _
            uniEncoding.GetBytes("Invalid file path characters are: ")
        Dim secondString As Byte() = _
            uniEncoding.GetBytes(Path.GetInvalidPathChars())

        Dim memStream As New MemoryStream(100)
        Try 
            ' Write the first string to the stream.
            memStream.Write(firstString, 0 , firstString.Length)

            ' Write the second string to the stream, byte by byte.
            count = 0
            While(count < secondString.Length)
                memStream.WriteByte(secondString(count))
                count += 1
            End While 

            ' Write the stream properties to the console.
            Console.WriteLine( _
                "Capacity = {0}, Length = {1}, Position = {2}", _
                memStream.Capacity.ToString(), _
                memStream.Length.ToString(), _
                memStream.Position.ToString())

            ' Set the stream position to the beginning of the stream.
            memStream.Seek(0, SeekOrigin.Begin)

            ' Read the first 20 bytes from the stream.
            byteArray = _
                New Byte(CType(memStream.Length, Integer)){}
            count = memStream.Read(byteArray, 0, 20)

            ' Read the remaining Bytes, Byte by Byte. 
            While(count < memStream.Length)
                byteArray(count) = _
                    Convert.ToByte(memStream.ReadByte())
                count += 1
            End While 

            ' Decode the Byte array into a Char array  
            ' and write it to the console.
            charArray = _
                New Char(uniEncoding.GetCharCount( _
                byteArray, 0, count)){}
            uniEncoding.GetDecoder().GetChars( _
                byteArray, 0, count, charArray, 0)
            Console.WriteLine(charArray)
        Finally
            memStream.Close()
        End Try 

    End Sub 
End Module

See MemoryStream for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM