生成缩略图,采用时间命名

生成缩略图并删除原图,采用时间命名,还可以添加版权信息.我不知道怎么能不用上传原图,只上传缩略图,若有高手知道,请告知,谢谢
程序
这是一个用户控件程序,属性filedername表示上传位置
#region 定义
  protected System.Web.UI.WebControls.RadioButton RadioButton1;
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.Label Label2;
  protected System.Web.UI.WebControls.Label Label3;
  protected System.Web.UI.WebControls.Label lb;
  protected System.Web.UI.WebControls.Label Label4;
  protected System.Web.UI.WebControls.Label Label5;
  protected System.Web.UI.WebControls.Label Label6;
  protected System.Web.UI.WebControls.TextBox tx_imgw;
  protected System.Web.UI.WebControls.TextBox tx_imgh;
  protected System.Web.UI.WebControls.TextBox tx_cr;
  protected System.Web.UI.WebControls.TextBox tx_l;
  protected System.Web.UI.WebControls.TextBox tx_t;
  protected System.Web.UI.WebControls.Button bt_upload;
  protected System.Web.UI.WebControls.Image img_preview;
  protected System.Web.UI.HtmlControls.HtmlInputFile uploadfile;
  protected System.Web.UI.WebControls.RadioButton RadioButton2;
  protected System.Web.UI.WebControls.Literal kj;
        public string filedername=null;
  protected string imgname=null;
  protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
  protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator2;
  protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator3;
  protected System.Web.UI.WebControls.Label lb_ing;
  protected System.Web.UI.WebControls.Label lb_imgname;
  protected System.Web.UI.WebControls.Label lb_success;
  protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator4;
  protected string path=null;
  public bool v;
  #endregion
 
  #region 用户控件属性Getimagenae
  public string Getimagenae
  {
   get
   {return this.lb_imgname.Text;}
   set
   {this.lb_imgname.Text=value;}
  }
  #endregion

  #region  用户控件属性GetImgUrl
  public string GetImgUrl
  {
   get
   {return this.img_preview.ImageUrl;}
   set
   {this.img_preview.ImageUrl=value;}
  }
  #endregion

  #region Page_Load事件
  private void Page_Load(object sender, System.EventArgs e)
  {
   if(!IsPostBack)
   {
    this.img_preview.Visible=this.v;
   }
   this.kj.Visible=false;
   this.lb_success.Visible=false;
  }
  #endregion

  #region 获取缩略图
  private void GetThumbNailImage(int width,int height,int left,int top,string strCr)
  {
   string file=this.filedername+"/"+this.imgname+"."+this.uploadfile.PostedFile.FileName.Substring(this.uploadfile.PostedFile.FileName.LastIndexOf(".")+1);
   string newfile=this.filedername+"/"+this.imgname+"s"+".jpg";
   string strc=zbeinfo.OtherCs.EncodeX(strCr);
   System.Drawing.Image oldimg=System.Drawing.Image.FromFile(Server.MapPath(file));
   System.Drawing.Image thumbnailimg=
     oldimg.GetThumbnailImage(width,height,new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback),IntPtr.Zero);
   Response.Clear();
   Bitmap output=new Bitmap(thumbnailimg);
   Graphics g=Graphics.FromImage(output);
   g.DrawString(strc,new Font("宋体",10),new SolidBrush(Color.White),left,top);
   output.Save(Server.MapPath(newfile),System.Drawing.Imaging.ImageFormat.Jpeg);
   Response.ContentType="image/gif";
   oldimg.Dispose();
   File.Delete(Server.MapPath(file));
   this.lb_imgname.Text=this.imgname+"s"+".jpg";
   this.lb_imgname.Visible=true;
   this.lb_success.Visible=true;
   this.lb_ing.Visible=false;
   this.img_preview.Visible=true;
   this.img_preview.ImageUrl=newfile;
   
  }
  private bool ThumbnailCallback()
  {
   return true;
  }
  #endregion

  #region 获取时间作为图片名称
  private string FileName()
  {
   string filename=null;
   DateTime now;
   now=DateTime.Now;
   filename=now.Year.ToString()+now.Month.ToString()+now.Day.ToString()+now.Hour.ToString()+
            now.Minute.ToString()+now.Second.ToString();
   return filename;

  }
  #endregion

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

  }
  #endregion

  #region 上传按钮事件
  private void bt_upload_Click(object sender, System.EventArgs e)
  {
   if(!(this.uploadfile.PostedFile.ContentLength>0))
   {
    this.kj.Text="alert('没有选择文件!')";
    this.kj.Visible=true;
   }
   else if(this.tx_imgw.Text==""||this.tx_imgh.Text=="")
   {
    this.kj.Text="alert('请输入要生成缩略图的大小')";
    this.kj.Visible=true;
   }
   else
   {  
    this.lb_ing.Visible=true;
    int width=Int32.Parse(this.tx_imgw.Text);
    int heigt=Int32.Parse(this.tx_imgh.Text);
    int left=0;
    int top=0;
    if(this.tx_l.Text!=""&&this.tx_t.Text!="")
    {
     left=Int32.Parse(this.tx_l.Text);
     top=Int32.Parse(this.tx_t.Text);
    }
    string strCr=this.tx_cr.Text;
    this.imgname=FileName();
    this.path=Server.MapPath(this.filedername+this.imgname+"."+this.uploadfile.PostedFile.FileName.Substring(this.uploadfile.PostedFile.FileName.LastIndexOf(".")+1));
    this.uploadfile.PostedFile.SaveAs(path);
    GetThumbNailImage(width,heigt,left,top,strCr);
          }
    
  }
  #endregion 

posted @ 2004-08-20 11:05  火火  阅读(1323)  评论(1编辑  收藏  举报