1 Previous Next 

AspNet: Server Side Clock (showing the use of timer and graphics)


This clock tip is mainly meant to show:
 How to make an image from scratch;
 How to use a client side timer;
 How to use an extra page to show an image.

 

'''Form 1 Needs an imagebox on the page
Private Sub Page_Load(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles MyBase.Load
        Image1.Height = New Unit(32)
        Image1.Width = New Unit(200)
        Image1.Visible = True
'Beneath is the Timer
        Dim scriptString As String = "<script language=JavaScript>" & _
        "setclock(); function setclock(){document.images.Image1.src = " & _
        "'http://localhost/WebClock/WebForm2.aspx';setTimeout('setclock()',1000)}</script>"
        Page.RegisterStartupScript("setclock", scriptString)
End Sub

'''Form2 needs only an empty page
   Private Sub Page_Load(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles MyBase.Load
        Response.Cache.SetExpires(DateTime.Now.AddTicks(500))
        Dim myForeBrush As Brush = Brushes.Black
        Dim myFont As New Font("Times New Roman", 8, FontStyle.Regular)
        Dim textHeight As Single
        Dim bm As New Bitmap(120, 20)
        Dim g As Graphics = Graphics.FromImage(bm)
        g.Clear(Color.White)
        Dim textSize As SizeF = g.MeasureString(now.tostring, myFont)
        g.DrawString(Now.ToString, myFont, myForeBrush, _
            New RectangleF(0, 0, 120, 20))
        Dim ms As New IO.MemoryStream
        Dim arrImage() As Byte
        bm.Save(ms, Imaging.ImageFormat.Bmp)
        arrImage = ms.GetBuffer
        Response.BinaryWrite(arrImage)
        g.dispose
End Sub



1 Previous Next