Simple Image Slide Show C# edition
Simple Image Slide Show C# edition
By Hehong Yang(杨贺宏)
In Scott Mitchell’s article, he create a simple image slide show using asp.net with vb.net. Now I rewrite it in C#. I create a sub directory Photos to store the image files. And instead of using DataList control, I used DropDownList control to list all image files. I use it in my personal web site, so you can view a live demo there( http://www17.brinkster.com/flycrane/Album/default.aspx ).
Soure code: Default.aspx
<%@ Page Language="C#" %>
<%@ import Namespace="System.IO" %>
<script runat="server">
// Insert page code here
private void Page_Load(object sender, System.EventArgs e)
{
//Get list of images
DirectoryInfo dirInfo= new DirectoryInfo( Server.MapPath("Photos"));
FileInfo[] images= ImagesFilter( dirInfo.GetFiles() );
//Determine the current image to show
int imageIndex= 0;
if( Request.QueryString["N"]!=null )
imageIndex= Convert.ToInt32( Request.QueryString["N"] );
lblTitle.Text+= Path.GetFileNameWithoutExtension( images[imageIndex].Name )
+" ("+ (imageIndex+1) +" of "+images.Length+")";
currentImage.ImageUrl= "/flycrane/Album/Photos/"+images[imageIndex].Name ;
if( imageIndex>0 )
lnkPrev.NavigateUrl= "Default.aspx?N=" + (imageIndex - 1);
if( imageIndex<images.Length-1 )
lnkNext.NavigateUrl= "Default.aspx?N=" + (imageIndex +1);
if(! IsPostBack)
{
ddlImages.DataSource= images;
ddlImages.DataBind();
}
}
public FileInfo[] ImagesFilter( FileInfo[] files )
{
ArrayList newImages= new ArrayList( files.Length );
for( int i=0;i<files.Length;i++ )
{
if( Path.GetExtension( files[i].Name.ToLower() )==".jpg" ||
Path.GetExtension( files[i].Name.ToLower() )==".jpeg" ||
Path.GetExtension( files[i].Name.ToLower() )==".png" ||
Path.GetExtension( files[i].Name.ToLower() )==".gif"
)
newImages.Add(files[i]);
}
return ( FileInfo[] ) newImages.ToArray( files[0].GetType() );
}
void ddlImages_SelectedIndexChanged(object sender, EventArgs e) {
Response.Redirect( "default.aspx?N="+ (ddlImages.SelectedIndex) );
}
</script>
<html>
<head>
<title>Flycrane's Personal Web Site</title>
<link rel="stylesheet" src="/flycrane/Styles/Flycrane.css" />
</head>
<body>
<form runat="server">
<table cellspacing="0" cellpadding="0" width="765" align="center" border="0">
<!--DWLayoutTable-->
<tbody>
<tr>
<td align="middle" width="765" bgcolor="#9999cc">
<asp:Label id="lblTitle" runat="server" width="206px">You are viewing: </asp:Label></td>
</tr>
<tr>
<td align="middle" width="765">
<p>
<asp:Image id="currentImage" runat="server"></asp:Image>
</p>
</td>
</tr>
<tr>
<td align="middle" width="765" bgcolor="#9999cc">
<asp:HyperLink id="lnkPrev" runat="server">Previous</asp:HyperLink>
<asp:HyperLink id="lnkNext" runat="server">Next</asp:HyperLink>
</td>
</tr>
<tr>
<td align="middle" width="765" bgcolor="#9999cc">
Select one photo:
<asp:DropDownList id="ddlImages" runat="server" OnSelectedIndexChanged="ddlImages_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
Reference
Scott Mitchell. Creating a Simple Image Slide Show. http://aspnet.4guysfromrolla.com/articles/070704-1.aspx