Creating Thumbnail Images on the fly with ASP.Net
http://west-wind.com/weblog/posts/283.aspxGenerating Thumbnails on the Fly Using ASP.NET!
http://www.developer.com/net/asp/article.php/3098311

解决方案 »

  1.   

    生成缩略图
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Drawing.Imaging;
    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 TipsTricks.Ch4
    {
    /// <summary>
    /// Summary description for ShowSmallImage.
    /// </summary>
    public class ShowSmallImage : System.Web.UI.Page
    {
    const int MaxLength=150;  //最大长度 private void Page_Load(object sender, System.EventArgs e)
    {
    if (Request.QueryString["filename"] != null)
    {
    //取得原图
    string filename=Request.QueryString["filename"];
    Bitmap bmpOld= new Bitmap(Server.MapPath("images/" + filename));  //计算缩小比例
    double d1;
    if (bmpOld.Height>bmpOld.Width)
    d1=(double)(MaxLength/(double)bmpOld.Width);
    else
    d1=(double)(MaxLength/(double)bmpOld.Height); //产生缩图
    Bitmap bmpThumb= new Bitmap(bmpOld,(int)(bmpOld.Width*d1),(int)(bmpOld.Height*d1)); // 清除缓冲 
    Response.Clear();
    //生成图片
    bmpThumb.Save(Response.OutputStream, ImageFormat.Jpeg);
    Response.End();
    //释放资源
    bmpThumb.Dispose();
    bmpOld.Dispose();
    }
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
    this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion
    }
    }
      

  2.   

    引用
    <asp:Image id=Image2 runat="server" ImageUrl='<%# "GetThumbnail.aspx?filename=" + Server.UrlEncode(DataBinder.Eval(Container, "DataItem.filename").ToString()) %>' AlternateText='<%# "文件名称:" + DataBinder.Eval(Container, "DataItem.filename") + "\n文件尺寸:" + DataBinder.Eval(Container, "DataItem.size") + " bytes" %>'></asp:Image>
    using System;
    using System.Collections;
    using System.ComponentModel;
    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 TipsTricks.Ch4
    {
    /// <summary>
    /// Summary description for ListImage.
    /// </summary>
    public class ListImage : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataList DataList1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!IsPostBack)
    {
    //获取文件名称
    string[] files=Directory.GetFiles(Server.MapPath("images"));
    //建立数据表
    DataTable dt=new DataTable();
    dt.Columns.Add("filename");
    dt.Columns.Add("size"); foreach (string s in files)
    {
    DataRow dr=dt.NewRow();
        FileInfo f=new FileInfo(s);
    dr["filename"]=f.Name;
    dr["size"]=f.Length;
    dt.Rows.Add(dr);
    }
    //绑定显示
    this.DataList1.DataSource=dt;
    this.DataList1.DataBind();
    }
    } public void HHH(object sender, System.EventArgs e)
    {
    this.Response.Write("hello");
    }
    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    }
    }