<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PhotoAlbum.ascx.cs" Inherits="PhotoAlbum" %>
<div style="margin:auto;">
<asp:DataList ID="DataList1" runat="server" RepeatColumns="4" Width="100%">
            <ItemTemplate><div style="width:160px;margin:auto;height:120px;border:1px solid gray;">
            <div>
            <a href="<%#Eval("NormalPhoto")%>" target="_blank"><img style="margin:auto;border:none;padding:5px 5px 5px 5px;" src="<%#Eval("SmallPhoto")%>"  /> </a>
            </div>
        </div>
        
        <div style="color:Gray;text-align:center;height:20px;"><%#Eval("Name") %></div>
        </ItemTemplate>
</asp:DataList>
<div style="margin:auto;height:20px;padding:0px;text-align:right;padding:0px;">
<br />
<asp:LinkButton ID="lbnPrevPage" runat="server" CommandName="prev" OnCommand="Page_OnClick">上一页</asp:LinkButton>
<asp:Label ID="lbCurrentPage" runat="server" Text=""></asp:Label>
&nbsp;&nbsp;<asp:Label ID="lbTotal" runat="server" Text=""></asp:Label>
<asp:LinkButton ID="lbnNextPage" runat="server" CommandName="next" OnCommand="Page_OnClick">下一页</asp:LinkButton>
&nbsp;&nbsp;跳转至第<asp:DropDownList ID="lstPage" runat="server" OnSelectedIndexChanged="lstPage_SelectedIndexChanged1" AutoPostBack="True">
    </asp:DropDownList>页&nbsp;
</div>
</div>
<div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" /><br />
        <asp:Label ID="Label1" runat="server"></asp:Label></div>

解决方案 »

  1.   


    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;using ImageProcess;
    public partial class PhotoAlbum : System.Web.UI.UserControl
    {
        int PageSize = 12;
        int CurrentPage = 0;
        int PageCount = 0;
        int RecordCount = 0;    string smallfoldername = String.Empty;
        string bigfoldername = String.Empty;    protected void Page_Load(object sender, EventArgs e)
        {        try
            {
                smallfoldername = Server.MapPath(ConfigurationManager.AppSettings["SmallPath"]);
                bigfoldername = Server.MapPath(ConfigurationManager.AppSettings["NormalPath"]);
            }
            catch
            {
                smallfoldername = Server.MapPath("./Photo/SmallPics/");
                bigfoldername = Server.MapPath("./Photo/NormalPics/");
            }        RecordCount = ProcessPhoto();        //设定PageSize
            int PageSize = 12;        //第一次请求执行
            if (!Page.IsPostBack)
            {
                CurrentPage = 0;
                ViewState["PageIndex"] = 0;            lbTotal.Text = "共" + RecordCount.ToString() + "张";            //计算总共有多少页
                //PageCount = RecordCount / PageSize;
                PageCount = (RecordCount + PageSize - 1) / PageSize;
                //lblPageCount.Text = PageCount.ToString();
                ViewState["PageCount"] = PageCount;            for (int i = 1; i <= PageCount; i++)
                {
                    lstPage.Items.Add(i.ToString());
                }            ListBind();
            }
        }    DataSet CreateSource()
        {        int StartIndex;        //设定导入的起终地址
            StartIndex = CurrentPage * PageSize;        //取得大图目录信息
            DirectoryInfo imagesfile = new DirectoryInfo(bigfoldername);
            //取得大图目录所有JPG文件集合
            FileInfo[] fi = imagesfile.GetFiles("*.jpg");        //取得小图目录信息        DirectoryInfo Smallimagesfile = new DirectoryInfo(smallfoldername);
            //取得小图目录所有JPG文件集合
            FileInfo[] Sfi = Smallimagesfile.GetFiles("*.jpg");        //通过hashtable绑定小图路径和大图路径
            DataSet ds = new DataSet();
            DataTable tb = new DataTable();
            ds.Tables.Add(tb);        DataColumn colSmall = new DataColumn("SmallPhoto");
            DataColumn colNormal = new DataColumn("NormalPhoto");
            DataColumn colName = new DataColumn("Name");
            ds.Tables[0].Columns.Add(colSmall);
            ds.Tables[0].Columns.Add(colNormal);
            ds.Tables[0].Columns.Add(colName);        if (Sfi.Length - StartIndex < 12)
            {
                for (int i = 0; i < (Sfi.Length - StartIndex); i++)
                {
                    DataRow row = ds.Tables[0].NewRow();
                    row["SmallPhoto"] = "./Photo/SmallPics/" + Sfi[i + StartIndex].Name;
                    row["NormalPhoto"] = "./Photo/NormalPics/" + Sfi[i + StartIndex].Name;
                    row["Name"] = Sfi[i + StartIndex].Name;
                    ds.Tables[0].Rows.Add(row);
                }
            }
            else
            {
                for (int i = 0; i < PageSize; i++)
                {
                    DataRow row = ds.Tables[0].NewRow();
                    row["SmallPhoto"] = "./Photo/SmallPics/" + Sfi[i + StartIndex].Name;
                    row["NormalPhoto"] = "./Photo/NormalPics/" + Sfi[i + StartIndex].Name;
                    row["Name"] = Sfi[i + StartIndex].Name;
                    ds.Tables[0].Rows.Add(row);
                }
            }        return ds;    }
        public void ListBind()
        {        this.DataList1.DataSource = CreateSource();
            this.DataList1.DataBind();        lbnNextPage.Enabled = true;
            lbnPrevPage.Enabled = true;
            if (CurrentPage == ((int)ViewState["PageCount"] - 1)) lbnNextPage.Enabled = false;
            if (CurrentPage == 0) lbnPrevPage.Enabled = false;
            lbCurrentPage.Text = "   第" + (CurrentPage + 1).ToString() + "/" + ViewState["PageCount"].ToString() + "页   ";    }    public void Page_OnClick(Object sender, CommandEventArgs e)
        {
            CurrentPage = (int)ViewState["PageIndex"];
            PageCount = (int)ViewState["PageCount"];        string cmd = e.CommandName;
            //判断cmd,以判定翻页方向
            switch (cmd)
            {
                case "next":
                    if (CurrentPage < (PageCount - 1)) CurrentPage++;
                    break;
                case "prev":
                    if (CurrentPage > 0) CurrentPage--;
                    break;
            }        ViewState["PageIndex"] = CurrentPage;        ListBind();    }
      

  2.   

    上接   private int ProcessPhoto()
        {
            //生成小图目录
            //DirectoryInfo di_smallforder = Directory.CreateDirectory(smallfoldername);        //取得大图目录信息
            DirectoryInfo imagesfile = new DirectoryInfo(bigfoldername);
            //取得大图目录所有JPG文件集合
            FileInfo[] fi = imagesfile.GetFiles("*.jpg");
            for (int i = 0; i < fi.Length; i++)
            {
                if (!File.Exists(smallfoldername + "/" + fi[i].Name))
                {
                    //生成缩略图文件
                    ImageThumbnail.MakeThumbnail(
                                                 bigfoldername + "/" + fi[i].Name,
                                                 smallfoldername + "/" + fi[i].Name,
                                                 150,
                                                 110,
                                                 "Cut"
                                                 );
                }
            }        return fi.Length;
        }    protected void lstPage_SelectedIndexChanged1(object sender, EventArgs e)
        {
            CurrentPage = lstPage.SelectedIndex;
            ViewState["PageIndex"] = CurrentPage;
            ListBind();
        }
    }
    public partial class PhotoAlbum : System.Web.UI.Page
    {
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string fileContentType = FileUpload1.PostedFile.ContentType;
                if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
                {
                    string name = FileUpload1.PostedFile.FileName;                  // 客户端文件路径                FileInfo file = new FileInfo(name);
                    string fileName = file.Name;                                    // 文件名称
                    string fileName_s = "s_" + file.Name;                           // 缩略图文件名称
                    string fileName_sy = "sy_" + file.Name;                         // 水印图文件名称(文字)
                    string fileName_syp = "syp_" + file.Name;                       // 水印图文件名称(图片)
                    string webFilePath = Server.MapPath("file/" + fileName);        // 服务器端文件路径
                    string webFilePath_s = Server.MapPath("file/" + fileName_s);  // 服务器端缩略图路径                if (!File.Exists(webFilePath))
                    {
                        try
                        {
                            FileUpload1.SaveAs(webFilePath);                                // 使用 SaveAs 方法保存文件
                            AddShuiYinWord(webFilePath, webFilePath_sy);
                            AddShuiYinPic(webFilePath, webFilePath_syp, webFilePath_sypf);
                            MakeThumbnail(webFilePath, webFilePath_s, 130, 130, "Cut");     // 生成缩略图方法
                            Label1.Text = "提示:文件“" + fileName + "”成功上传,并生成“" + fileName_s + "”缩略图,文件类型为:" + FileUpload1.PostedFile.ContentType + ",文件大小为:" + FileUpload1.PostedFile.ContentLength + "B";
                        }
                        catch (Exception ex)
                        {
                            Label1.Text = "提示:文件上传失败,失败原因:" + ex.Message;
                        }
                    }
                    else
                    {
                        Label1.Text = "提示:文件已经存在,请重命名后上传";
                    }
                }
                else
                {
                    Label1.Text = "提示:文件类型不符";
                }
            }
        }
        /**/
        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="originalImagePath">源图路径(物理路径)</param>
        /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
        /// <param name="width">缩略图宽度</param>
        /// <param name="height">缩略图高度</param>
        /// <param name="mode">生成缩略图的方式</param>   
        public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
        {
            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);        int towidth = width;
            int toheight = height;        int x = 0;
            int y = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;        switch (mode)
            {
                case "HW"://指定高宽缩放(可能变形)               
                    break;
                case "W"://指定宽,高按比例                   
                    toheight = originalImage.Height * width / originalImage.Width;
                    break;
                case "H"://指定高,宽按比例
                    towidth = originalImage.Width * height / originalImage.Height;
                    break;
                case "Cut"://指定高宽裁减(不变形)               
                    if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                    {
                        oh = originalImage.Height;
                        ow = originalImage.Height * towidth / toheight;
                        y = 0;
                        x = (originalImage.Width - ow) / 2;
                    }
                    else
                    {
                        ow = originalImage.Width;
                        oh = originalImage.Width * height / towidth;
                        x = 0;
                        y = (originalImage.Height - oh) / 2;
                    }
                    break;
                default:
                    break;
            }        //新建一个bmp图片
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);        //新建一个画板
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);        //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;        //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;        //清空画布并以透明背景色填充
            g.Clear(System.Drawing.Color.Transparent);        //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
                new System.Drawing.Rectangle(x, y, ow, oh),
                System.Drawing.GraphicsUnit.Pixel);        try
            {
                //以jpg格式保存缩略图
                bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                originalImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
        }
    }
      

  3.   

    哦忘了这个错误 1 “PhotoAlbum”的分部声明一定不能指定不同的基类 c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\photoalbum_51aspx\58265914\d2d01fe9\App_Web_q-kb5x9t.0.cs 14 行是14的没有列,我就奇怪行14应该没错
      

  4.   

    这句是行14 using System.Web.UI; 
    错误信息应该说我的声明部份有错吧?
      

  5.   

    public partial class PhotoAlbum : System.Web.UI.Page这个分部类和上面的基类不同,所以出错.你用户控件,干嘛继承Page类?
      

  6.   


    不要看这个行号.这是framework运行时生成的
      

  7.   

    中间有很多是注释啦~并不长...copy到VS上修改不就行了吗??
    高手们帮帮手啊....