/// <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); }
/// <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); }
/// <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); }
/// <summary> /// Applies the color of the background. /// /// </summary> /// <param name="args">The arguments.</param><param name="imageFormat">The image format.</param><param name="transformationOptions">The transformation options.</param> protected virtual void ApplyBackgroundColor(GetMediaStreamPipelineArgs args, ImageFormat imageFormat, CustomTransformationOptions transformationOptions) { Assert.ArgumentNotNull((object)args, "args"); Assert.ArgumentNotNull((object)imageFormat, "imageFormat"); Assert.ArgumentNotNull((object)transformationOptions, "transformationOptions"); if (!transformationOptions.BackgroundColor.IsEmpty || imageFormat != ImageFormat.Bmp && imageFormat != ImageFormat.Gif && imageFormat != ImageFormat.Jpeg) return; transformationOptions.BackgroundColor = Settings.Media.DefaultImageBackgroundColor; }
/// <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; }
/// <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 }; }
/// <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; }
/// <summary> /// Applies the color of the background. /// /// </summary> /// <param name="args">The arguments.</param><param name="imageFormat">The image format.</param><param name="transformationOptions">The transformation options.</param> protected virtual void ApplyBackgroundColor(GetMediaStreamPipelineArgs args, ImageFormat imageFormat, CustomTransformationOptions transformationOptions) { Assert.ArgumentNotNull((object)args, "args"); Assert.ArgumentNotNull((object)imageFormat, "imageFormat"); Assert.ArgumentNotNull((object)transformationOptions, "transformationOptions"); if (!transformationOptions.BackgroundColor.IsEmpty || imageFormat != ImageFormat.Bmp && imageFormat != ImageFormat.Gif && imageFormat != ImageFormat.Jpeg) { return; } transformationOptions.BackgroundColor = Settings.Media.DefaultImageBackgroundColor; }
/// <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); }
/// <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); } }
private Stream CropLegacy(Stream inputStream, CustomTransformationOptions options, ImageFormat outputFormat) { throw new NotImplementedException(); }