zoukankan      html  css  js  c++  java
  • 上传图片并生成图片缩略图

      1using System;
      2using System.Collections;
      3using System.ComponentModel;
      4using System.Data;
      5using System.Drawing;
      6using System.Web;
      7using System.Web.SessionState;
      8using System.Web.UI;
      9using System.Web.UI.WebControls;
     10using System.Web.UI.HtmlControls;
     11
     12namespace MyTest
     13{
     14    /// <summary>
     15    /// 生成图片缩略图 的摘要说明。
     16    /// </summary>

     17    public class 生成图片缩略图 : System.Web.UI.Page
     18    {
     19        protected System.Web.UI.HtmlControls.HtmlInputFile upImage;
     20        protected System.Web.UI.WebControls.Button btnUp;
     21        protected System.Web.UI.WebControls.Image imageSource;
     22        protected System.Web.UI.WebControls.Image imageSmall;
     23        //定义image类的对象
     24        System.Drawing.Image image,newimage;
     25        //图片路径
     26        protected string imagePath;
     27        //图片类型
     28        protected string imageType;
     29        //图片名称
     30        protected string imageName;
     31
     32        //提供一个回调方法,用于确定Image对象在执行生成缩略图操作时何时提前取消执行
     33        //如果此方法确定GetThumbnailImage方法应提前停止执行,则返回true;否则返回false
     34        System.Drawing.Image.GetThumbnailImageAbort callb = null;
     35    
     36        private void Page_Load(object sender, System.EventArgs e)
     37        {
     38            // 在此处放置用户代码以初始化页面
     39        }

     40
     41        Web 窗体设计器生成的代码
     62
     63        //"上传并生成缩略图"按钮的单击事件
     64        private void btnUp_Click(object sender, System.EventArgs e)
     65        {
     66            string mPath;
     67            if(this.upImage.PostedFile.FileName != "")
     68            {
     69                imagePath = upImage.PostedFile.FileName;
     70                imageType = imagePath.Substring(imagePath.LastIndexOf(".")+1);//取图片类型
     71                imageName = imagePath.Substring(imagePath.LastIndexOf("\\")+1);//取图片名称
     72
     73                if(imageType!= "jpg" && imageType!="gif")
     74                {
     75                    Response.Write("<script>alert('对不起!请您选择JPG或者GIF格式的图片!')</script>");
     76                    return;
     77                }

     78                else
     79                {
     80                    try
     81                    {
     82                        //建立虚拟路径
     83                        mPath = Server.MapPath("upFile");
     84                        //保存到虚拟路径
     85                        upImage.PostedFile.SaveAs(mPath+"\\"+imageName);
     86                        //显示原图
     87                        imageSource.ImageUrl = "upFile/"+imageName;
     88                        //为上传的图片建立引用
     89                        image = System.Drawing.Image.FromFile(mPath+"\\"+imageName);
     90                        //生成缩略图 取原图的1/10高,宽。
     91                        newimage = image.GetThumbnailImage(image.Width/10,image.Height/10,callb,new System.IntPtr());
     92                        //把缩略图保存到指定的虚拟路径
     93                        newimage.Save(Server.MapPath("upFile")+"\\small"+imageName);
     94                        //释放image对象占用的资源
     95                        image.Dispose();
     96                        //释放newimage对象的资源
     97                        newimage.Dispose();
     98                        //显示缩略图
     99                        imageSmall.ImageUrl="upFile/"+"small"+imageName;
    100                        Response.Write("上传成功!");
    101                    }

    102                    catch
    103                    {
    104                        Response.Write("上传失败!");
    105                    }

    106                }

    107            }

    108        }

    109
    110    }

    111}

    112
  • 相关阅读:
    python ModuleNotFoundError: No module named 'requests' 的 解决方案
    Win环境下如何在cmd运行python文件
    阿里云ECS服务器连接RDS数据库
    mysql5.6采集数据插入出现MySQL server has gone away解决办法
    Ubuntu 18.04 单系统U盘安装
    查看ubuntu系统是32位还是64位,查看系统版本
    Ubuntu 18.04 设置固定的静态ip
    Ubuntu 18.04 新系统 允许root远程登录设置方法
    ubuntu 新系统 使用root用户登录
    Ubuntu 18.04远程登录服务器--ssh的安装和配置
  • 原文地址:https://www.cnblogs.com/Lewis/p/449094.html
Copyright © 2011-2022 走看看