Search code examples
vb.netrazorip-cameraasp.net-webpages

VB Razor Web Page to render Image from remote URL on page?


I am currently implementing Camera streaming on a web application.

I have a page Poll_Camera.vbhtml, when accessing this page I want the page to only render the image produced from the remote URL.

Every time the URL gets accessed it produces a new snapshot image from the Camera.

What would be the best way to do this?

I need to refresh the image every second or so.

I have tried to use

Dim CameraResponse As WebRequest = WebRequest.Create(CameraUri)

I have managed to do this with Javascript/Ajax, but it's not ideal since the URL contains the username and password for the camera. I also don't want to download the image first to a local directory.

Another piece of code I have tried where I get response to the screen but I think the encoding or something might be wrong:

Try
    Dim request As HttpWebRequest = CType(WebRequest.Create(CameraUri), HttpWebRequest)

    Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)

    ' Get the stream associated with the response.
    Dim receiveStream As Stream = response.GetResponseStream()

    ' Pipes the stream to a higher level stream reader with the required encoding format. 
    Dim readStream As New StreamReader(receiveStream, Encoding.UTF8)

    HttpContext.Current.Response.write(readStream.ReadToEnd())
    response.Close()
    readStream.Close()
Catch ex As System.Net.WebException
    'Error in accessing the resource, handle it
End Try

I get the following output on the screen

����JFIF���

I have tried setting the following, but then I get a black screen.

Response.ContentType = "image/jpeg"
Response.Charset = "UTF-8"

Any help would be greatly appreciated.


Solution

  • Managed to solve the above by using doing the following. Easy and minimal code.

    Try
        Dim webClient As New System.Net.WebClient
        Response.WriteBinary(webClient.DownloadData(siteUri), "image/jpeg")
    Catch ex As System.Net.WebException
        'Error in accessing the resource, handle it
    End Try
    

    Now I can parse the encrypted URL to my Poll_Camera.vbhtml, decrypt on the page and page will render camera image as jpeg.