Imports System Imports System.ComponentModel Imports System.IO Imports System.Web Imports System.Web.UI Imports System.Web.HttpResponse Namespace LozinskiWebScript Public Class RandomImage2 : Inherits Control '#CLASS: RandomImage2 - Version 1.0 '# '#INSTALLATION INSTRUCTIONS: '# 1) Compile this VB .NET code and move the resulting dll file into your web project's "bin" directory. '# 2) In the aspx page you wish to have this control run, add in the appropriate "Register" directive. '# Example: '# <%@ Register TagPrefix="LozinskiWebScript" Namespace="LozinskiRandomImage2.Lozinski" Assembly="LozinskiRandomImage2" %> '# 3) Add the control's tag to your ASPX page wherever you would like the random image to occur. '# You MUST supply the "ImageDirectory" property as a URL relative to the location of the aspx page. '# The URL must have a trailing slash "/" character. '# Example tag below given the above "Register": '# '# '#TO ADD THE CONTROL TO YOUR VISUAL STUDIO TOOLBOX: '# 1) Select "File | New Project" '# 2) Make sure "VB Class" is slected. Change the default name '# to "RandomImage2.vb", select the location, and '# select "close solution". '# 3) Click "ok". '# 3) Copy and paste this code into your new class file. '# 4) Compile this file. '# 5) Open up your web project with the aspx forms you would like '# to have the random images on. '# 6) Select "Project | Add Reference" '# 7) Click the "browse" button. Find the location of the '# LozinskiRandomImage2.dll file, select the file, '# and click "ok". '# 8) You should now see it in your "solution explorer". '# 9) In the design view to any of your aspx web forms, right click '# within your toolbox and select "add/remove items". '# 10) Click "browse", select the LozinskiRandomImage2.dll '# file, and click "ok". '# 11) It should now be in your toolbox! Just drag and drop '# like any other tool. Enjoy! :) '# '#REQUIREMENTS: '# 1) Windows 2000 Server or later '# 2) IIS 5.0 or later '# 3) .NET Framework >= 1.1 '# '#ABOUT THIS SCRIPT: '# This is a very simple .NET script. All it does is grab a '#random image from a directory and print the name within the associated '#HTML IMG tag. This code assumes the files stored in the '#directory are actual images such as jpgs, gifs, etc. '# '#HISTORY: '# 07/30/05 1.0 Original release '# '#AUTHOR: '# http://www.davelozinski.com/scripts '# '#Global class variable which holds the location of the random images. Private IMAGE_DIRECTORY As String _ Public Property ImageDirectory() As String '#This value needs to be set in order for this control to work. Get Return Me.IMAGE_DIRECTORY End Get Set(ByVal Value As String) If (Value = "") Then Throw New ArgumentException("""ImageDirectory"" cannot be an empty string.") ElseIf (Not Value.EndsWith("/")) Then Throw New ArgumentException("""" & Value & """ should have a trailing slash ""/"" character.") Else Me.IMAGE_DIRECTORY = Value End If End Set End Property Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter) '#Render the HTML IMG tag, or the textual message if '#no files are found. Dim Images() As String '#Holds the list of images to choose randomly from. Dim ReturnStr As String '#Either the IMG tag or message to be returned. Dim tempString As String '#Holds the list of images within the directory Dim dir As DirectoryInfo If (Not Directory.Exists(HttpContext.Current.Server.MapPath(Me.IMAGE_DIRECTORY))) Then Throw New ArgumentException("""" & Me.IMAGE_DIRECTORY & """ does not appear to be a valid directory.") Else dir = New DirectoryInfo(HttpContext.Current.Server.MapPath(Me.IMAGE_DIRECTORY)) For Each fi As FileInfo In dir.GetFiles("*.*") tempString = tempString & fi.Name & "," Next If (Len(tempString) > 0) Then tempString = tempString & "," '#Puts a double comma to demark the end tempString = StrReverse(tempString) tempString = Replace(tempString, ",,", "", 1) tempString = StrReverse(tempString) Images = Split(tempString, ",") Randomize() ReturnStr = "" & vbCrLf Else ReturnStr = "No random images located in """ & Me.IMAGE_DIRECTORY & """" & vbCrLf End If output.Write(ReturnStr) End If End Sub End Class End Namespace