idea for a app. banner rotator w/ automatic Internet updates

Do you need something made? then ask in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
12 posts Page 2 of 2
Contributors
User avatar
NewGuy
New Member
New Member
Posts: 20
Joined: Sat Jun 04, 2011 8:46 am

mandai wrote:
You could use this:
Code: Select all
    Dim imageList As String()

    Sub refreshBanners()

        Dim wc As WebClient = New WebClient()
        Dim banners As List(Of Image) = New List(Of Image)

        For i As Integer = 0 To imageList.Length - 1 'downloads and stores the images in memory
            Dim imagebytes As Byte()
            Try
                imagebytes = wc.DownloadData(imageList(i))
            Catch ex As Exception
                MsgBox("Error downloading banner " & imageList(i) & ": " & ex.Message)
                Continue For
            End Try

            Dim ms As MemoryStream = New MemoryStream(imagebytes)

            Dim img As Image
            Try
                img = Image.FromStream(ms)
            Catch ex As Exception
                MsgBox("Error getting image from " & imageList(i))
                Continue For
            Finally
                ms.Close()
            End Try

            picBanner.Image = img
            banners.Add(img)

            Thread.Sleep(10000) 'starting delay per banner
        Next

        If banners.Count = 0 Then Return

        While True 'loops through downloaded images

            For i As Integer = 0 To banners.Count - 1
                picBanner.Image = banners(i)
                Thread.Sleep(10000) 'delay per banner
            Next
        End While
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim wc As WebClient = New WebClient()
        imageList = wc.DownloadString("http://localhost/banners.txt").Split(vbCr)
        For i As Integer = 0 To imageList.Length - 1
            imageList(i) = imageList(i).Replace(vbLf, "")
        Next

        Dim t As Thread = New Thread(AddressOf refreshBanners)
        t.IsBackground = True
        t.Start()
    End Sub
Where banners.txt could be something like this:
Code: Select all
http://localhost/banners/banner1.png
http://localhost/banners/banner2.png
Thanks for this input Mandai :D .. i truely appreciate it ! - i have tried this .. and i had a working solution, and i loved the way it displayed pics in my app ... and that shouldnt be changed at all .... worked with thread.Sleep(10000) and all ... but if its not to much trouble ... would you help me change your suggestion with some minor details ? :)

1.i would like for it to check for internet availability
2. if internet is present, then goto the server, ex : www.myserver.com\banners
3. on the server where all the banners and the banners.txt are present, - download all .. ex:
-banner1.jpg
-banner2.jpg
-banner3.jpg
-banners.txt

By deault always overwriting all the present local files in "c:\my application\banners" ... without any prompt

maybe the banners.txt even contains a "banner version number" in the first line .. that the application checks up against ?.. and if the "banner version number" on "www.myserver.com\banners" is higher than the "banner version numberr" in the local installed application ... then banners are downloaded ... if its not .. then no banners are downloaded

4. the banner diplay ( picBanner.Image) should by default always run out of a local folder ex : "c:\my application\banners" - and not from a website ... this makes the application internet independant

if you would help me out with this i would be most greatfull ! :)
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

If you want it to check for internet availability then all you need to do is add a try/catch block to the WebClient.DownloadString function under the form load function.
Code: Select all
        Try
            imageList = wc.DownloadString("http://localhost/banners.txt").Split(vbCr)
        Catch ex As Exception
            'error downloading banners.txt/happens when internet is unavailable
            imageList = {}
        End Try
You could use this code to store banners and always re-download them:
Code: Select all
    Sub refreshBanners()

        Dim wc As WebClient = New WebClient()

        Dim localFiles As String() = Directory.GetFiles("banners")

        For i As Integer = 0 To imageList.Length - 1
            Dim filename As String = imageList(i).Remove(0, imageList(i).LastIndexOf("/"c) + 1)
            Try
                wc.DownloadFile(imageList(i), "banners/" & filename)
            Catch ex As Exception
                MsgBox("Error downloading banner " & imageList(i) & " or saving to " & filename & ": " & ex.Message)
                Continue For
            End Try

            Dim img As Image = Image.FromFile("banners/" & filename)
            picBanner.Image = img

            Thread.Sleep(10000) 'starting delay per banner 
        Next

        localFiles = Directory.GetFiles("banners") 'refresh local files
        If localFiles.Length = 0 Then Return

        While True

            For i As Integer = 0 To localFiles.Length - 1
                picBanner.Image = Image.FromFile(localFiles(i))
                Thread.Sleep(10000) 'delay per banner
            Next
        End While
    End Sub
For the versioning you could check to see if the filename contains the name of an existing image minus the extension. You could then parse an integer from part of the new filename and if it is different the old file could be deleted/replaced.
12 posts Page 2 of 2
Return to “Tutorial Requests”