public static string CreateThumbnail(this WebImage image, string oldImagePath, string serverPath, string folder) { if (image.Width > 240) { image.Resize(240, 240, preserveAspectRatio: true, preventEnlarge: true); } var imgName = Path.GetFileNameWithoutExtension(image.FileName).ToUrlString() + Path.GetExtension(image.FileName); //Add GUID var guidID = SequentialGuid.NewGuid(); var srvrImg = Path.Combine(serverPath, guidID + "_" + imgName); image.Save(srvrImg); if (System.IO.File.Exists(oldImagePath)) { System.IO.File.Delete(oldImagePath); } imgName = Path.Combine(folderRoot, folder, guidID + "_" + imgName); return(imgName); }
public static Media CreateMedia(this HttpPostedFileBase file) { if (file == null) { return(new Media()); } //get file information string type; var folder = folderRoot; int len = file.ContentLength; var guidId = SequentialGuid.NewGuid(); var filetype = file.ContentType; var name = Path.GetFileNameWithoutExtension(file.FileName).ToUrlString("-"); var filename = name + Path.GetExtension(file.FileName); if (filetype.ToLower().Contains("image")) { type = "Image"; folder += "Images"; } else if (filetype.ToLower().Contains("video") || filetype.ToLower().Contains("movie")) { type = "Video"; folder += "Videos"; } else { type = "Document"; folder += "Documents"; } //save file info var m = new Media() { UrlParameter = name, IsActive = true, Type = type, Name = filename, ContentType = filetype, Size = len, Title = Path.GetFileNameWithoutExtension(file.FileName), Description = Path.GetFileName(file.FileName) + ": " + file.ContentType, Extension = Path.GetExtension(file.FileName), Directory = Path.Combine(folder, name) }; if (m.Type == "Image") { using (var image = Image.FromStream(file.InputStream, true, true)) { var maindirectory = Path.Combine(folder, name); var newname = guidId + Path.GetExtension(file.FileName); //Get New Image Resized and Save var org = GetNewImage <Original>(image, new Size(), maindirectory, "", newname); var std = GetNewImage <Standard>(image, new Size(800, 600), maindirectory, "Standard", newname); var thb = GetNewImage <Thumbnail>(image, new Size(160, 160), maindirectory, "Thumbnail", newname); image.Dispose(); //Get General Image Ratio m.Thumbnail = thb; m.Standard = std; m.Original = org; } } else { //Add GUID & save file m.Directory = Path.Combine(folder, filename); file.SaveAs(Path.Combine(HttpContext.Current.Server.MapPath(folder), filename)); } return(m); }