private static string GetResizedImagePath(string filepath, int width, int height) { string resizedPath = filepath; if (width > 0 || height > 0) { resizedPath = filepath.GetPathForResizedImage(width, height); if (!Directory.Exists(resizedPath)) { Directory.CreateDirectory(new FileInfo(resizedPath).DirectoryName); } if (!File.Exists(resizedPath)) { var imageResizer = new ImageResizer(filepath); if (width > 0 && height > 0) { imageResizer.Resize(width, height, ImageEncoding.Jpg90); } else if (width > 0) { imageResizer.Resize(width, ImageEncoding.Jpg90); } imageResizer.SaveToFile(resizedPath); imageResizer.Dispose(); } } return(resizedPath); }
private static string GetResizedImagePath(string filepath, int width, int height) { string resizedPath = filepath; if (width > 0 || height > 0) { resizedPath = filepath.GetPathForResizedImage(width, height); if (!Directory.Exists(resizedPath)) Directory.CreateDirectory(new FileInfo(resizedPath).DirectoryName); if (!File.Exists(resizedPath)) { var imageResizer = new ImageResizer(filepath); if (width > 0 && height > 0) { imageResizer.Resize(width, height, ImageEncoding.Jpg90); } else if (width > 0) { imageResizer.Resize(width, ImageEncoding.Jpg90); } imageResizer.SaveToFile(resizedPath); imageResizer.Dispose(); } } return resizedPath; }
/// <summary> /// Gets the resized image path. /// </summary> /// <param name="filepath">The filepath.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <returns></returns> public static string GetResizedImagePath(string filepath, int width, int height) { var resizedPath = filepath; try { if (width > 0 || height > 0) { resizedPath = GetPathForResizedImage(filepath, width, height); if (!Directory.Exists(resizedPath)) { var fileInfo = new FileInfo(resizedPath); if (!string.IsNullOrEmpty(fileInfo.DirectoryName)) { Directory.CreateDirectory(fileInfo.DirectoryName); } } if (!File.Exists(resizedPath)) { var imageResizer = new ImageResizer(filepath); if (width > 0 && height > 0) { imageResizer.Resize(width, height, ImageEncoding.Jpg90); } else if (width > 0) { imageResizer.Resize(width, ImageEncoding.Jpg90); } imageResizer.SaveToFile(resizedPath); imageResizer.Dispose(); } } } catch (Exception ex) { Logger.ErrorFormat("Failed to get resized image path - error: {0}", ex.ToString()); } return(resizedPath); }
/// <summary> /// The method for handling upload a file to server /// </summary> /// <param name="file"> /// The file will be uploaded to server /// </param> /// <param name="maximumWidth"> /// Maximum width /// </param> /// <param name="storagePath"> /// The directory which the file will be stored in /// </param> /// <param name="fileName"> /// fileName will be created based on DateTime.Now /// as binary string and uploaded file extension /// </param> public static bool UploadAndResizeFile(HttpPostedFile file, int maximumWidth, string storagePath, ref string fileName) { var result = true; try { // Get storage upload path var fileInfo = new FileInfo(file.FileName); fileName = DateTime.Now.ToBinary() + fileInfo.Extension; var fullStoragePath = Path.Combine(storagePath, fileName); // Get to be uploaded image width using (Image sourceImage = Image.FromStream(file.InputStream)) { var uploadImageWidth = sourceImage.Width; if (uploadImageWidth > maximumWidth) { var fileByteArray = ConvertImageToByteArray(sourceImage, fileInfo.Extension); var imageResizer = new ImageResizer(fileByteArray); imageResizer.Resize(maximumWidth, ImageEncoding.Jpg90); imageResizer.SaveToFile(fullStoragePath); imageResizer.Dispose(); } else { file.SaveAs(fullStoragePath); } } } catch (Exception ex) { result = false; Logger.ErrorFormat("UploadAndResizeFile: {0}", ex.ToString()); } return(result); }