缩略图的生成

这个博客展示了如何在ASP.NET中生成缩略图。通过读取数据库中的图片,利用System.Drawing.Image类创建缩略图,并将其保存到服务器上的特定路径。缩略图在用户界面上作为链接展示,点击后跳转到详细信息页面。

namespace ServerFlat.UserCtrl
{
 using System;
 using System.Data;
 using System.Drawing;
 using System.Web;
 using System.Web.UI.WebControls;
 using System.Web.UI.HtmlControls;

 /// <summary>
 ///  Newfocus 的摘要说明。
 /// </summary>
 public class Newfocus : System.Web.UI.UserControl
 {
  protected System.Data.SqlClient.SqlConnection sqlCon;
  protected System.Data.SqlClient.SqlCommand sqlCommand1;
  protected System.Web.UI.WebControls.Image ImageSource;
  protected System.Web.UI.WebControls.HyperLink HyperLink1;
  protected System.Web.UI.WebControls.HyperLink HyperLink2;
  protected System.Web.UI.WebControls.HyperLink HyperLink3;
  //定义变量
  public int Id2;
  public int Id1;
  public int Id3;
  protected string imagename;
  //protected string mpath;
  //定义image类的对象, GDI画图类
  System.Drawing.Image Image ,NewImage;
  protected System.Web.UI.WebControls.ImageButton ImgBtt1;
  protected System.Web.UI.WebControls.ImageButton ImgBtt2;
  protected System.Web.UI.WebControls.ImageButton ImgBtt3;
  
  //protected System.Web.UI.WebControls.Label Label1;
  //提供一个回调方法,用于确定Image对象在执行生成缩略图操作时何时提前取消执行
  //如果此方法确定 GetThumbnailImage 方法应提前停止执行,则返回 true;否则返回 false
  System.Drawing.Image.GetThumbnailImageAbort callb=null;

  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
      this.sqlCon.Open();
   this.ImageSource.Visible=false;//隐藏原图
   System.Data.SqlClient.SqlDataReader  dr=sqlCommand1.ExecuteReader();
   if (dr.Read ())
    {
   for (int i=0; i<3; i++)
   {
    
     this.ImageSource.ImageUrl=dr["PicturePath"].ToString();
     
     //获取图片名称
     imagename=ImageSource.ImageUrl.Substring(this.ImageSource.ImageUrl.LastIndexOf("//")+1);
     //为图片建立引用
     Image=System.Drawing.Image.FromFile(Server.MapPath(this.ImageSource.ImageUrl));
     //生成缩略图
     NewImage=Image.GetThumbnailImage(100,100,callb,IntPtr.Zero);
     //保存缩略图在虚拟路径上
     NewImage.Save(Request.PhysicalApplicationPath+"small//"+imagename,System.Drawing.Imaging.ImageFormat.Jpeg);
     //释放空间
     Image.Dispose();
     NewImage.Dispose();
     //显示缩略图
     if (i==0)
     {  
      Id1= System.Convert.ToInt16 (dr["ID"].ToString ());      
      this.HyperLink1.Text =dr["PicDescribe"].ToString();
      this.HyperLink1.NavigateUrl="../WebForm/infoview.aspx?ID="+Id1;      
      this.ImgBtt1.ImageUrl=Request.PhysicalApplicationPath+"small//"+imagename;
     
     }
     if (i==1)
     {
      Id2= System.Convert.ToInt16 (dr["ID"].ToString ());      
      this.HyperLink2.Text=dr["PicDescribe"].ToString();
      this.HyperLink2.NavigateUrl="../WebForm/infoview.aspx?ID="+Id2;
      this.ImgBtt2.ImageUrl=Request.PhysicalApplicationPath+"small//"+imagename;
     }
     if (i==2)
     {
      Id3= System.Convert.ToInt16 (dr["ID"].ToString ());      
      this.HyperLink3.Text=dr["PicDescribe"].ToString();
      this.HyperLink3.NavigateUrl="../WebForm/infoview.aspx?ID="+Id3;
      this.ImgBtt3.ImageUrl=Request.PhysicalApplicationPath+"small//"+imagename;
      
     }
    
    dr.Read(); 
   }
   } 
       dr.Close();
       sqlCon.Close();
   }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  ///  设计器支持所需的方法 - 不要使用代码编辑器
  ///  修改此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();
   this.sqlCon = new System.Data.SqlClient.SqlConnection();
   this.sqlCommand1 = new System.Data.SqlClient.SqlCommand();
   this.ImgBtt1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
   this.ImgBtt2.Click += new System.Web.UI.ImageClickEventHandler(this.ImgBtt2_Click);
   this.ImgBtt3.Click += new System.Web.UI.ImageClickEventHandler(this.ImgBtt3_Click);
   //
   // sqlCon
   //
   this.sqlCon.ConnectionString = ((string)(configurationAppSettings.GetValue("ConnString", typeof(string))));
   this.sqlCon.InfoMessage += new System.Data.SqlClient.SqlInfoMessageEventHandler(this.sqlCon_InfoMessage);
   //
   // sqlCommand1
   //
   this.sqlCommand1.CommandText = "[sp_GetPicture]";
   this.sqlCommand1.CommandType = System.Data.CommandType.StoredProcedure;
   this.sqlCommand1.Connection = this.sqlCon;
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void sqlCon_InfoMessage(object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e)
  {
  
  }
  private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  {
   Response.Redirect("../WebForm/infoview.aspx?ID="+Id1);
  }

  private void ImgBtt2_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  {
    Response.Redirect("../WebForm/infoview.aspx?ID="+Id2);
  }

  private void ImgBtt3_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  {
   Response.Redirect("../WebForm/infoview.aspx?ID="+Id3);
  }

 
  

  
 

 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值