当前位置:编程学习 > asp >>

asp.net实现图片上传,缩略图,水印

/*Title:单身男女

*Author:Insun

*Blog:http://yxmhero1989.blog.163.com

*From:http://web087429.s2.eiisp.com.cn/forum.php

*/

 

0x00.首先来写简单的图片上传。

利用FileUpload控件,添加Button事件,定义好上传图片的许可形式,label显示一些图片信息,用try。。catch。。格式处理可能异常。首先要建立一个图片文件夹images,保存后的文件名我们下面的例子里面设为yyyyMMddhhmmssfff时间,避免冲突。

//-----------------------------------------------------------------------------------------------

//--代码的确很简单。儿童都能看懂,是吧?

 

protected void Button1_Click(object sender, EventArgs e) //不足在于 同名会覆盖,不会重命名!!!考虑修改!!
    {
        bool fileIsValid = false;
        string strImageName = "";
        string Publishtime = DateTime.Now.ToString();

        if(this.FileUpload1.HasFile)
        {
            String fileExtension = System.IO.Path.GetExtension(this.FileUpload1.FileName).ToLower();
            String[] restrictExtension = {".gif",".jpg",".bmp",".png"};
            for (int i = 0;i < restrictExtension.Length;i++)
            {
                if(fileExtension == restrictExtension[i])
                {
                    fileIsValid = true;
                }
            }
            if (fileIsValid == true)
            {
                try
                {
                    //方案一:利用GUID结构生成唯一文件名
                    //方案二:利用时间生成新文件名(年+月+日+小时+分钟+秒+毫秒)

                    string SaveName = DateTime.Now.ToString("yyyyMMddhhmmssfff");//根据时间生成图片名

                    strImageName = SaveName + this.FileUpload1.PostedFile.FileName.Substring(this.FileUpload1.PostedFile.FileName.LastIndexOf("."));//图片名加上图片后缀名
                    //string strpath = Server.MapPath("") + "\UpImages\" + strImageName;//得到将要保存图片的路径

                    this.Image1.ImageUrl = "~/images/" + strImageName;
                    this.FileUpload1.SaveAs(Server.MapPath("~/images/") + strImageName);//需要建立image目录;若不建立请删掉~/images/"
                    //this.FileUpload1.SaveAs(Server.MapPath("~/images/") + FileUpload1.FileName);//需要建立image目录;若不建立请删掉~/images/"
                    //图片名在上传时改为当前的时间,这样在图片多的时候不至于重复而覆盖掉原来的图片,还控制了图片的大小,在你选择正确的图片时,图片将显示在IMAGE控件里。
                    this.Label1.Text = "文件上传成功!";
                    this.Label1.Text += "<br/>";
                    this.Label1.Text += "<li>" +"原文件路径: " + this.FileUpload1.PostedFile.FileName;
                    this.Label1.Text += "<br/>";
                    this.Label1.Text += "<li>" +"文件大小: " + this.FileUpload1.PostedFile.ContentLength + "字节";
                    this.Label1.Text += "<br/>";
                    this.Label1.Text += "<li>" +"文件类型: " + this.FileUpload1.PostedFile.ContentType;
                    this.Label1.Text += "<br/>";
                    this.Label1.Text += "<li>" + "InputStream: " + this.FileUpload1.PostedFile.InputStream;
                    this.Label1.Text += "<br/>";
                    this.Label1.Text += "<li>" + "HashCode: " + this.FileUpload1.PostedFile.GetHashCode();
                    this.Label1.Text += "<br/>";
                    this.Label1.Text += "<li>" + "操作时间: " + SaveName;
                }
                catch
                {
                    this.Label1.Text = "文件上传不成功!";
                }
            }
      &nb

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,