QingHan.NET技术交流

路漫漫兮其修远,吾上下而求索....

导航

用C#编写的一个简单的图片浏览程序

Posted on 2006-08-10 11:12  鱼水情  阅读(1561)  评论(0)    收藏  举报

   前几天在做一个电子相册,由于在网上所见到的都是用VB.NET写的,所以就用C#写了一个,代码如下:

C#代码部分:

using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
namespace WebApplication10
{
 /// <summary>
 /// WebForm1 的摘要说明。
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Image Image1;
  protected System.Web.UI.WebControls.DataList DataList1;
  protected System.Web.UI.WebControls.HyperLink HyperLink1;
  protected System.Web.UI.WebControls.HyperLink HyperLink2;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   int index = 0;
   DirectoryInfo di = new DirectoryInfo(Server.MapPath("") + "/photo");//设置图片所在的位置
   FileInfo[] fi = di.GetFiles("*.jpg");//读取DI目录下的所有JPG文件

   this.Image1.ImageUrl = Path.GetFileName(fi[0].Name);//得到图片文件的
   this.DataList1.DataSource = fi;
   this.DataList1.DataBind();
   
   if(Request.QueryString["N"] != null)
   {
    index = int.Parse(Request.QueryString["N"]);
   }

   if(index > 0 )
   {
    this.HyperLink1.NavigateUrl = "WebForm1.aspx?N=" + (index - 1);
    this.Image1.ImageUrl = Path.GetFileName(fi[index - 1].Name);
   }

   if(index < fi.Length - 1)
   {
   
    this.HyperLink2.NavigateUrl = "WebForm1.aspx?N=" + (index + 1);
    this.Image1.ImageUrl =   Path.GetFileName(fi[index + 1].Name);

   }

  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.DataList1.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler(this.DataList1_ItemDataBound);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
  {
   if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
   {
    HyperLink hi = (HyperLink)(e.Item.FindControl("lnkpic"));
    hi.Text = Path.GetFileNameWithoutExtension(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"Name")))  +
     "(" + Convert.ToInt32(DataBinder.Eval(e.Item.DataItem,"Length")) / 1000 + "KB" + ")";
    hi.NavigateUrl = "WebForm1.aspx?N=" + e.Item.ItemIndex;
   }
  }
 }
}

HTML代码部分:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication10.WebForm1" %>

上一页下一页
 <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication10.WebForm1" %> <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication10.WebForm1" %> <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication10.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <asp:image id="Image1" style="Z-INDEX: 102; LEFT: 232px; POSITION: absolute; TOP: 40px" runat="server"
     Width="296px" Height="224px"></asp:image><asp:datalist id="DataList1" style="Z-INDEX: 103; LEFT: 64px; POSITION: absolute; TOP: 328px"
     runat="server" Width="784px" Height="56px" RepeatColumns="10" Font-Size="12px">
     <ItemTemplate>
      <asp:HyperLink id="lnkpic" Runat="server"></asp:HyperLink>
     </ItemTemplate>
    </asp:datalist><asp:hyperlink id="HyperLink1" style="Z-INDEX: 104; LEFT: 272px; POSITION: absolute; TOP: 304px"
     runat="server">上一页</asp:hyperlink><asp:hyperlink id="HyperLink2" style="Z-INDEX: 105; LEFT: 392px; POSITION: absolute; TOP: 304px"
     runat="server">下一页</asp:hyperlink></FONT></form>
 </body>
</HTML>
上一页下一页
上一页下一页
上一页下一页<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication10.WebForm1" %> 上一页下一页