예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="destFilePath"></param>
        /// <param name="destFileName"></param>
        /// <param name="cropOptions"></param>
        /// <param name="resizeOptions"></param>
        /// <param name="compressOptions"></param>
        public void ProcessAndSaveImageFromStream(Stream stream, string destFilePath, string destFileName,
                                                  ImageCropOpts cropOptions, ImageResizeOpts resizeOptions, ImageCrompressOpts compressOptions, ImageCropOpts maxDimensionsAftersize)
        {
            stream.Seek(0, SeekOrigin.Begin);

            using (var image = Image.FromStream(stream))
            {
                //-- Set the processed image to the original image incase we don't apply any cropping or resizing we'll save the image as is.
                Image processedImg = image;
                if (cropOptions != null)
                {
                    processedImg = ImageCropper.Crop(processedImg, cropOptions);
                }
                if (resizeOptions != null)
                {
                    processedImg = ImageResizer.ResizeImage(processedImg, resizeOptions);
                }

                //-- Clip the width or height if it's still too large after resized
                if (maxDimensionsAftersize != null)
                {
                    if ((maxDimensionsAftersize.W != 0) && (processedImg.Width > maxDimensionsAftersize.W))
                    {
                        var widthCropOptions = new ImageCropOpts(0, 0, maxDimensionsAftersize.W, processedImg.Height);
                        processedImg = ImageCropper.Crop(processedImg, widthCropOptions);
                    }
                    if ((maxDimensionsAftersize.H != 0) && (processedImg.Height > maxDimensionsAftersize.H))
                    {
                        var heightCropOptions = new ImageCropOpts(0, 0, processedImg.Width, maxDimensionsAftersize.H);
                        processedImg = ImageCropper.Crop(processedImg, heightCropOptions);
                    }
                }

                //-- TODO: think about putting in a max dimensions crop, where if an image is resized by exact width or height, we clip
                //-- the corresponding height or width
                //if (maxDimensionsAftersize != null) { }

                using (var compressedImgStream = ImageCompressor.CompressImage(processedImg, compressOptions))
                {
                    //-- If there's been no cropping or resizing & the original image size is small enough
                    //-- then we're going to save the original stream to avoid image quality degradation
                    if (processedImg.Height == image.Height && processedImg.Width == processedImg.Width)
                    {
                        bool originalImageSizeUnderThreshold = (stream.Length < compressOptions.OriginalByteLengthThreshold);

                        //-- If it's under the threshold then we just use the original image would compounding the compression
                        if (originalImageSizeUnderThreshold)
                        {
                            stream.CopyTo(compressedImgStream); //http://stackoverflow.com/questions/230128/best-way-to-copy-between-two-stream-instances-c-sharp
                            stream.Position = compressedImgStream.Position = 0;

                            ImagePersister.SaveImage(compressedImgStream, destFilePath, destFileName);
                            return;
                        }
                    }

                    ImagePersister.SaveImage(compressedImgStream, destFilePath, destFileName);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Save an image to the temp file store and reduce it to reasonable size so we don't kill our server by reducing bandwidth & storage
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="fileName"></param>
        public void SaveTempImage(Stream stream, string fileName)
        {
            //-- We using save media images as 640x640 max so we give 720 to allow for some cropping
            var resizeOpts = new ImageResizeOpts(720, 720, ResizeMode.ShrinkOnly);

            ProcessAndSaveImageFromStream(stream, TempPath, fileName, null, resizeOpts, ImageCrompressOpts.TempImage, null);
        }
예제 #3
0
 /// <summary>
 /// Take an existing image somewhere on the internet and save it using the destination path/name, crop and resize options.
 /// </summary>
 /// <param name="originalImgUrl"></param>
 /// <param name="destFilePath"></param>
 /// <param name="destFileName"></param>
 /// <param name="cropOptions"></param>
 /// <param name="resizeOptions"></param>
 /// <remarks>
 /// This method is useful when saving images from "web" (external sites). It is also useful during the upload / cropping process
 /// </remarks>
 public void ProcessAndSaveImageFromWebUrl(string originalImgUrl, string destFilePath, string destFileName,
                                           ImageCropOpts cropOptions, ImageResizeOpts resizeOptions, ImageCrompressOpts compressOptions)
 {
     using (Stream imageStream = new ImageDownloader().DownloadImageAsStream(originalImgUrl))
     {
         ProcessAndSaveImageFromStream(imageStream, destFilePath, destFileName, cropOptions, resizeOptions, compressOptions, null);
     }
 }
예제 #4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="destFilePath"></param>
        /// <param name="destFileName"></param>
        /// <param name="cropOptions"></param>
        /// <param name="resizeOptions"></param>
        /// <param name="compressOptions"></param>
        public void ProcessAndSaveImageFromStream(Stream stream, string destFilePath, string destFileName, 
            ImageCropOpts cropOptions, ImageResizeOpts resizeOptions, ImageCrompressOpts compressOptions, ImageCropOpts maxDimensionsAftersize)
        {
            stream.Seek(0, SeekOrigin.Begin);

            using (var image = Image.FromStream(stream))
            {
                //-- Set the processed image to the original image incase we don't apply any cropping or resizing we'll save the image as is.
                Image processedImg = image;
                if (cropOptions != null) { processedImg = ImageCropper.Crop(processedImg, cropOptions); }
                if (resizeOptions != null) { processedImg = ImageResizer.ResizeImage(processedImg, resizeOptions); }

                //-- Clip the width or height if it's still too large after resized
                if (maxDimensionsAftersize != null)
                {
                    if ((maxDimensionsAftersize.W != 0) && (processedImg.Width > maxDimensionsAftersize.W)) {
                        var widthCropOptions = new ImageCropOpts(0, 0, maxDimensionsAftersize.W, processedImg.Height);
                        processedImg = ImageCropper.Crop(processedImg, widthCropOptions);
                    }
                    if ((maxDimensionsAftersize.H != 0) && (processedImg.Height > maxDimensionsAftersize.H)) {
                        var heightCropOptions = new ImageCropOpts(0, 0, processedImg.Width, maxDimensionsAftersize.H);
                        processedImg = ImageCropper.Crop(processedImg, heightCropOptions);
                    }
                }

                //-- TODO: think about putting in a max dimensions crop, where if an image is resized by exact width or height, we clip
                //-- the corresponding height or width
                //if (maxDimensionsAftersize != null) { }

                using (var compressedImgStream = ImageCompressor.CompressImage(processedImg, compressOptions))
                {
                    //-- If there's been no cropping or resizing & the original image size is small enough
                    //-- then we're going to save the original stream to avoid image quality degradation
                    if (processedImg.Height == image.Height && processedImg.Width == processedImg.Width)
                    {
                        bool originalImageSizeUnderThreshold = (stream.Length < compressOptions.OriginalByteLengthThreshold);

                        //-- If it's under the threshold then we just use the original image would compounding the compression
                        if (originalImageSizeUnderThreshold)
                        {
                            stream.CopyTo(compressedImgStream); //http://stackoverflow.com/questions/230128/best-way-to-copy-between-two-stream-instances-c-sharp
                            stream.Position = compressedImgStream.Position = 0;

                            ImagePersister.SaveImage(compressedImgStream, destFilePath, destFileName);
                            return;
                        }
                    }

                    ImagePersister.SaveImage(compressedImgStream, destFilePath, destFileName);
                }
            }
        }
예제 #5
0
        /// <summary>
        /// Resize a given image based on resize options with checks that the resizing options are valid and do not cause Overflow exceptions etc.
        /// </summary>
        /// <param name="img"></param>
        /// <param name="o"></param>
        /// <returns></returns>
        public static Image ResizeImage(Image img, ImageResizeOpts o)
        {
            var error = string.Empty;

            if (o == null)
            {
                error = "Cannot resize image with null resize options...";
            }
            else if (o.Mode == ResizeMode.ExactWidth && o.Width == 0)
            {
                error = string.Format("Cannot resize image with w[{0}] on width only resize. Resize with a 0px dimension is not possible.", o.Width);
            }
            else if (o.Mode == ResizeMode.ExactHeight && o.Height == 0)
            {
                error = string.Format("Cannot resize image with h[{0}]. Resize with a 0px dimension is not possible.", o.Height);
            }
            else if (o.Mode == ResizeMode.ShrinkOnly && (o.Height == 0 && o.Width == 0))
            {
                error = string.Format("Cannot resize image with w[{0}]h[{1}]. Resize with a 0px dimension is not possible.", o.Width, o.Height);
            }

            if (!string.IsNullOrEmpty(error))
            {
                throw new ArgumentException(error);
            }

            //-- If we're only wanting to shrink the image if it's too big & it's already bigger than the desired dimensions, we don't need to do anything
            if (o.Mode == ResizeMode.ShrinkOnly && (img.Width < o.Width && img.Height < o.Height))
            {
                return(img);
            }
            else if (o.Mode == ResizeMode.ExactWidth)
            {
                return(GetResizedImage(img, o.Width, 0));
            }
            else if (o.Mode == ResizeMode.ExactHeight)
            {
                return(GetResizedImage(img, 0, o.Height));
            }
            else
            {
                return(GetResizedImage(img, o.Width, o.Height));
            }
        }
예제 #6
0
        /// <summary>
        /// Resize a given image based on resize options with checks that the resizing options are valid and do not cause Overflow exceptions etc.
        /// </summary>
        /// <param name="img"></param>
        /// <param name="o"></param>
        /// <returns></returns>
        public static Image ResizeImage(Image img, ImageResizeOpts o)
        {
            var error = string.Empty;
            if (o == null) { error = "Cannot resize image with null resize options..."; }
            else if (o.Mode == ResizeMode.ExactWidth && o.Width == 0) { error = string.Format("Cannot resize image with w[{0}] on width only resize. Resize with a 0px dimension is not possible.", o.Width); }
            else if (o.Mode == ResizeMode.ExactHeight && o.Height == 0) { error = string.Format("Cannot resize image with h[{0}]. Resize with a 0px dimension is not possible.", o.Height); }
            else if (o.Mode == ResizeMode.ShrinkOnly && (o.Height == 0 && o.Width ==0)) { error = string.Format("Cannot resize image with w[{0}]h[{1}]. Resize with a 0px dimension is not possible.", o.Width, o.Height); }

            if (!string.IsNullOrEmpty(error)) { throw new ArgumentException(error); }

            //-- If we're only wanting to shrink the image if it's too big & it's already bigger than the desired dimensions, we don't need to do anything
            if (o.Mode == ResizeMode.ShrinkOnly && (img.Width < o.Width && img.Height < o.Height)) { return img; }
            else if (o.Mode == ResizeMode.ExactWidth) { return GetResizedImage(img, o.Width, 0); }
            else if (o.Mode == ResizeMode.ExactHeight) { return GetResizedImage(img, 0, o.Height); }
            else
            {
                return GetResizedImage(img, o.Width, o.Height);
            }
        }
예제 #7
0
        /// <summary>
        /// Save a thumbnail of an image with x/y dimensions specified by thumbW & thumbH
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="destFilePath"></param>
        /// <param name="destFileName"></param>
        /// <param name="thumbW"></param>
        /// <param name="thumbH"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="compressOptions"></param>
        private void SaveThumbImageWithResizeThenCrop(Stream stream, string destFilePath, string destFileName, int thumbW, int thumbH,
            ImageCropOpts cropOpts, ImageCrompressOpts compressOptions)
        {
            var thumbPixelBuffer = 0;
            ImageResizeOpts resizeOpts = new ImageResizeOpts() { Mode = ResizeMode.ShrinkOnly, Height = thumbW + thumbPixelBuffer, Width = thumbH + thumbPixelBuffer };

            stream.Seek(0, SeekOrigin.Begin);
            using (var image = Image.FromStream(stream))
            {
                //-- Set the processed image to the original image incase we don't apply any cropping or resizing we'll save the image as is.
                Image processedImg = image;

                //-- Do the initial crop
                if (cropOpts != null) { processedImg = ImageCropper.Crop(image, cropOpts); }

                //-- If our cropped image dimensions are bigger than the Thumb buffer dimensions
                if (processedImg.Width > resizeOpts.Width && processedImg.Height > resizeOpts.Height)
                {
                    processedImg = ImageResizer.ResizeImage(processedImg, resizeOpts);
                }

                //-- In case the image width is smaller than the desired thumb width, we enlarge it
                if (processedImg.Width < thumbW)
                {
                    //-- If the image is smaller we first enlarge the image based of width, if the height is still to small we enlarge it again
                    var enlargeWidthOptions = new ImageResizeOpts() { Mode = ResizeMode.ExactWidth, Height = 0, Width = thumbH };

                    //-- First try the width
                    processedImg = ImageResizer.ResizeImage(processedImg, enlargeWidthOptions);
                }

                //-- In case the image height is smaller than the desired Thumb height, we enlarge it
                if (processedImg.Height < thumbH)
                {
                    ImageResizeOpts enlargeHeightOptions = new ImageResizeOpts() { Mode = ResizeMode.ExactHeight, Height = thumbW, Width = 0 };
                    processedImg = ImageResizer.ResizeImage(processedImg, enlargeHeightOptions);
                }

                //-- If needed, crop the width to be exact
                if (processedImg.Width > thumbW)
                {
                    var widthCropOptions = new ImageCropOpts(0, 0, thumbW, processedImg.Height);
                    processedImg = ImageCropper.Crop(processedImg, widthCropOptions);
                }

                //-- If needed, crop the height to be exact
                if (processedImg.Height > thumbH)
                {
                    var heightCropOptions = new ImageCropOpts(0, 0, processedImg.Width, thumbH);
                    processedImg = ImageCropper.Crop(processedImg, heightCropOptions);
                }

                //-- Save the thumb compressed heavily
                using (var compressedImage = ImageCompressor.CompressImage(processedImg, compressOptions))
                {
                    ImagePersister.SaveImage(compressedImage, destFilePath, destFileName);
                }
            }
        }
예제 #8
0
        /// <summary>
        /// Save an image to the temp file store and reduce it to reasonable size so we don't kill our server by reducing bandwidth & storage
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="fileName"></param>
        public void SaveTempImage(Stream stream, string fileName)
        {
            //-- We using save media images as 640x640 max so we give 720 to allow for some cropping
            var resizeOpts = new ImageResizeOpts(720, 720, ResizeMode.ShrinkOnly);

            ProcessAndSaveImageFromStream(stream, TempPath, fileName, null, resizeOpts, ImageCrompressOpts.TempImage, null);
        }
예제 #9
0
 /// <summary>
 /// Take an existing image somewhere on the internet and save it using the destination path/name, crop and resize options.
 /// </summary>
 /// <param name="originalImgUrl"></param>
 /// <param name="destFilePath"></param>
 /// <param name="destFileName"></param>
 /// <param name="cropOptions"></param>
 /// <param name="resizeOptions"></param>
 /// <remarks>
 /// This method is useful when saving images from "web" (external sites). It is also useful during the upload / cropping process
 /// </remarks>
 public void ProcessAndSaveImageFromWebUrl(string originalImgUrl, string destFilePath, string destFileName, 
     ImageCropOpts cropOptions, ImageResizeOpts resizeOptions, ImageCrompressOpts compressOptions)
 {
     using (Stream imageStream = new ImageDownloader().DownloadImageAsStream(originalImgUrl))
     {
         ProcessAndSaveImageFromStream(imageStream, destFilePath, destFileName, cropOptions, resizeOptions, compressOptions, null);
     }
 }
예제 #10
0
        /// <summary>
        /// Save a thumbnail of an image with x/y dimensions specified by thumbW & thumbH
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="destFilePath"></param>
        /// <param name="destFileName"></param>
        /// <param name="thumbW"></param>
        /// <param name="thumbH"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="compressOptions"></param>
        private void SaveThumbImageWithResizeThenCrop(Stream stream, string destFilePath, string destFileName, int thumbW, int thumbH,
                                                      ImageCropOpts cropOpts, ImageCrompressOpts compressOptions)
        {
            var             thumbPixelBuffer = 0;
            ImageResizeOpts resizeOpts       = new ImageResizeOpts()
            {
                Mode = ResizeMode.ShrinkOnly, Height = thumbW + thumbPixelBuffer, Width = thumbH + thumbPixelBuffer
            };

            stream.Seek(0, SeekOrigin.Begin);
            using (var image = Image.FromStream(stream))
            {
                //-- Set the processed image to the original image incase we don't apply any cropping or resizing we'll save the image as is.
                Image processedImg = image;

                //-- Do the initial crop
                if (cropOpts != null)
                {
                    processedImg = ImageCropper.Crop(image, cropOpts);
                }

                //-- If our cropped image dimensions are bigger than the Thumb buffer dimensions
                if (processedImg.Width > resizeOpts.Width && processedImg.Height > resizeOpts.Height)
                {
                    processedImg = ImageResizer.ResizeImage(processedImg, resizeOpts);
                }

                //-- In case the image width is smaller than the desired thumb width, we enlarge it
                if (processedImg.Width < thumbW)
                {
                    //-- If the image is smaller we first enlarge the image based of width, if the height is still to small we enlarge it again
                    var enlargeWidthOptions = new ImageResizeOpts()
                    {
                        Mode = ResizeMode.ExactWidth, Height = 0, Width = thumbH
                    };

                    //-- First try the width
                    processedImg = ImageResizer.ResizeImage(processedImg, enlargeWidthOptions);
                }

                //-- In case the image height is smaller than the desired Thumb height, we enlarge it
                if (processedImg.Height < thumbH)
                {
                    ImageResizeOpts enlargeHeightOptions = new ImageResizeOpts()
                    {
                        Mode = ResizeMode.ExactHeight, Height = thumbW, Width = 0
                    };
                    processedImg = ImageResizer.ResizeImage(processedImg, enlargeHeightOptions);
                }

                //-- If needed, crop the width to be exact
                if (processedImg.Width > thumbW)
                {
                    var widthCropOptions = new ImageCropOpts(0, 0, thumbW, processedImg.Height);
                    processedImg = ImageCropper.Crop(processedImg, widthCropOptions);
                }

                //-- If needed, crop the height to be exact
                if (processedImg.Height > thumbH)
                {
                    var heightCropOptions = new ImageCropOpts(0, 0, processedImg.Width, thumbH);
                    processedImg = ImageCropper.Crop(processedImg, heightCropOptions);
                }

                //-- Save the thumb compressed heavily
                using (var compressedImage = ImageCompressor.CompressImage(processedImg, compressOptions))
                {
                    ImagePersister.SaveImage(compressedImage, destFilePath, destFileName);
                }
            }
        }