zoukankan      html  css  js  c++  java
  • 上传图片

      public int UpdateImgs(FileUpload FileUpload1, HttpServerUtility server, int id)
            {
                //如果不是图片文件
                if (!isImg(FileUpload1))
                    return 0;
                string fileName = FileUpload1.FileName;
                string FileFullName = FileUpload1.PostedFile.FileName;
                string type = Path.GetExtension(FileUpload1.FileName);//文件类型
                string sImgName = id + "s" + type;//小封面文件名
                string bImgName = id + "b" + type;//大封面文件名
                string sSaveImgPath = server.MapPath("~/BooksImages/" + sImgName.Trim());
                string bSaveImgPath = server.MapPath("~/BooksImages/" + bImgName.Trim());
                //图片转为流
                Stream st = FileUpload1.PostedFile.InputStream;
                Bitmap SmalBt = (Bitmap)System.Drawing.Image.FromStream(st);
                Bitmap BigBt = (Bitmap)System.Drawing.Image.FromStream(st);
                st.Close();
                //改变图片大小
                Bitmap SmalBm = new Bitmap(SmalBt, new Size(150, 180));
                Bitmap BigBm = new Bitmap(BigBt, new Size(350, 350));
                //保存图片
                SmalBm.Save(sSaveImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                BigBm.Save(bSaveImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                return BookDAL.UpdateImgs(id, sImgName, bImgName);
            }
            //是否为图片文件
            public bool isImg(FileUpload FileUpload1)
            {
                bool fileOK = false;
                string fileName = FileUpload1.FileName;//上传文件名称            
                String[] Extensions = { ".gif", ".png", ".jpeg", ".jpg", ".bmp" };//允许的扩展名
                if (FileUpload1.HasFile)
                {
                    String fileExtension = Path.GetExtension(fileName).ToLower();
                    for (int i = 0; i < Extensions.Length; i++)
                    {
                        if (fileExtension == Extensions[i])
                        {
                            fileOK = true;
                        }
                    }
                }
                return fileOK;
            }

  • 相关阅读:
    【PHP】window系统中设置计划任务,定时调用某接口
    【php】在laravel中使用 easy-wechat实现企业付款到零钱
    【转载】laravel中使用WangEditor及多图上传
    [PHP] curl: (60) SSL certificate problem: unable to get local issuer certificate
    阿里云服务器win10 访问服务器图片资源提示 401
    【PHP】创瑞短信接口
    C#中Lock锁的对象选择问题
    TCP三次握手,四次挥手异常情况(坑)
    C# Hashtable、HashSet和Dictionary的区别
    浅析C# Dictionary实现原理
  • 原文地址:https://www.cnblogs.com/xiaowei-blog/p/4281741.html
Copyright © 2011-2022 走看看