public ImageRetrieveResult RetrieveImage(LayersCmsImage image) { if (image == null || image.Filename == null) throw new ArgumentNullException("image"); try { // Get the full file path String imageFilePath = Path.Combine(_folderPath, image.Filename); // Load the contents of the file at the given path into an Image instance Image img = Image.FromFile(imageFilePath); // Return the successful output return new ImageRetrieveResult() { ErrorMessage = null, Exception = null, Image = img, Successful = true }; } catch (Exception e) { // Report the error return new ImageRetrieveResult() { ErrorMessage = "Unable to retrieve image file from the file system", Exception = e, Image = null, Successful = false }; } }
public ImageSaveResult SaveImage(Image graphic, LayersCmsImage imageData, bool disposeOnComplete) { if (graphic == null) throw new ArgumentNullException("graphic"); if (imageData == null) throw new ArgumentNullException("imageData"); ImageSaveResult output = SaveImage(graphic, imageData); // Dispose of the image (if requested) if (disposeOnComplete) graphic.Dispose(); return output; }
public override Image GetImage(LayersCmsImage imageData, ImageResizeDetails resizeDetails) { if (imageData == null) throw new ArgumentNullException("imageData"); if (resizeDetails == null) throw new ArgumentNullException("resizeDetails"); var settings = new ResizeSettings() {Format = ImageFileExtension}; // If a height AND a width have been specified, resize to the exact width/height combo if (resizeDetails.Height != null && resizeDetails.Width != null) { settings.Height = resizeDetails.Height.Value; settings.Width = resizeDetails.Width.Value; settings.Mode = FitMode.Crop; // If the image is small, use 100% quality, else 90% settings.Quality = (resizeDetails.Height.Value <= SmallImageThreshold && resizeDetails.Width.Value <= SmallImageThreshold ? SmallImageQualityPercent : StandardImageQualityPercent); } else if (resizeDetails.Height != null) { // Only a height has been specified. Proportionally resize the image scaled to the height value settings.Height = resizeDetails.Height.Value; } else if (resizeDetails.Width != null) { // Only a width has been specified. Proportionally resize the image scaled to the width value settings.Width = resizeDetails.Width.Value; } using (ImageRetrieveResult retrievedImage = ImageStore.RetrieveImage(imageData)) { if (retrievedImage.Successful && retrievedImage.Image != null) { // Instantiate a new stream to store the resized image into using (Stream imageStream = new MemoryStream()) { // Let ImageResizer handle the resizing ImageBuilder.Current.Build(retrievedImage.Image, imageStream, settings, true); // Return an Image constructed from the stream return Image.FromStream(imageStream); } } // If the image data is invalid (no image found), return null return null; } }
private ImageSaveResult SaveImage(Image graphic, LayersCmsImage imageData) { if (graphic == null) throw new ArgumentNullException("graphic"); if (imageData == null || imageData.Filename == null) throw new ArgumentNullException("imageData"); try { // Generate a unique filename given the data in the imageData param String newFilename = _fileHelper.GenerateUniqueFilename(imageData.Filename, _folderPath, true); // Save the image file to the file system graphic.Save(newFilename); // Update the LayersCmsImage so it the filename property matches the filename of the saved file imageData.Filename = newFilename; // Return the successful result return new ImageSaveResult() { CmsImage = imageData, ErrorMessage = null, Exception = null, Successful = true }; } catch (Exception e) { // Report the error return new ImageSaveResult() { Exception = e, CmsImage = null, ErrorMessage = "Unable to save image file", Successful = false }; } }
public abstract Image GetImage(LayersCmsImage imageData, ImageResizeDetails resizeDetails);