• 上传图片


      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;
            }

  • 相关阅读:
    八、springboot 简单优雅的通过docker-compose 构建
    【并发编程】ThreadLocal其实很简单
    计算机网络
    相似度
    不同激活函数的区别
    快速排序+归并排序
    xgboost
    c++面试
    PCA算法和SVD
    各种排序算法的时间复杂度和空间复杂度
  • 原文地址:https://www.cnblogs.com/xiaowei-blog/p/4281741.html
Copyright © 2020-2023  润新知