您的位置:时时app平台注册网站 > 编程知识 > 图片缩放的相关处理时时app平台注册网站

图片缩放的相关处理时时app平台注册网站

2019-10-30 04:21
  1. 调整和减少未有独到的技术
  2. 前段时间只扶植JPEG格式的图样
  3. 黄金时代部分图形张开缩放之后会留给一个1像素浅浅的边

图形缩放的连带处理,图片缩放相关管理

图形缩放的相干处理

近日和好做的点东西上用到了图片的管理,可是未有找到确切的图纸处理相关算法,倒是找到了多少个得到缩略图的算法。代码相当短,轻松看了看规律也比较轻巧(其实不是简单是C#的卷入的太多了,所以展示比较轻便)。既然没有相关的合併调用的代码,索性,作者来整治意气风发套相关的管理代码然后公开出来吧。

图表管理的相干剖析

实质上作者要用的图样管理超轻松,就是简单的滑坡一下图片。然后把图纸设置到对应大小而已。然则对于设置的艺术,C#基本功的函数中接收的Rect范围设置。那些界定设置总结起来大概相对脑瓜疼一些。所以作者在思索要不要让调用者在忽视Rect那一个参数的景观欢快的调用接口呢?于是乎,作者定义了多少个图片缩放类型(Full、Zoom、Overflow、Original)。然后默许的让原本图片的中央点与输出图片的骨干点对齐。然后依照缩放类型来对图纸打开不一样的缩放情势。

缩放形式

  1. Full:让图片甩掉原来的比例,直接依照指标数据开展拉伸
  2. Zoom:让图片保持原本比例,况且保险原始图片在指标图片的正中心,未有图片的职位保留米石青
  3. Original:让图片保持原本的分寸,输出图片相似于裁剪中间地方某些范围的图样
  4. Overflow:让图片保持原本的比例,何况有限扶植输指标是被图片填满的不留空白

调用的圭臬

ImageHelper.GetInstance().ImageCompress(InputFile, Path.Combine(OutPutPath, item_file), new System.Drawing.Size(500, 300), ImageZoomType.Full, 50);

样本显示

原始图片

时时app平台注册网站 1

Full 300*500 

时时app平台注册网站 2

 

Full 500 * 300

时时app平台注册网站 3

 

Original 300 * 500

时时app平台注册网站 4

 

Original 500 * 300

时时app平台注册网站 5

 

Overflow 300 * 500

时时app平台注册网站 6

 

Overflow 500 * 300

时时app平台注册网站 7

 

Zoom 300 * 500

时时app平台注册网站 8

 

Zoom 500 * 300

时时app平台注册网站 9

存在的不足

  1. 减去没有独到的本领
  2. 时下只帮忙JPEG格式的图纸
  3. 一些图纸实行缩放之后会留给贰个1像素浅浅的边

继续的内容

图片管理不光是缩放,还或然有别的的不菲事物比较实用(截图、压缩、按高缩放、按宽缩放、增加水印、加多隐蔽水印),作者盘算把图纸常用的连带管理。整理成一个扶助类,方便迁入到各样品类中去。

由此小编构建了一个专程的图形管理的开源项目便利后续效应步向,地址如下:

 

收缩相关的代码

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageCompress
{
    public struct AnchorPoint
    {
        //public AnchorPoint() : this(0, 0) { }
        public AnchorPoint(double x, double y)
        {
            X = x;
            Y = y;
        }

        public double X, Y;
    }

    public enum ImageZoomType
    {
        Full,
        Zoom,
        Overflow,
        Original,
    }

    public delegate void ImageHandle(Image image, Bitmap bitmap);

    public class ImageHelper
    {
        private ImageHelper() { }

        private static ImageHelper _image_helper = null;
        private Dictionary<ImageZoomType, ImageHandle> _name_to_handle = new Dictionary<ImageZoomType, ImageHandle>();

        public static ImageHelper GetInstance()
        {
            if (_image_helper == null)
            {
                _image_helper = new ImageHelper();

                //填满
                _image_helper._name_to_handle[ImageZoomType.Full] = (image, bitmap) =>
                {
                    _image_helper.DrawImage(image, bitmap, new Rectangle(new Point(0, 0), bitmap.Size));
                };

                //原始
                _image_helper._name_to_handle[ImageZoomType.Original] = (image, bitmap) =>
                {
                    _image_helper.DrawImage(image, bitmap, 1, new AnchorPoint(0.5, 0.5), new AnchorPoint(0.5, 0.5));
                };

                //溢出
                _image_helper._name_to_handle[ImageZoomType.Overflow] = (image, bitmap) =>
                {
                    float proportion_x = (float)bitmap.Width / image.Width;
                    float proportion_y = (float)bitmap.Height / image.Height;
                    _image_helper.DrawImage(image, bitmap, proportion_x > proportion_y ? proportion_x : proportion_y, new AnchorPoint(0.5, 0.5), new AnchorPoint(0.5, 0.5));
                };

                //缩放
                _image_helper._name_to_handle[ImageZoomType.Zoom] = (image, bitmap) =>
                {
                    float proportion_x = (float)bitmap.Width / image.Width;
                    float proportion_y = (float)bitmap.Height / image.Height;
                    _image_helper.DrawImage(image, bitmap, proportion_x < proportion_y ? proportion_x : proportion_y, new AnchorPoint(0.5, 0.5), new AnchorPoint(0.5, 0.5));
                };
            }

            return _image_helper;
        }

        /// <summary>
        /// 压缩图片
        /// </summary>
        /// <param name="source_path">源数据位置</param>
        /// <param name="save_path">保存数据位置</param>
        /// <param name="save_size">保存图片大小</param>
        /// <param name="ztype">缩放模式</param>
        /// <param name="flag">图片保存品质</param>
        /// <returns>是否完成压缩</returns>
        public bool ImageCompress(string source_path, string save_path, Size save_size, ImageZoomType ztype, int flag)
        {
            bool success = false;

            Image source = null;

            while (true)
            {
                source = LoadImage(source_path);
                if (source == null)
                    break;

                Bitmap bitmap = new Bitmap(save_size.Width, save_size.Height);

                if (_name_to_handle.ContainsKey(ztype))
                {
                    _name_to_handle[ztype](source, bitmap);
                }
                else
                {
                    break;
                }

                success = SaveImage(bitmap, save_path, source.RawFormat, flag);
                break;
            }

            if (source != null)
                source.Dispose();

            return success;
        }

        public Image LoadImage(string source_path)
        {
            Image image = null;
            while (true)
            {
                if (!File.Exists(source_path))
                    break;

                try
                {
                    image = Image.FromFile(source_path);
                }
                catch
                {
                    break;
                }

                break;
            }

            return image;
        }

        /// <summary>
        /// 将BitMap保存到磁盘上
        /// </summary>
        /// <param name="image">需要保存的图片</param>
        /// <param name="save_path">保存的路径</param>
        /// <param name="format">保存格式</param>
        /// <param name="flag">保存质量</param>
        /// <returns></returns>
        public bool SaveImage(Bitmap image, string save_path, ImageFormat format, int flag)
        {
            //以下代码为保存图片时,设置压缩质量  
            EncoderParameters ep = new EncoderParameters();
            long[] qy = new long[1];
            qy[0] = flag;//设置压缩的比例1-100  
            EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
            ep.Param[0] = eParam;
            try
            {
                ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo jpegICIinfo = null;
                for (int x = 0; x < arrayICI.Length; x  )
                {
                    if (arrayICI[x].FormatDescription.Equals("JPEG"))
                    {
                        jpegICIinfo = arrayICI[x];
                        break;
                    }
                }
                if (jpegICIinfo != null)
                {
                    image.Save(save_path, jpegICIinfo, ep);//dFile是压缩后的新路径  
                }
                else
                {
                    image.Save(save_path, format);
                }
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                image.Dispose();
            }
        }

        /// <summary>
        /// 画图
        /// </summary>
        /// <param name="source">原始图像</param>
        /// <param name="output">输出图像</param>
        /// <param name="souce_scale">原始图像的缩放值</param>
        /// <param name="souce_anchor"></param>
        /// <param name="graphics_anchor"></param>
        public void DrawImage(Image source, Bitmap output, float souce_scale, AnchorPoint souce_anchor, AnchorPoint graphics_anchor)
        {
            DrawImage(source, output, souce_scale, souce_anchor, new Point((int)(output.Width * graphics_anchor.X), (int)(output.Height * graphics_anchor.Y)));
        }

        /// <summary>
        /// 画图
        /// </summary>
        /// <param name="source">原始图像</param>
        /// <param name="output">输出凸显</param>
        /// <param name="source_scale">图像的所放值</param>
        /// <param name="source_anchor">源图像锚点</param>
        /// <param name="souce_point">图像位置</param>
        public void DrawImage(Image source, Bitmap output, float source_scale, AnchorPoint source_anchor, Point souce_point)
        {
            var pic_po = new Point((int)(souce_point.X - source.Size.Width * source_scale * source_anchor.X), (int)(souce_point.Y - source.Size.Height * source_scale * source_anchor.Y));
            DrawImage(source, output, new Rectangle(pic_po, new Size((int)(source.Width * source_scale), (int)(source.Height * source_scale))));
        }

        /// <summary>
        /// 画图
        /// </summary>
        /// <param name="source"></param>
        /// <param name="output"></param>
        /// <param name="rect"></param>
        public void DrawImage(Image source, Bitmap output, Rectangle rect)
        {
            Graphics g = Graphics.FromImage(output);
            g.Clear(Color.WhiteSmoke);
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(source, rect, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel);
            g.Dispose();
        }
    }
}

  

 

PS:为了美丽本文中的原始图片并非原始图片,因为本来图片太大了。会招致页面滑动,所以自身上传了一张十分小的图片。Git的图纸则是原本的图纸

图片缩放的相关处理近年来温馨做的点东西上用到了图片的管理,不过并没有找到相符的图片管理相...

时时app平台注册网站 10

样板呈现

故而笔者建构了三个专程的图形管理的开源项目便利后续效应进入,地址如下:

 

Zoom 500 * 300

时时app平台注册网站 11

Full 300*500 

Full 500 * 300

ImageHelper.GetInstance().ImageCompress(InputFile, Path.Combine(OutPutPath, item_file), new System.Drawing.Size(500, 300), ImageZoomType.Full, 50);

时时app平台注册网站 12

 

 

 

前些天协调做的点东西上用到了图片的管理,可是尚未找到适当的图形管理相关算法,倒是找到了多少个获得缩略图的算法。代码非常短,轻易看了看规律也比较容易(其实不是大约是C#的包裹的太多了,所以显示比较轻便)。既然未有相关的联合调用的代码,索性,笔者来整合治理少年老成套相关的管理代码然后公开出来吧。

持续的内容

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageCompress
{
    public struct AnchorPoint
    {
        //public AnchorPoint() : this(0, 0) { }
        public AnchorPoint(double x, double y)
        {
            X = x;
            Y = y;
        }

        public double X, Y;
    }

    public enum ImageZoomType
    {
        Full,
        Zoom,
        Overflow,
        Original,
    }

    public delegate void ImageHandle(Image image, Bitmap bitmap);

    public class ImageHelper
    {
        private ImageHelper() { }

        private static ImageHelper _image_helper = null;
        private Dictionary<ImageZoomType, ImageHandle> _name_to_handle = new Dictionary<ImageZoomType, ImageHandle>();

        public static ImageHelper GetInstance()
        {
            if (_image_helper == null)
            {
                _image_helper = new ImageHelper();

                //填满
                _image_helper._name_to_handle[ImageZoomType.Full] = (image, bitmap) =>
                {
                    _image_helper.DrawImage(image, bitmap, new Rectangle(new Point(0, 0), bitmap.Size));
                };

                //原始
                _image_helper._name_to_handle[ImageZoomType.Original] = (image, bitmap) =>
                {
                    _image_helper.DrawImage(image, bitmap, 1, new AnchorPoint(0.5, 0.5), new AnchorPoint(0.5, 0.5));
                };

                //溢出
                _image_helper._name_to_handle[ImageZoomType.Overflow] = (image, bitmap) =>
                {
                    float proportion_x = (float)bitmap.Width / image.Width;
                    float proportion_y = (float)bitmap.Height / image.Height;
                    _image_helper.DrawImage(image, bitmap, proportion_x > proportion_y ? proportion_x : proportion_y, new AnchorPoint(0.5, 0.5), new AnchorPoint(0.5, 0.5));
                };

                //缩放
                _image_helper._name_to_handle[ImageZoomType.Zoom] = (image, bitmap) =>
                {
                    float proportion_x = (float)bitmap.Width / image.Width;
                    float proportion_y = (float)bitmap.Height / image.Height;
                    _image_helper.DrawImage(image, bitmap, proportion_x < proportion_y ? proportion_x : proportion_y, new AnchorPoint(0.5, 0.5), new AnchorPoint(0.5, 0.5));
                };
            }

            return _image_helper;
        }

        /// <summary>
        /// 压缩图片
        /// </summary>
        /// <param name="source_path">源数据位置</param>
        /// <param name="save_path">保存数据位置</param>
        /// <param name="save_size">保存图片大小</param>
        /// <param name="ztype">缩放模式</param>
        /// <param name="flag">图片保存品质</param>
        /// <returns>是否完成压缩</returns>
        public bool ImageCompress(string source_path, string save_path, Size save_size, ImageZoomType ztype, int flag)
        {
            bool success = false;

            Image source = null;

            while (true)
            {
                source = LoadImage(source_path);
                if (source == null)
                    break;

                Bitmap bitmap = new Bitmap(save_size.Width, save_size.Height);

                if (_name_to_handle.ContainsKey(ztype))
                {
                    _name_to_handle[ztype](source, bitmap);
                }
                else
                {
                    break;
                }

                success = SaveImage(bitmap, save_path, source.RawFormat, flag);
                break;
            }

            if (source != null)
                source.Dispose();

            return success;
        }

        public Image LoadImage(string source_path)
        {
            Image image = null;
            while (true)
            {
                if (!File.Exists(source_path))
                    break;

                try
                {
                    image = Image.FromFile(source_path);
                }
                catch
                {
                    break;
                }

                break;
            }

            return image;
        }

        /// <summary>
        /// 将BitMap保存到磁盘上
        /// </summary>
        /// <param name="image">需要保存的图片</param>
        /// <param name="save_path">保存的路径</param>
        /// <param name="format">保存格式</param>
        /// <param name="flag">保存质量</param>
        /// <returns></returns>
        public bool SaveImage(Bitmap image, string save_path, ImageFormat format, int flag)
        {
            //以下代码为保存图片时,设置压缩质量  
            EncoderParameters ep = new EncoderParameters();
            long[] qy = new long[1];
            qy[0] = flag;//设置压缩的比例1-100  
            EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
            ep.Param[0] = eParam;
            try
            {
                ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo jpegICIinfo = null;
                for (int x = 0; x < arrayICI.Length; x  )
                {
                    if (arrayICI[x].FormatDescription.Equals("JPEG"))
                    {
                        jpegICIinfo = arrayICI[x];
                        break;
                    }
                }
                if (jpegICIinfo != null)
                {
                    image.Save(save_path, jpegICIinfo, ep);//dFile是压缩后的新路径  
                }
                else
                {
                    image.Save(save_path, format);
                }
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                image.Dispose();
            }
        }

        /// <summary>
        /// 画图
        /// </summary>
        /// <param name="source">原始图像</param>
        /// <param name="output">输出图像</param>
        /// <param name="souce_scale">原始图像的缩放值</param>
        /// <param name="souce_anchor"></param>
        /// <param name="graphics_anchor"></param>
        public void DrawImage(Image source, Bitmap output, float souce_scale, AnchorPoint souce_anchor, AnchorPoint graphics_anchor)
        {
            DrawImage(source, output, souce_scale, souce_anchor, new Point((int)(output.Width * graphics_anchor.X), (int)(output.Height * graphics_anchor.Y)));
        }

        /// <summary>
        /// 画图
        /// </summary>
        /// <param name="source">原始图像</param>
        /// <param name="output">输出凸显</param>
        /// <param name="source_scale">图像的所放值</param>
        /// <param name="source_anchor">源图像锚点</param>
        /// <param name="souce_point">图像位置</param>
        public void DrawImage(Image source, Bitmap output, float source_scale, AnchorPoint source_anchor, Point souce_point)
        {
            var pic_po = new Point((int)(souce_point.X - source.Size.Width * source_scale * source_anchor.X), (int)(souce_point.Y - source.Size.Height * source_scale * source_anchor.Y));
            DrawImage(source, output, new Rectangle(pic_po, new Size((int)(source.Width * source_scale), (int)(source.Height * source_scale))));
        }

        /// <summary>
        /// 画图
        /// </summary>
        /// <param name="source"></param>
        /// <param name="output"></param>
        /// <param name="rect"></param>
        public void DrawImage(Image source, Bitmap output, Rectangle rect)
        {
            Graphics g = Graphics.FromImage(output);
            g.Clear(Color.WhiteSmoke);
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(source, rect, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel);
            g.Dispose();
        }
    }
}

Original 500 * 300

图片管理不光是缩放,还应该有其余的不在少数事物比较实用(截图、压缩、按高缩放、按宽缩放、增添水印、增加隐蔽水印),小编绸缪把图片常用的连带管理。收拾成二个声援类,方便迁入到各种档期的顺序中去。

 

图形管理的连带剖析

Overflow 500 * 300

时时app平台注册网站 13

 

Original 300 * 500

时时app平台注册网站 14

调用的范例

 

  1. Full:让图片扬弃原本的比重,间接依照目的数据实行拉伸
  2. Zoom:让图片保持原有比例,并且保险原始图片在对象图片的正中心,未有图片的地方保留米深橙
  3. Original:让图片保持原本的尺寸,输出图片相似于裁剪中间地点有个别范围的图片
  4. Overflow:让图片保持原来的比重,何况保障输目的是被图片填满的不留空白

实际笔者要用的图纸管理特轻易,就是轻巧的减弱一下图形。然后把图纸设置到对应大小而已。不过对于设置的法子,C#基本功的函数中央银行使的Rect范围设置。这么些范围设置总结起来只怕相对头痛一些。所以小编在思索要不要让调用者在忽视Rect这么些参数的情况欢愉的调用接口呢?于是乎,笔者定义了几个图片缩放类型(Full、Zoom、Overflow、Original)。然后暗许的让原有图片的宗旨点与出口图片的中央点对齐。然后依据缩放类型来对图片实行差异的缩放方式。

 

缩放格局

时时app平台注册网站 15

时时app平台注册网站 16

图片缩放的有关管理

本来图片

减少相关的代码

Zoom 300 * 500

PS:为了美貌本文中的原始图片实际不是本来图片,因为原来图片太大了。会导致页面滑动,所以自个儿上传了一张超小的图样。Git的图样则是原有的图片

时时app平台注册网站 17

留存的欠缺

时时app平台注册网站 18

Overflow 300 * 500

  

 

本文由时时app平台注册网站发布于编程知识,转载请注明出处:图片缩放的相关处理时时app平台注册网站

关键词: