public static ImagePaths SaveThumbnail(string originalImagePath, int thumbSize = DEFAULT_THUMB_SIZE, int imgQuality = 95) { var origImg = new KalikoImage(originalImagePath); //Create thumbnail name from original image name //var ext = Path.GetExtension(originalImagePath); //var imgWOExt = Path.GetFileNameWithoutExtension(originalImagePath); var newThumbName = GetUniqueFileName(originalImagePath, "thumb");// String.Format("{0}_thumb.{1}", imgWOExt, ext); //var origFileName = Path.GetFileName(originalImagePath); var newThumbPath = GetNewFullPath(originalImagePath, newThumbName); // originalImagePath.Replace(origFileName, newThumbName); var splitPath = originalImagePath.Split(@"\".ToCharArray()); var unixStyleFolderPortion = new StringBuilder(); for (var i = 0; i < splitPath.Length; i++) { if (i == 0) { continue; } //We don't want the drive letter if (i == splitPath.Length - 1) { break; } //we don't want the fileName either unixStyleFolderPortion.AppendFormat("{0}/", splitPath[i]); //we just need the folder paths } var dateFolder = splitPath[splitPath.Length - 2]; origImg.GetThumbnailImage(thumbSize, thumbSize, ThumbnailMethod.Crop).SavePNG(newThumbPath, imgQuality); //var webAddress = String.Concat(ConfigurationManager.AppSettings["BaseDomain"], unixStyleFolderPortion.ToString(), newThumbName); //var webAddress = String.Concat(ConfigurationManager.AppSettings["BaseDomain"], "Uploads/", dateFolder, slash, newThumbName); var webAddress = String.Concat(ConfigurationManager.AppSettings["BaseDomain"], "Uploads/", newThumbName); //var sqlAddress = String.Format("{0}{1}", unixStyleFolderPortion.ToString(), newThumbName); //var sqlAddress = String.Format("Uploads/{0}{1}{2}", dateFolder, slash, newThumbName); var sqlAddress = String.Format("Uploads/{0}", newThumbName); var paths = new ImagePaths(webAddress, sqlAddress, newThumbPath); return paths; }
private void GenerateThumbnail(StorageFile file, IList<StorageFile> results) { if (file.Data.Length > 0) { using (MemoryStream stream = new MemoryStream(file.Data)) { using (MemoryStream output = new MemoryStream()) { try { KalikoImage image = new KalikoImage(stream); int width = 60; int height = 60; image.BackgroundColor = Color.White; KalikoImage thumb = image.GetThumbnailImage(width, height, ThumbnailMethod.Pad); thumb.SaveJpg(output, 75); FileInfo info = new FileInfo(file.Filename); StorageFile thumbnail = new StorageFile(); thumbnail.Filename = info.Name.Replace(info.Extension, "") + "-thumb.jpg"; thumbnail.Data = output.ToArray(); results.Add(thumbnail); } catch (Exception) { throw new ArgumentException("The uploaded content is not supported. Must be a valid image type (png,jpeg or gif)"); } } } } }