Imports System.IO 'used to work with streams
Imports System.Net 'used for client side HTTP communication
Imports System.Text.RegularExpressions 'used to find matches
Imports System.Drawing.Imaging 'used to read and write image files
Public Class websitemonitor
Dim startmonitor As Integer 'will be 1 when monitor starts
Dim radarpic(250) As Image 'holds the radar images
Dim picshow As Integer 'will be 1 when display selected
Dim picnum As Integer 'number of the radar picture
Dim picnumd As Integer 'pic number used in display routines
Dim imageadd As String 'address of radar image
Dim imaget As String 'temp as string + dosent work on array
Dim req As WebRequest ' used to get the stream back from the server
Dim rsp As WebResponse 'response
Dim ms As MemoryStream ' used to move the image into the PictureBox
Dim filepath As String ' filepath of my document on user computer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
startmonitor = 1
webcallnow()
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Webcallnow()
End Sub
Private Sub webcallnow()
If startmonitor = 1 Then 'get site and image
'enter the required radar site
Dim Url As String = TextBox1.Text
Dim Page As String 'this will hold the full web page
Try 'wrap in try catch blocks to ensure timely release
'create the request for the web page
Dim PageRequest As HttpWebRequest = CType(WebRequest.Create(Url), HttpWebRequest)
'get the respose this could take time
Dim PageResponse As WebResponse = PageRequest.GetResponse()
'read the response into a stream 'r'
Dim r As New StreamReader(PageResponse.GetResponseStream())
'put into standard string page
Page = r.ReadToEnd() 'start to finish
'close stream
r.Close()
Catch err As Exception 'write error message to textbox
TextBox3.Text = "internet not working " & err.ToString()
Return
End Try
'use text.regularexpression regex to find match for title
Dim TitlePattern As String = "
(?.*?)"
Dim TitleRegex As New Regex(TitlePattern, RegexOptions.IgnoreCase Or RegexOptions.Singleline)
Dim TitleMatch As Match = TitleRegex.Match(Page)
If TitleMatch.Success Then
TextBox2.Text = "Found site: " & TitleMatch.Groups("match").Value
End If
'use text.regularexpression regex to find match for radar picture
Dim TitlePatternp As String = "/radar/IDR(?.*?)border=0"
'to change image subsitute your
Dim TitleRegexp As New Regex(TitlePatternp, RegexOptions.IgnoreCase Or RegexOptions.Singleline)
Dim TitleMatchp As Match = TitleRegexp.Match(Page)
If TitleMatchp.Success Then
TextBox4.Text = "Found image: " & "/radar/IDR" & TitleMatchp.Groups("match").Value & " Total images found=" & picnum
imageadd = TitleMatchp.Groups("match").Value
imageadd = "http://mirror.bom.gov.au/radar/IDR" & imageadd
End If
If picnum = 0 Or imaget <> imageadd Then ' First time through get first image
imaget = imageadd 'only get new image when we have new image on site
imagereceive(imageadd, picnum)
picnum = picnum + 1
End If
End If
End Sub
Private Sub imagereceive(ByVal imageadd, ByVal picnum)
' This method requests and receives an image from a website. The file
' is streamed back to this method, which copies the contents into a
' MemoryStream, then associates the MemoryStream with a PictureBox on
' the form. The contents are streamed using the ResponseStream of the
' WebResponse class. The stream access is wrapped in Try/Catch blocks
' to ensure timely release of the resources.
Dim picnumsave As String 'file string
Try
' This creates the WebRequest for the image at BOM
req = WebRequest.Create(imageadd)
' Use a GET since we' re not sending any data
req.Method = "GET"
' ObjRef to get the server's response.
Try
' This causes the round-trip to retrieve the image.
rsp = req.GetResponse()
' Create a MemoryStream to hold the image
ms = New MemoryStream()
' Copy the streamed image into the MemoryStream
CopyData(rsp.GetResponseStream(), ms)
' Load the image into the Picture array
radarpic(picnum) = Image.FromStream(ms)
displayimage.Image = radarpic(picnum)
'get file path for new image
Dim picnums As String = picnum
picnumsave = "radar" + picnums + ".gif" 'must specify gif
filepath = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "radarimage")
filepath = System.IO.Path.Combine(filepath, picnumsave)
' get new file path for next image
displayimage.Image.Save(filepath, ImageFormat.Gif)
'displayimage.Image.Save("C:\Documents and Settings\carl\My Documents\My Pictures\radar.gif", ImageFormat.Gif)
'this instruction saves image to computer file system
TextBox3.Text = "image received corectly. The image received is displayed below"
Catch exp As Exception
' Will catch any error that we're not explicitly trapping.
MessageBox.Show(exp.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)
Finally
' Guarantee the streams will be closed
If Not ms Is Nothing Then ms.Close()
If Not rsp Is Nothing Then rsp.GetResponseStream.Close()
End Try
Catch exp As Exception
' Will catch any error that we're not explicitly trapping.
MessageBox.Show(exp.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)
End Try
End Sub
Private Sub CopyData(ByVal FromStream As Stream, ByVal ToStream As Stream)
' This routine copies content from one stream to another, regardless
' of the media represented by the stream.
' This will track the # bytes read from the FromStream
Dim intBytesRead As Integer
' The maximum size of each read
Const intSize As Integer = 4096
Dim bytes(intSize) As Byte
' Read the first bit of content, then write and read all the content
' From the FromStream to the ToStream.
intBytesRead = FromStream.Read(bytes, 0, intSize)
While intBytesRead > 0
ToStream.Write(bytes, 0, intBytesRead)
intBytesRead = FromStream.Read(bytes, 0, intSize)
End While
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
picshow = 1 'display the images
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If picshow = 1 And picnum > 0 Then
'display selected and have 1 or more pics
displayimage.Image = radarpic(picnumd)
Label3.Text = picnumd + 1
picnumd = picnumd + 1
If picnumd = picnum Then
picnumd = 0 'last displayed so start again
End If
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
picshow = 0 'pause the image display
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If picnumd > 0 Then 'cant go back to minus
picnumd = picnumd - 1 'get previous image
End If
displayimage.Image = radarpic(picnumd) 'display
Label3.Text = picnumd + 1
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
If picnumd < picnum - 1 Then 'cant go pass the last
picnumd = picnumd + 1 'get next image
End If
displayimage.Image = radarpic(picnumd) 'display
Label3.Text = picnumd + 1
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
startmonitor = 0 'end monitor of site
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
filepath = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "radarimage")
My.Computer.FileSystem.CreateDirectory(filepath)
'creates a folder radarimages in my documents on user computer
End Sub
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
Timer1.Interval = 250
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
Timer1.Interval = 500
End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
Timer1.Interval = 1500
End Sub
End Class