public ImageMediaFormats(IImageFormatSpec[] specs) { Checks.Argument.IsNotNull(specs, "specs"); Checks.Argument.IsNotEmpty(specs, "specs"); try { var duplicationMsgFormat = "ImageMediaFormats can only accept a mutually exclusive array of IImageFormatSpec. The array provided has a duplicate property value."; var specCount = specs.Length; //make sure all IImageFormatSpec objects are unqiue - this could be done with a single comparison against all properties but the error message wouldn't be as specific if (specs.DistinctBy(s => s.GetHashCode()).Count() != specCount) { //log.Fatal("An exception was thrown while trying to instantiate a ImageMediaFormats object."); throw new ArgumentException(duplicationMsgFormat); } _specs = specs.ToDictionary<IImageFormatSpec, Guid>(x => x.Format.Guid); } catch (ArgumentNullException ex) { //log.Error("An exception was thrown while trying to instantiate a ImageMediaFormats object.", ex); throw; } catch (ArgumentException ex) { //log.Error("An exception was thrown while trying to instantiate a ImageMediaFormats object. Make sure you are not specifying IImageFormatSpec object with the same Format as this is not permitted.", ex); throw; } }
public void Save(IPhoto tempImageStorage, IImageFormatSpec mediaFormat, out bool success) { success = false; Checks.Argument.IsNotNull(tempImageStorage, "tempImageStorage"); Checks.Argument.IsNotNull(mediaFormat, "mediaFormat"); Checks.Argument.IsNotNull(_tempImageStorageRepository, "_tempImageStorageRepository"); //ValidateTempImageStorage(tempImageStorage, out validationState); //if (!validationState.IsValid) //{ // return; //} if (null == this.GetImage(tempImageStorage.PhotoId)) { try { _tempImageStorageRepository.Add(tempImageStorage); success = true; } catch (Exception ex) { success = false; //var valState = new ValidationState(); //valState.Errors.Add(new ValidationError("Id.AlreadyExists", tempImageStorage, ex.Message)); //validationState.Append(typeof(ITempImageStorage), valState); } } }
//consider changing the out parameter to a validation type object //public void Save(IPhoto photo, out bool success) //{ // Checks.Argument.IsNotNull(photo, "photo"); // success = false; // if (null == _repo.FindByPhotoId(photo.PhotoId)) // { // try // { // _repo.Add(photo); // success = true; // } // catch (Exception ex) // { // success = false; // } // } //} public void Save(IPhoto photo, IImageFormatSpec mediaFormat, out bool success) { success = false; Checks.Argument.IsNotNull(photo, "photo"); Checks.Argument.IsNotNull(mediaFormat, "mediaFormat"); //ValidateTempImageStorage(tempImageStorage, out validationState); //if (!validationState.IsValid) //{ // return; //} if (null == this.GetPhoto(photo.PhotoId)) { try { _repo.Add(photo); success = true; } catch (Exception ex) { success = false; //var valState = new ValidationState(); //valState.Errors.Add(new ValidationError("Id.AlreadyExists", tempImageStorage, ex.Message)); //validationState.Append(typeof(ITempImageStorage), valState); } } }
//NEED THIS ONE TO BE IMPLEMENTED //public IList<ITempImageStorage> GetImageByUserId(Guid userId) //{ // return _tempImageStorageRepository.FindByUserName(userName); //} //public IList<ITempImageStorage> GetOlderThan(DateTime date) //{ // return _tempImageStorageRepository.FindByOlderThan(date); //} #region ITempImageStorageService Members public void SaveCommit(ITempImageStorage tempImageStorage, IImageFormatSpec mediaFormat, out bool success) { using (var unitOfWork = UnitOfWork.Begin()) { Save(tempImageStorage, mediaFormat, out success); if (success) { unitOfWork.Commit(); } } }
public void SaveCommit(IPhoto photo, IImageFormatSpec mediaFormat, out bool success) { using (var unitOfWork = UnitOfWork.Begin()) { Save(photo, mediaFormat, out success); if (success) { unitOfWork.Commit(); } } }
/// <summary> /// Resizes image /// </summary> /// <param name="imageBytes"></param> /// <param name="NewWidth"></param> /// <param name="MaxHeight"></param> /// <param name="OnlyResizeIfWider"></param> /// <param name="imageFormat"></param> /// <returns></returns> public byte[] ResizeImage(byte[] imageBytes, int NewWidth, int MaxHeight, bool OnlyResizeIfWider, IImageFormatSpec imageFormatSpec) { using (var ms = new MemoryStream(imageBytes)) { System.Drawing.Image FullsizeImage = System.Drawing.Image.FromStream(ms); // Prevent using images internal thumbnail FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); if (OnlyResizeIfWider) { if (FullsizeImage.Width <= NewWidth) { NewWidth = FullsizeImage.Width; } } int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width; if (NewHeight > MaxHeight) { // Resize with height instead NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height; NewHeight = MaxHeight; } System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero); // Clear handle to original file so that we can overwrite it if necessary FullsizeImage.Dispose(); // Save resized picture using (var msOut = new MemoryStream()) { NewImage.Save(msOut, imageFormatSpec.Format); return msOut.GetBuffer(); } } }
/// <summary> /// Resizes image /// </summary> /// <param name="imageBytes"></param> /// <param name="imageSize"></param> /// <param name="imageFormatSpec"></param> /// <returns></returns> public byte[] ResizeImage(byte[] imageBytes, IImageSizes imageSize, IImageFormatSpec imageFormatSpec) { return this.ResizeImage(imageBytes, imageSize.Width, imageSize.MaxHeight, imageSize.OnlyResizeIfWider, imageFormatSpec); }
/// <summary> /// Resizes image /// </summary> /// <param name="imageBytes"></param> /// <param name="imageSize"></param> /// <param name="imageFormatSpec"></param> /// <returns></returns> public byte[] ResizeImage(byte[] imageBytes, IImageSizes imageSize, IImageFormatSpec imageFormatSpec) { return(this.ResizeImage(imageBytes, imageSize.Width, imageSize.MaxHeight, imageSize.OnlyResizeIfWider, imageFormatSpec)); }
/// <summary> /// Resizes image /// </summary> /// <param name="imageBytes"></param> /// <param name="NewWidth"></param> /// <param name="MaxHeight"></param> /// <param name="OnlyResizeIfWider"></param> /// <param name="imageFormat"></param> /// <returns></returns> public byte[] ResizeImage(byte[] imageBytes, int NewWidth, int MaxHeight, bool OnlyResizeIfWider, IImageFormatSpec imageFormatSpec) { using (var ms = new MemoryStream(imageBytes)) { System.Drawing.Image FullsizeImage = System.Drawing.Image.FromStream(ms); // Prevent using images internal thumbnail FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); if (OnlyResizeIfWider) { if (FullsizeImage.Width <= NewWidth) { NewWidth = FullsizeImage.Width; } } int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width; if (NewHeight > MaxHeight) { // Resize with height instead NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height; NewHeight = MaxHeight; } System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero); // Clear handle to original file so that we can overwrite it if necessary FullsizeImage.Dispose(); // Save resized picture using (var msOut = new MemoryStream()) { NewImage.Save(msOut, imageFormatSpec.Format); return(msOut.GetBuffer()); } } }