/// <summary> /// 图片缩放(外部调用该方法) /// </summary> /// <param name="filePath">源文件路径</param> /// <param name="basetpath">缩略图保存路径</param> /// <param name="fileName">文件名称</param> /// <param name="targetWidth">图片宽度</param> /// <param name="targetHeight">图片高度</param> public static void ImageZoomAuto(string filePath, string ThumbnailPath, string fileName, double targetWidth = 200, double targetHeight = 134) { FileStream fs = null; using (fs = new FileStream(filePath, FileMode.OpenOrCreate)) { if (null == fs) { return; } if (!System.IO.Directory.Exists(ThumbnailPath)) { System.IO.Directory.CreateDirectory(ThumbnailPath); } PTImage.ZoomAuto(fs, ThumbnailPath + fileName, targetWidth, targetHeight, string.Empty, string.Empty); } }
/// <summary> /// 上传图片 /// </summary> /// <param name="upImg">文件流</param> /// <param name="maxSize">限制图片最大值kb</param> /// <param name="maxWidth">像素最大宽度</param> /// <param name="maxHeight">像素最大高度</param> /// <param name="isThumbnail">是否生成缩略图(1:生成等比缩放的;2:不生成;3:生成非等比缩放的)</param> /// <param name="targetWidth">缩略后的宽度</param> /// <param name="targetHeight">缩略后的高度</param> /// <returns></returns> public static string UploadImg(ref string errorStr, HttpPostedFileBase upImg, int maxSize = 1000000, int maxWidth = 100000, int maxHeight = 100000, int isThumbnail = 2, double targetWidth = 50, double targetHeight = 50) { if (upImg == null || upImg.ContentLength == 0) { return(""); } //验证文件格式 string allowedImage = "gif,img,imge"; if (!CheckFileType(upImg, allowedImage)) { errorStr = "图片格式不正确!"; return(""); } //验证文件大小 int ImageMaxSize = 2048; if (!CheckFileSize(upImg, ImageMaxSize)) { errorStr = "上传图片大小不能超过" + ImageMaxSize + "KB!"; return(""); } //验证图片宽高 if (!CheckImgWidthAndHeight(upImg, maxWidth, maxHeight)) { errorStr = "图片格式有误,请上传[" + maxWidth + "*" + maxHeight + "]以内的图片!"; return(""); } //后缀 string extName = Path.GetExtension(upImg.FileName); //文件名称 string fileName = GetNewFileName() + extName; //文件保存Web相对路劲 string dirWebPath = "/upload/"; //文件保存物理绝对路劲 string dirAbsolutePath = DirFile.GetServerPath(dirWebPath); try { //正式上传 Upload(upImg, dirAbsolutePath, fileName); //缩略图保存路径 string ThumbnailAbsolutePath = dirAbsolutePath + "Thumbnail/"; if (isThumbnail == 1) { //生成缩略图片 PTImage.ImageZoomAuto(dirAbsolutePath + fileName, ThumbnailAbsolutePath, fileName, targetWidth, targetHeight); } else if (isThumbnail == 3) { PTImage.Compress(dirAbsolutePath + fileName, ThumbnailAbsolutePath, (int)targetWidth, (int)targetHeight); } return(dirWebPath + fileName); } catch (Exception ex) { return(""); } }