.net MVC 后台接收图片并保存

///  
        /// 流转bitmap 
        ///  
        /// 要缩小的图片 
        /// 缩小比例 
        /// 缩小后的结果 
        public static Bitmap PercentImage(HttpPostedFileBase file, double percent =1)
        {
            Image srcImage = Image.FromStream(file.InputStream);
            // 缩小后的高度 
            int newH = int.Parse(Math.Round(srcImage.Height * percent).ToString());
            // 缩小后的宽度 
            int newW = int.Parse(Math.Round(srcImage.Width * percent).ToString());
            try
            {
                // 要保存到的图片 
                Bitmap b = new Bitmap(newW, newH);
                Graphics g = Graphics.FromImage(b);
                // 插值算法的质量 
                g.InterpolationMode = InterpolationMode.Default;
                g.DrawImage(srcImage, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel);
                g.Dispose();
                return b;
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// 
        /// 保存图片(bs使用)
        /// 
        /// 请求体
        /// 待保存的文件夹路径
        /// 
        public string UploadImg(HttpRequestBase Request,string path)
        {
            string src = "", res = "";
            int width = 0; int height = 0;
            try
            {
                HttpPostedFileBase file = null;
                HttpFileCollectionBase files = Request.Files;
                for (int i = 0; i < files.Count; i++)
                {
                    file = files[i];
                }
                //判断文件是否为空
                if (file != null)
                {

                    //获取文件类型
                    string fileExtension = System.IO.Path.GetExtension(file.FileName);
                    //自定义文件名(时间+唯一标识符+后缀)
                    string fileName = DateTime.Now.ToString("yyyy-MM-dd") + Guid.NewGuid() + fileExtension;
                    //判断是否存在需要的目录,不存在则创建 
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    //拼接保存文件的详细路径
                    string filePath = path+ fileName;
                    //若扩展名不为空则判断文件是否是指定视频类型
                    if (fileExtension != null)
                    {
                        #region 视频
                        //if ("(.mp4)|(.avi)|(.flv)|(.rmvb)|(.wmv)".Contains(fileExtension))
                        //{
                        //    //保存文件
                        //    file.SaveAs(filePath);
                        //    //拼接返回的Img标签
                        //    src = "/Document/Temp/" + fileName;
                        //    //Shell shell = new ShellClass();
                        //    //文件路径
                        //    //Shell32.Folder folder = shell.NameSpace(filePath.Substring(0, filePath.LastIndexOf("\\")));
                        //    文件名称
                        //    //Shell32.FolderItem folderitem = folder.ParseName(filePath.Substring(filePath.LastIndexOf("\\") + 1));

                        //    //string videotime = folder.GetDetailsOf(folderitem, 21);
                        //    string ffmpegfile = Server.MapPath("~/FFmpeg/ffmpeg.exe");
                        //    //string sourceFile = Server.MapPath("~/UploadFile/20150821113001.mp4");

                        //    FormatConverter fConverter = new FormatConverter();
                        //    string times = fConverter.GetVideoDuration(ffmpegfile, filePath);//获取视频时长

                        //    string[] sArray = times.Split(':');
                        //    int Hours = Convert.ToInt32(sArray[0]);
                        //    int minutes = Convert.ToInt32(sArray[1]);
                        //    int seconds = Convert.ToInt32(sArray[2]);
                        //    int Times = 0;
                        //    if (Hours != 0)
                        //    {
                        //        Times = Hours * 3600 + minutes * 60 + seconds;
                        //    }
                        //    else if (minutes != 0)
                        //    {
                        //        Times = minutes * 60 + seconds;
                        //    }
                        //    else
                        //    {
                        //        Times = seconds;
                        //    }


                        //    if (Convert.ToInt32(Times) > Convert.ToInt32(VIDEOTIME))
                        //    {
                        //        if (System.IO.File.Exists(filePath))
                        //        {
                        //            System.IO.File.Delete(filePath);
                        //        }
                        //        var result = new { code = 0, info = "视频时长不能超过" + VIDEOTIME + "秒" };
                        //        return Json(result);
                        //    }
                        //    else
                        //    {
                        //        type = "1";
                        //    }
                        #endregion

                        if ("(.jpeg)|(.png)|(.gif)|(.jpg)".Contains(fileExtension))
                        {
                            Bitmap bitmap = PercentImage(file);
                            bitmap.Save(filePath);

                            //if (width > 120 && height > 60)
                            //{
                            //    pic.Dispose();
                            //    if (System.IO.File.Exists(filePath))
                            //    {
                            //        System.IO.File.Delete(filePath);
                            //    }
                            //    return Json(new { code = 0, info = "您上传的小图大小必须是像素" + 120 + "px*" + 60 + "px尺寸" });
                            //}
                            res = JsonConvert.SerializeObject(new { code = 1, info = "上传成功", filepath = filePath });
                        }
                        else
                        {
                            res = JsonConvert.SerializeObject(new { code = 0, info = "上传失败,不是图片" });
                        }
                    }
                    else
                    {
                        res = JsonConvert.SerializeObject(new { code = 0, info = "上传失败" });
                    }
                }
                else
                {
                    res = JsonConvert.SerializeObject(new { code = 0, info = "上传失败,扩展名为空" });
                }
            }
            catch (Exception ex)
            {
                res = JsonConvert.SerializeObject(new { code = 0, info = ex.Message });
            }
            return res;
        }

 

你可能感兴趣的:(.net MVC 后台接收图片并保存)