从流中检索图像时参数无效

时间:2019-01-29 14:00:00

标签: vb.net

我需要从流中获取图像。第一次运行时,我可以获取图像并将其保存,但是第二次运行时,出现“参数无效”错误 请在下面查看我的代码:

Dim url = "http://172.23.111.102//onvifsnapshot/media_service/snapshot?channel=1&subtype=1"
Dim req As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)

Private Sub LaneCtl_Load(sender As Object, e As EventArgs) Handles Me.Load
    req.Credentials = New NetworkCredential("admin", "admin")
End Sub

Private Function CaptureImage(ByVal imagePath As String, ByVal fileName As String, CaptureNow As Boolean) As String
    Try
        If CaptureNow Then
            If (CamType = 2) Then

                Dim fn = fileName
                Dim filePath = imagePath
                Dim file = $"{filePath}{fn}"

                Dim buffer As Byte() = New Byte(99999) {}
                Dim read As Integer = 0
                Dim total As Integer = 0

                Dim resp As WebResponse = req.GetResponse
                Dim stream As IO.Stream = resp.GetResponseStream
                Dim bm As Image = New Bitmap(stream)
                bm.Save(file, Imaging.ImageFormat.Jpeg)
            End If

        End If
    Catch ex As Exception
        CreateLogTextFile(String.Format("Error on Capture Image Request >> {0}", ex.Message.ToString), False)
    End Try
    Return IO.Path.Combine(imagePath, fileName)
End Function

请帮助我。谢谢。

1 个答案:

答案 0 :(得分:1)

尝试一下

 Dim bm As Image = Image.FromStream (stream)
 bm.Save(file, Imaging.ImageFormat.Jpeg)
 bm.Dispose ()