上传图片

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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。