传递WebResponse字节数组以提取图像

时间:2016-06-28 15:47:36

标签: vb.net image bytearray

有许多建议在Internet上使用Image.FromStream(stream),看起来应该按照我的意愿行事。但是,只有一件我不完全理解。

我正在向一个返回字节数组的URL发出GET请求,该数组是一个图像。我发出请求并得到这样的回复:

Public Function getPic(ByRef stu As String) As Image

    Dim url As String = "myurl" & stu 'identifying number I am adding to the url
    Dim hwrequest As HttpWebRequest = Net.WebRequest.Create(url)
    hwrequest.Method = "Get"

    Dim hwresponse = hwrequest.GetResponse().GetResponseStream()
    Dim ms As New MemoryStream()
    hwresponse.CopyTo(ms)
    Dim img As Image
    img = Image.FromStream(ms)

    PictureBox1.Image = img

End Function

我找到了两个不同的代码建议:

Public Function byteArrayToImage(ByVal byteArrayIn As Byte()) As Image
    Using mStream As New MemoryStream(byteArrayIn)
        Return Image.FromStream(mStream)
End Function

在这里,我理解我需要从我对函数的响应中传递Byte数组,但这是我缺乏理解的地方。当我通过

 Public Function byteArrayToImage(ByVal hwresponse As Byte()) As Image
    Using mStream As New MemoryStream(hwresponse)
        Return Image.FromStream(mStream)

我得到" Stream的值无法转换为Byte"

当我尝试这段代码时:

Private Function BytesToImage(hwresponse AS Stream) As Image
Dim imgNew As Image
Dim memImage As New System.IO.MemoryStream(hwresponse)
imgNew = Image.FromStream(memImage)
Return imgNew

结束功能

我得到"参数无效"在Image.fromStream上。

我不明白如何获取我的WebResponse并将其正确传递给函数以提取图像

0 个答案:

没有答案