Simple Image Slide Show C# edition
2004-09-19 19:52
459 查看
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;
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>
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相关文章推荐
- android轮播图之ImageSlideshow
- 【简报】一款超灵活的jQuery幻灯插件: Really Simple Slideshow
- 【简报】一款超灵活的jQuery幻灯插件: Really Simple Slideshow
- 15 Amazing jQuery Image Gallery/Slideshow Plugins and Tutorials
- Image slideshow - 图片相册插件
- 【简报】一款超灵活的jQuery幻灯插件: Really Simple Slideshow
- slide-show image from MongoDB GridFS chunks @ D...
- 15 Amazing jQuery Image Gallery/Slideshow Plugins and Tutorials
- C# 窗体位置 Show和ShowDialog
- 一个简单的 C# 图片缩放模块 —— Less.Image
- IP Watchdog: Simple Windows Service Written in C#
- Deep learning for image segmentation slide by Matthew Opala
- Image Processing for Dummies with C# and GDI+ Part 6 - The HSL color space
- 基于Simple Image Statistics(简单图像统计,SIS)的图像二值化算法。
- C# 存取SqlServer中的Image类型
- C#(Winform)的Show()和ShowDialog()方法
- C# 使用Image Guid 验证图片类型 (转载,感谢原作者)
- 给SlideShowExtender增加链接
- C# 窗体位置 Show和ShowDialog
- Visual Studio 2010——C#的TreeView和ImageList控件的使用