/// <summary> /// 上传商品图片 /// </summary> /// <returns></returns> public JsonResult UploadImage(bool isTemp = false, int cutWidth = 0) { //相对路径 string relativePath = string.Empty; string url = string.Empty; int width = 0, height = 0; HttpPostedFileBase fileData = Request.Files[0]; if (!fileData.HasFile()) { return(Json(new ImageView() { Error = 1, Message = "上載的圖片无效!" })); } if (fileData.ContentLength > (2 * 1024 * 1024)) { return(Json(new ImageView() { Error = 1, Message = "上載的圖片尺寸不能超过2M!!" })); } try { string uploadFileName = fileData.FileName; string imageExtension = Path.GetExtension(uploadFileName); if (!ValidateImageExtension(imageExtension)) { return(Json(new ImageView() { Error = 1, Message = "不允許上載“" + imageExtension + "”格式的圖片!" })); } else { string relativeDir = PathHelper.GetSaveSjPathImg(); if (isTemp)//保存到临时路径下 { relativeDir = PathHelper.GetSavePathTemp(); } CheckOrCreateDirectory(ConfigHelper.SharePath + relativeDir); var fileName = StringHelper.GetRandomString(12); //保存原图 relativePath = relativeDir + fileName + imageExtension; var absolutePath = ConfigHelper.SharePath + relativePath; fileData.SaveAs(absolutePath); //重新压缩图片 if (fileData.ContentLength > (1024 * 500))//如果大于500k,那么就需要重新压缩图片了 { string relativeDirNew = PathHelper.GetSaveSjPathImg();; if (isTemp)//保存到临时路径下 { relativeDirNew = PathHelper.GetSavePathTemp(); } CheckOrCreateDirectory(ConfigHelper.SharePath + relativeDirNew); fileName = StringHelper.GetRandomString(12); string relativePathNew = relativeDirNew + fileName + imageExtension; string absolutePathnew = ConfigHelper.SharePath + relativePathNew; string urlNew = ConfigHelper.ImageServer + relativePathNew.Replace('\\', '/'); ReCompressImage(absolutePath, absolutePathnew); relativePath = relativePathNew; absolutePath = absolutePathnew; url = urlNew; } //这个为了剪切功能生成图片大小 Bitmap originalImg = new Bitmap(absolutePath); width = originalImg.Width; height = originalImg.Height; if (width < 300 || height < 300) { return(Json(new ImageView { Error = 1, Message = "上載的圖片尺寸不能小於300x300" })); } #region 生成缩略图 var thumbsDir = Path.GetDirectoryName(absolutePath); EncoderParameters parms = new EncoderParameters(1); var imgCodecInfo = GetImageCodecInfo(GetImageFormat(imageExtension)); parms.Param[0] = new EncoderParameter(Encoder.Quality, ((long)80)); //切640的图 Bitmap b640 = LTImage.ResizeImage(originalImg, 640, 640, true, true, true); string imgPath640 = thumbsDir + "\\" + fileName + "_640" + imageExtension; b640.Save(imgPath640, imgCodecInfo, parms); b640.Dispose(); //切180的图 Bitmap b180 = LTImage.ResizeImage(originalImg, 180, 180, true, true, true); originalImg.Dispose(); string imgPath180 = thumbsDir + "\\" + fileName + "_180" + imageExtension; b180.Save(imgPath180, imgCodecInfo, parms); b180.Dispose(); #endregion url = ConfigHelper.ImageServer + relativePath.Replace('\\', '/'); } } catch (Exception ex) { // LogHelper.GetLogger(typeof(UtilityController)).Error("上传失败:" + ex.ToString()); return(Json(new ImageView { Error = 1, Message = ex.Message })); } return(Json(new ImageView { Url = url, Path = relativePath, Width = width, Height = height })); }
public string UploadSupplierLogo() { HttpPostedFileBase fileData = Request.Files[0]; int newImgWidth = 0; int newImgHeight = 0; if (!fileData.HasFile()) { return(JsonHelper.ToJson(new ImageView() { Error = 1, Message = "上載的圖片无效!" })); } if (fileData.ContentLength > (512 * 1024)) { return(JsonHelper.ToJson(new ImageView() { Error = 1, Message = "上載的圖片尺寸不能超过500KB!" })); } string uploadFileName = fileData.FileName; string imageExtension = Path.GetExtension(uploadFileName); if (!ValidateImageExtension(imageExtension)) { return(JsonHelper.ToJson(new ImageView() { Error = 1, Message = "不允許上載“" + imageExtension + "”格式的圖片!" })); } string saveFilePath = string.Empty; string saveFileDir = ConfigHelper.SharePath + PathHelper.GetSavePathImg(); if (!Directory.Exists(saveFileDir)) { Directory.CreateDirectory(saveFileDir); } saveFilePath = saveFileDir + StringHelper.GetRandomString(12) + imageExtension; fileData.SaveAs(saveFilePath); Bitmap aImg = new Bitmap(saveFilePath); Bitmap b = LTImage.ResizeImage(aImg, 300, 300); newImgWidth = b.Width; newImgHeight = b.Height; if (fileData.ContentLength > (1024 * 500))//如果大于500k,那么就需要重新压缩图片了 { string saveFileDirNew = ConfigHelper.SharePath + PathHelper.GetSavePathImg(); string saveFilePathNew = saveFileDirNew + StringHelper.GetRandomString(12) + imageExtension; ReCompressImage(saveFilePath, saveFilePathNew); saveFilePath = saveFilePathNew; } string saveImg200 = saveFilePath.Replace(imageExtension, "_218" + imageExtension); SaveProductImageSizeOld(saveFilePath.Replace(ConfigHelper.SharePath, ""), saveImg200, 218, 218); ImageView result = new ImageView() { Url = saveFilePath.Replace(ConfigHelper.SharePath, ConfigHelper.ImageServer).Replace("\\", "//"), Path = saveFilePath.Replace(ConfigHelper.SharePath, ""), }; return(JsonHelper.ToJson(result)); }
private ImageView CutImageAndReturnPath(bool isCut, string imgsrc, double x, double y = 0, double w = 150, double h = 150) { Bitmap originalImg = new Bitmap(imgsrc); int origWidth = originalImg.Width; int origHeight = originalImg.Height; Bitmap newImg = LTImage.ResizeImage(originalImg, 300, 300, true, false); int newWidth = newImg.Width; int newHeight = newImg.Height; if (!isCut) { x = 0; //(newWidth - 150) / 2; if (newWidth < newHeight) { w = newWidth; h = newWidth; } else { w = newHeight; h = newHeight; } } //转换系数 double zoom = Math.Round((double)originalImg.Width / (double)newImg.Width, 2); //转换后新坐标 Point p = new Point(Convert.ToInt32(x * zoom), Convert.ToInt32(y * zoom)); //转换后新截图范围 Size s = new Size(Convert.ToInt32(w * zoom), Convert.ToInt32(h * zoom)); if (originalImg.Width > p.X + s.Width && originalImg.Height > p.Y + s.Height) { if (s.Width < 218 && s.Height < 218) { s.Width = s.Height = 218; } } Bitmap newCutImg = LTImage.Cut(originalImg, p.X, p.Y, s.Width, s.Height); var avatarDir = Path.GetDirectoryName(imgsrc); var ext = ".jpg";//Path.GetExtension(imgsrc); EncoderParameters parms = new EncoderParameters(1); var jpgCodecInfo = GetImageCodecInfo(ImageFormat.Jpeg); EncoderParameter parm = new EncoderParameter(Encoder.Quality, ((long)80)); parms.Param[0] = parm; //if (origWidth < 640 || origHeight < 640) //{ Bitmap b640 = LTImage.ResizeImage(newCutImg, 640, 640, true, true); string big640 = avatarDir + @"\img_0" + ext; b640.Save(big640, jpgCodecInfo, parms); //} //else //{ Bitmap b320 = LTImage.ResizeImage(newCutImg, 320, 320, true, true); string big320 = avatarDir + @"\img_320" + ext; b320.Save(big320, jpgCodecInfo, parms); //} Bitmap b218 = LTImage.ResizeImage(newCutImg, 218, 218, true, true); Bitmap b100 = LTImage.ResizeImage(newCutImg, 100, 100, true, true); Bitmap b350 = LTImage.ResizeImage(newCutImg, 350, 350, true, true); string imginit = avatarDir + @"\img_0_init" + ext; string bigAvatarDir = avatarDir + @"\img_0_218" + ext; string middleAvatarDir = avatarDir + @"\img_0_100" + ext; string storePAvatarDir = avatarDir + @"\img_0_350" + ext; //用户上传的图片重新命名 if (!System.IO.File.Exists(imginit)) { originalImg.Save(imginit, jpgCodecInfo, parms); } //切218的图 b218.Save(bigAvatarDir, jpgCodecInfo, parms); //切100的图 b100.Save(middleAvatarDir, jpgCodecInfo, parms); b350.Save(storePAvatarDir, jpgCodecInfo, parms); originalImg.Dispose(); newImg.Dispose(); newCutImg.Dispose(); b218.Dispose(); b100.Dispose(); b350.Dispose(); return(new ImageView { Height = origHeight, Width = origWidth, Name = Path.GetFileName(imgsrc), Url = imgsrc.Replace(ConfigHelper.SharePath, ConfigHelper.ImageServer).Replace('\\', '/') + "?v=" + DateTime.Now.ToString("HHMMddss"), Path = imgsrc.Replace(ConfigHelper.SharePath, "") }); }