Exemplo n.º 1
0
 public Imager Resize(int maxWidth, int maxHeight)
 {
     var param = new ImagerResizeParams(ImagerResizeType.Scale, new Size(maxWidth, maxHeight), Color.Empty);
     return this.Modify(param);
 }
Exemplo n.º 2
0
 public Imager Modify(ImagerResizeType type, Size size)
 {
     var param = new ImagerResizeParams(type, size, Color.Empty);
     return this.Modify(param);
 }
Exemplo n.º 3
0
        //public Imager Load(byte[] data)
        //{
        //    this.Image = null;
        //    if (data != null)
        //    {
        //        using (MemoryStream ms = new MemoryStream(data))
        //        {
        //            ms.Position = 0;
        //            this.Image = Image.FromStream(ms);
        //            this._properties = this.Image.PropertyItems;
        //            ms.Close();
        //        }
        //    }
        //    return this;
        //}
        //public PropertyItem GetProperty(int id)
        //{
        //    try
        //    {
        //        if (this.Image == null)
        //            return null;
        //        PropertyItem property = null;
        //        if (this.Image.PropertyItems != null && this.Image.PropertyItems.Length > 0)
        //            property = this.Image.GetPropertyItem(id);
        //        else if (this._properties != null && this._properties.Length > 0)
        //            property = this._properties.FirstOrDefault(p => p.Id == id);
        //        return property;
        //    }
        //    catch { return null; }
        //}
        //public string GetProperty(int id, Encoding encoding)
        //{
        //    try
        //    {
        //        var property = this.GetProperty(id);
        //        if (property == null || property.Value == null || property.Len <= 0)
        //            return "";
        //        //return encoding.GetString(property.Value, 0, property.Len - ((encoding.IsSingleByte) ? 1 : 2);
        //        return encoding.GetString(property.Value, 0, property.Len).Trim(new char[] { '\0' });
        //    }
        //    catch { return ""; }
        //}
        //public void SetProperty(int id, string value, Encoding encoding)
        //{
        //    try
        //    {
        //        PropertyItem property = this.GetProperty(id);
        //        if (property == null)
        //            return;
        //        property.Value = encoding.GetBytes(value);
        //        this.Image.SetPropertyItem(property);
        //    }
        //    catch { }
        //}
        //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 Imager Pad(Size size, Color background)
        {
            var param = new ImagerResizeParams(ImagerResizeType.Fit, size, background);

            return this.Modify(param);
        }
Exemplo n.º 4
0
        public Imager Modify(ImagerResizeParams param)
        {
            //размеры текущего изображения
            int image_width = this.Width;
            int image_height = this.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;
        }