img yanghehong

Simple Image Slide Show C# edition

发表于2004/9/19 19:52:00  1329人阅读

Simple Image Slide Show C# edition


   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;






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"




return ( FileInfo[] ) newImages.ToArray( files[0].GetType() );


void ddlImages_SelectedIndexChanged(object sender, EventArgs e) {

Response.Redirect( "default.aspx?N="+ (ddlImages.SelectedIndex) );





<title>Flycrane's Personal Web Site</title>

<link rel="stylesheet" src="/flycrane/Styles/Flycrane.css" />



<form runat="server">

<table cellspacing="0" cellpadding="0" width="765" align="center" border="0">




<td align="middle" width="765" bgcolor="#9999cc">

<asp:Label id="lblTitle" runat="server" width="206px">You are viewing: </asp:Label></td>



<td align="middle" width="765">


<asp:Image id="currentImage" runat="server"></asp:Image>





<td align="middle" width="765" bgcolor="#9999cc">

<asp:HyperLink id="lnkPrev" runat="server">Previous</asp:HyperLink>

&nbsp;<asp:HyperLink id="lnkNext" runat="server">Next</asp:HyperLink>




<td align="middle" width="765" bgcolor="#9999cc">

Select one photo:

<asp:DropDownList id="ddlImages" runat="server" OnSelectedIndexChanged="ddlImages_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>









Scott Mitchell. Creating a Simple Image Slide Show. http://aspnet.4guysfromrolla.com/articles/070704-1.aspx
0 0



取 消