Пример #1
0
        public System.IO.Stream Process(IImageProcessorSetting setting, System.IO.Stream imageStream)
        {
            ExceptionHelper.ThrowIfNull(imageStream, "imageStream");

            if (!_Args.IsNeetProcess())
            {
                return(imageStream.Copy());
            }

            var image = Image.FromStream(imageStream);

            var height = _Args.Height;
            var width  = _Args.Width;

            if (_Args.Multiple.HasValue)
            {
                if (height.HasValue)
                {
                    height = height.Value * _Args.Multiple.Value;
                }
                if (width.HasValue)
                {
                    width = width.Value * _Args.Multiple.Value;
                }
            }

            if (height.HasValue && width.HasValue)
            {
                var heightFirst = _Args.Edge == ZoomProcessArguments.EdgeEnum.Long
                                    ? image.Height >= image.Width
                                    : image.Height <= image.Width;
                if (heightFirst)
                {
                    var e = (double)image.Height / (double)height.Value;
                    width = (int)((double)image.Width / e);
                }
                else
                {
                    var e = (double)image.Width / (double)width.Value;
                    height = (int)((double)image.Height / e);
                }
            }
            else
            {
                var e = (double)image.Height / (double)image.Width;
                if (height.HasValue)
                {
                    width = (int)((double)height.Value / e);
                }
                else if (width.HasValue)
                {
                    height = (int)(width.Value * e);
                }
            }

            var format   = String.IsNullOrWhiteSpace(_Args.Format) ? ".jpg" : _Args.Format;
            var mineType = Flh.IO.MimeTypeHelper.GetMimeType(format);

            ExceptionHelper.ThrowIfTrue(!mineType.StartsWith("image/"), "_Args", "输出图片格式设置错误");

            if (height.HasValue && width.HasValue && _Args.Large.HasValue && _Args.Large.Value)
            {
                if (height.Value > image.Height || width.Value > image.Width)
                {
                    height = null;
                    width  = null;
                }
            }

            if (height.HasValue && width.HasValue)
            {
                using (var zoomImage = new Bitmap(width.Value, height.Value))
                {
                    using (var graphics = Graphics.FromImage(zoomImage))
                    {
                        graphics.DrawImage(image, 0, 0, width.Value, height.Value);
                        return(GetQualityZoomStream(zoomImage, mineType, _Args.Quality, _Args.AbsoluteQuality));
                    }
                }
            }
            else if (_Args.AbsoluteQuality.HasValue || _Args.Quality.HasValue)
            {
                return(GetQualityZoomStream(image, mineType, _Args.Quality, _Args.AbsoluteQuality));
            }
            else
            {
                var result = new System.IO.MemoryStream();
                image.Save(result, GetImageFormatByMineType(mineType));
                result.Position = 0;
                return(result);
            }
        }