Esempio n. 1
0
        //public Image RoundCorners(int radius, Color background_color)
        //{
        //    radius *= 2;
        //    Bitmap result = new Bitmap(this.Image.Width, this.Image.Height);

        //    using (Graphics g = Graphics.FromImage(result))
        //    {
        //        g.Clear(background_color);
        //        g.SmoothingMode = SmoothingMode.AntiAlias;

        //        using (Brush brush = new TextureBrush(this.Image))
        //        using (GraphicsPath gp = new GraphicsPath())
        //        {
        //            gp.AddArc(0, 0, radius, radius, 180, 90);
        //            gp.AddArc(0 + result.Width - radius, 0, radius, radius, 270, 90);
        //            gp.AddArc(0 + result.Width - radius, 0 + result.Height - radius, radius, radius, 0, 90);
        //            gp.AddArc(0, 0 + result.Height - radius, radius, radius, 90, 90);
        //            g.FillPath(brush, gp);

        //            this.Image = result;
        //        }
        //    }

        //    return this.Image;
        //}

        public ImagerService Pad(Size size, Color background)
        {
            var param = new ImagerResizeParams(ImagerResizeType.Fit, size, background);

            return(this.Modify(param));
        }
Esempio n. 2
0
        public ImagerService Resize(int max_size)
        {
            var param = new ImagerResizeParams(ImagerResizeType.Scale, new Size(max_size, max_size), Color.Empty);

            return(this.Modify(param));
        }
Esempio n. 3
0
        public ImagerService Modify(ImagerResizeType type, Size size)
        {
            var param = new ImagerResizeParams(type, size, Color.Empty);

            return(this.Modify(param));
        }
Esempio n. 4
0
        public ImagerService Modify(ImagerResizeType type, int maxSize)
        {
            var param = new ImagerResizeParams(type, new Size(maxSize, maxSize), Color.Empty);

            return(this.Modify(param));
        }
Esempio n. 5
0
        public ImagerService Modify(ImagerResizeParams param)
        {
            //размеры текущего изображения
            int image_width  = this.Image.Width;
            int image_height = this.Image.Height;

            //определяем процент сжатия, чтобы уложиться в новый размер
            float percent_width  = ((float)param.Size.Width / (float)image_width);
            float percent_height = ((float)param.Size.Height / (float)image_height);

            float percent = Math.Min(percent_height, percent_width);

            //если нужно обрезать, то изображение должно быть больше указанных размеров
            if (param.Type == ImagerResizeType.Crop)
            {
                percent = Math.Max(percent_height, percent_width);
            }

            //если менярь размеры не нужно, то процент === 1
            if (param.Type == ImagerResizeType.None)
            {
                percent = 1.0f;
            }

            //окончательные размеры сжатого изображения
            int redraw_width  = (int)(image_width * percent);
            int redraw_height = (int)(image_height * percent);

            //если нужно заполнить указанный размер с растяжением изображения
            if (param.Type == ImagerResizeType.Fill)
            {
                redraw_width  = param.Size.Width;
                redraw_height = param.Size.Height;
            }

            int canvas_width  = param.Size.Width;
            int canvas_height = param.Size.Height;

            //если мы просто меняем размеры изображения пропорционально
            // или не меняем размер изображения совсем
            // - холст должен совпадать с размерами измененного изображения
            if (param.Type == ImagerResizeType.None || param.Type == ImagerResizeType.Scale)
            {
                canvas_width  = redraw_width;
                canvas_height = redraw_height;
            }

            //результирующий холст - новое изображение в новых размерах
            Bitmap result = new Bitmap(canvas_width, canvas_height);

            //как-то не правильно получилось - задаю CROP в квадрат - а возвращает он мне прямоугольник!!!
            ////если цвет заполнения не задан, то незачем слушаться новых размеров - просто нужно отмасштабировать
            //if (param.Type == ImageResizeType.None || param.Background == Color.Empty)
            //    result = new Bitmap(redraw_width, redraw_height);

            using (Graphics graphics = Graphics.FromImage(result))
            {
                if (param.Background != Color.Empty)
                {
                    using (Brush br = new SolidBrush(param.Background))
                        graphics.FillRectangle(br, 0, 0, result.Width, result.Height);
                }

                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                graphics.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                graphics.DrawImage(this.Image, (result.Width / 2) - (redraw_width / 2), (result.Height / 2) - (redraw_height / 2), redraw_width, redraw_height);
            }

            this.Image = result;

            return(this);
        }