示例#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 virtual Stream ResizeImageFromStream(Stream inputStream, TransformationOptions options, ImageFormat outputFormat)
        {
            Assert.ArgumentNotNull((object)inputStream, "inputStream");
            Assert.ArgumentNotNull((object)options, "options");
            Assert.ArgumentNotNull((object)outputFormat, "outputFormat");
            ResizeOptions resizeOptions = this.GetResizeOptions(options);

            if (resizeOptions.IsEmpty)
            {
                return(inputStream);
            }
            if (inputStream.Length > Settings.Media.MaxSizeInMemory)
            {
                Tracer.Error((object)"Could not resize image stream as it was larger than the maximum size allowed for memory processing.");
                return((Stream)null);
            }
            if (Settings.Media.UseLegacyResizing)
            {
                return(this.ResizeLegacy(inputStream, options, outputFormat));
            }
            Resizer resizer = new Resizer();

            using (Bitmap originalBitmap = new Bitmap((Stream)inputStream))
            {
                Size frameSize = resizer.GetFrameSize(originalBitmap, resizeOptions);
                if (originalBitmap.Size.Equals((object)frameSize))
                {
                    inputStream.Seek(0L, SeekOrigin.Begin);
                    return((Stream)inputStream);
                }
                using (Bitmap resizedBitmap = resizer.Resize(originalBitmap, resizeOptions, outputFormat))
                {
                    MemoryStream memoryStream = new MemoryStream();
                    memoryStream.Seek(0L, SeekOrigin.Begin);
                    ImageCodecInfo    encoderInfo   = this.FindEncoderInfo(outputFormat);
                    EncoderParameters encoderParams = new EncoderParameters(1);
                    encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, (long)options.Quality);
                    resizedBitmap.Save((Stream)memoryStream, encoderInfo, encoderParams);
                    memoryStream.SetLength(memoryStream.Position);
                    memoryStream.Seek(0L, SeekOrigin.Begin);
                    return((Stream)memoryStream);
                }
            }
        }