Beispiel #1
0
        /// <summary>
        /// Resizes an image represented by a stream.
        ///
        /// </summary>
        /// <param name="inputStream">The input stream.</param><param name="options">The options.</param><param name="outputFormat">The output format.</param>
        /// <returns>
        /// The image stream.
        /// </returns>
        public Stream CropImageStream(Stream inputStream, CustomTransformationOptions options, ImageFormat outputFormat)
        {
            Assert.ArgumentNotNull(inputStream, "inputStream");
            Assert.ArgumentNotNull(options, "options");
            Assert.ArgumentNotNull(outputFormat, "outputFormat");

            if (inputStream.Length <= Settings.Media.MaxSizeInMemory)
            {
                if (options.CropRegion == null || options.CropRegion.Count() != 4)
                {
                    return(null);
                }
                var stream   = new MemoryStream();
                var newImage = new Cropper().Crop(new Bitmap(inputStream), options, outputFormat);
                newImage.Save(stream, outputFormat);

                stream.Seek(0L, SeekOrigin.Begin);

                newImage.Dispose();

                return(stream);
            }
            Tracer.Error("Could not crop image stream as it was larger than the maximum size allowed for memory processing.");
            return(null);
        }
Beispiel #2
0
 /// <summary>
 /// Transforms an image stream.
 ///
 /// </summary>
 /// <param name="inputStream">The stream containing the media data.</param><param name="options">The image options.</param><param name="outputFormat">The image format of the resulting thumbnail image.</param>
 /// <returns/>
 public virtual Stream TransformImageStream(Stream inputStream, CustomTransformationOptions options, ImageFormat outputFormat)
 {
     Assert.ArgumentNotNull(inputStream, "inputStream");
     Assert.ArgumentNotNull(options, "options");
     Assert.ArgumentNotNull(outputFormat, "outputFormat");
     return(CropImageStream(inputStream, options, outputFormat));
 }
Beispiel #3
0
 /// <summary>
 /// Crops the specified original image. Determines image format and calls appropriate methods for jpeg and gif image formats.
 /// </summary>
 /// <param name="originalImage">The original image.</param>
 /// <param name="options">The options.</param>
 /// <param name="outputFormat">The output format.</param>
 /// <returns>Cropped bitmap</returns>
 public Bitmap Crop(Bitmap originalImage, CustomTransformationOptions options, ImageFormat outputFormat)
 {
     if (outputFormat == ImageFormat.Gif)
     {
         return(this.CropGif(originalImage, options));
     }
     return(this.CropAny(originalImage, options));
 }
Beispiel #4
0
        /// <summary>
        /// Gets the rectangle.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        private Rectangle GetRectangle(Image image, CustomTransformationOptions options)
        {
            if (options.CropRegion == null && options.CropRegion.Length != 4)
            {
                return(new Rectangle(0, 0, image.Width, image.Height));
            }
            var _x1 = 0;
            var _y1 = 0;
            var _x2 = 0;
            var _y2 = 0;

            var _x1Convert = int.TryParse(options.CropRegion[0], out _x1) ? _x1 : 0;
            var _y1Convert = int.TryParse(options.CropRegion[1], out _y1) ? _y1 : 0;
            var _x2Convert = int.TryParse(options.CropRegion[2], out _x2) ? _x2 : 0;
            var _y2Convert = int.TryParse(options.CropRegion[3], out _y2) ? _y2 : 0;

            if (_x2 > image.Width)
            {
                _x2 = image.Width;
            }

            if (_y2 > image.Height)
            {
                _y2 = image.Height;
            }

            if (_x1 > image.Width)
            {
                _x1 = 0;
            }

            if (_y1 > image.Height)
            {
                _y1 = 0;
            }

            if (_x1 < 0)
            {
                _x1 = 0;
            }
            if (_y1 < 0)
            {
                _y1 = 0;
            }

            var newWidth  = _x2 - _x1;
            var newHeight = _y2 - _y1;

            return(new Rectangle(_x1, _y1, newWidth, newHeight));
        }
Beispiel #5
0
        /// <summary>
        /// Crops the GIF.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="options">The options.</param>
        /// <returns>Cropped bitmap for GIF image</returns>
        private Bitmap CropGif(Bitmap image, CustomTransformationOptions options)
        {
            int _x1 = 0;
            int _y1 = 0;
            int _x2 = 0;
            int _y2 = 0;

            string[] region = options.CropRegion;
            if (region.Length >= 4)
            {
                _x1 = int.TryParse(region[0], out _x1) ? _x1 : 0;
                _y1 = int.TryParse(region[1], out _y1) ? _y1 : 0;
                _x2 = int.TryParse(region[2], out _x2) ? _x2 : 0;
                _y2 = int.TryParse(region[3], out _y2) ? _y2 : 0;

                if (_x2 > image.Width)
                {
                    _x2 = image.Width;
                }
                if (_y2 > image.Height)
                {
                    _y2 = image.Height;
                }

                if (_x1 < 0)
                {
                    _x1 = 0;
                }
                if (_y1 < 0)
                {
                    _y1 = 0;
                }
            }

            int  newWidth  = _x2 - _x1;
            int  newHeight = _y2 - _y1;
            Size size      = new Size(newWidth, newHeight);

            int colorIndex = this.GetColorIndex(options.BackgroundColor, image);

            if (colorIndex < 0)
            {
                colorIndex = this.ReplaceLeastUsedColor(image, options.BackgroundColor);
            }
            Bitmap frame = this.CreateGifFrame(size, (byte)colorIndex, image);

            return(this.OverlayGif(image, frame, options.CropRegion));
        }
Beispiel #6
0
        /// <summary>
        /// Crops image if it's format is not GIF.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="options">The options.</param>
        /// <returns>Cropped bitmap.</returns>
        private Bitmap CropAny(Image image, CustomTransformationOptions options)
        {
            try
            {
                //cropping
                var bmpImage  = new Bitmap(image);
                var rectangle = GetRectangle(image, options);
                var newImage  = bmpImage.Clone(rectangle, bmpImage.PixelFormat);

                return(newImage);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message, this);
                return(new Bitmap(image));
            }
        }
Beispiel #7
0
 /// <summary>
 /// Gets the crop options.
 /// </summary>
 /// <param name="options">The options.</param>
 /// <returns></returns>
 protected virtual CustomTransformationOptions GetCropOptions(CustomTransformationOptions options)
 {
     return(new CustomTransformationOptions
     {
         AllowStretch = options.AllowStretch,
         BackgroundColor = options.BackgroundColor,
         IgnoreAspectRatio = options.IgnoreAspectRatio,
         MaxSize = options.MaxSize,
         Scale = options.Scale,
         Size = options.Size,
         PreserveResolution = options.PreserveResolution,
         CompositingMode = options.CompositingMode,
         InterpolationMode = options.InterpolationMode,
         PixelOffsetMode = options.PixelOffsetMode,
         CropRegion = options.CropRegion
     });
 }
Beispiel #8
0
        /// <summary>
        /// Gets the image options.
        ///
        /// </summary>
        ///
        /// <returns>
        /// The transformation options.
        ///
        /// </returns>
        public new CustomTransformationOptions GetTransformationOptions()
        {
            var options = new CustomTransformationOptions()
            {
                AllowStretch      = this.AllowStretch,
                BackgroundColor   = this.BackgroundColor,
                IgnoreAspectRatio = this.IgnoreAspectRatio,
                MaxSize           = new Size(this.MaxWidth, this.MaxHeight),
                Scale             = this.Scale,
                Size = new Size(this.Width, this.Height)
            };

            if (!string.IsNullOrEmpty(this.CropRegion))
            {
                options.CropRegion = this.CropRegion.Split(',');
            }
            return(options);
        }
Beispiel #9
0
 private Stream CropLegacy(Stream inputStream, CustomTransformationOptions options, ImageFormat outputFormat)
 {
     throw new NotImplementedException();
 }