【问题标题】:VB.Net Picturebox "Parameter is not Valid" when get online imagesVB.Net Picturebox“参数无效”获取在线图像时
【发布时间】:2014-08-07 22:00:57
【问题描述】:
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

我正在使用此代码为我的图片框名称获取在线验证码ptbCaptcha

但是当我请求这个图像时,我的程序显示错误参数无效

为什么会这样?以及如何让它发挥作用?

【问题讨论】:

  • 可能是这样的:lblCheckSum.Text。删除.ToString()。它已经是一个字符串..
  • 错误是由这个引起的吗?
  • 将 ImageStream 调暗为新的 MemoryStream(ImageInBytes)
  • 错误发生在哪一行?确切的消息是什么(包括堆栈跟踪,如果可能的话)?

标签: vb.net picturebox


【解决方案1】:

这不是使用MemoryStream 的正确方法。

以下是来自微软网站的示例:

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

更多信息请参见MemoryStream

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 2012-08-01
    • 2019-06-30
    • 2013-10-11
    相关资源
    最近更新 更多