码迷,mamicode.com
首页 > 其他好文 > 详细

图片缩放(附源码)

时间:2015-08-26 17:43:37      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

按给定大小等比例缩放图片的一个小工具,支持GIF动态图的缩放。

主要代码

public class FormatImage
    {
        public static string FormatALLImage(ImageInfo info)
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(info.FromPath);
            try
            {
                int ImageWidth = image.Width;
                int imageHeight = image.Height;
                //图片本身宽高与缩放宽高比较,判断是否有缩放的必要
                if (ImageWidth > info.SizeWidth && imageHeight > info.SizeHeigth)
                {
                    //判断图片是否是动态图(动态图片有多帧)
                    if (image.GetFrameCount(new FrameDimension(image.FrameDimensionsList[0])) > 1)
                    {
                        //先分解缩放每帧图片
                        IList<string> imageurls = Decompose(info, image);
                        //缩放完合并
                        return Merger(info.OutPath, imageurls, info.Time);
                    }
                    else
                    {
                        //缩放静态图
                        return FormatStaticImage(info, image);
                    }
                }
                else
                {
                    image.Dispose();
                    return SaveNoFormatImage(info);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                image.Dispose();
            }
        }

        /// <summary>
        /// GIF 动态图片分解
        /// </summary>
        /// <param name="info"></param>
        /// <param name="image"></param>
        /// <returns></returns>
        private static IList<string> Decompose(ImageInfo info, System.Drawing.Image image)
        {
            //分解
            IList<string> imageurls = new List<string>();

            for (int i = 0; i < image.GetFrameCount(new FrameDimension(image.FrameDimensionsList[0])); i++)
            {
                image.SelectActiveFrame(new FrameDimension(image.FrameDimensionsList[0]), i);

                System.Drawing.Image formatImage = new Bitmap(info.SizeWidth, info.SizeHeigth);

                Graphics g = Graphics.FromImage(formatImage);

                g.Clear(Color.Blue);

                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;

                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;

                g.DrawImage(image, new System.Drawing.Rectangle(0, 0, formatImage.Width, formatImage.Height),
                new System.Drawing.Rectangle(0, 0, image.Width, image.Height),

                System.Drawing.GraphicsUnit.Pixel);

                string SavePath = Path.Combine(Path.GetFullPath(info.OutPath), DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".png");

                formatImage.Save(SavePath, System.Drawing.Imaging.ImageFormat.Png);

                imageurls.Add(SavePath);
            }

            return imageurls;
        }

        /// <summary>
        /// GIF动态图片合并
        /// </summary>
        /// <param name="OutPath"></param>
        /// <param name="imageurls"></param>
        /// <returns></returns>
        private static string Merger(string OutPath, IList<string> imageurls, int Time)
        {
            //合并

            string ImageName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".gif";

            string outputFilePath = Path.Combine(Path.GetFullPath(OutPath), ImageName);

            AnimatedGifEncoder e = new AnimatedGifEncoder();

            e.Start(outputFilePath);
            //每帧图片间隔
            e.SetDelay(Time);

            e.SetRepeat(0);

            for (int i = 0; i < imageurls.Count; i++)
            {
                e.AddFrame(System.Drawing.Image.FromFile(imageurls[i]));
            }

            e.Finish();

            return ImageName;
        }


        /// <summary>
        /// 静态图片缩放
        /// </summary>
        /// <param name="info"></param>
        /// <param name="image"></param>
        /// <returns></returns>
        private static string FormatStaticImage(ImageInfo info, System.Drawing.Image image)
        {
            image = System.Drawing.Image.FromFile(info.FromPath);

            System.Drawing.Image formatImage = new Bitmap(info.SizeWidth, info.SizeHeigth);

            Graphics g = Graphics.FromImage(formatImage);

            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            g.Clear(Color.Blue);

            g.DrawImage(image, new System.Drawing.Rectangle(0, 0, formatImage.Width, formatImage.Height),
            new System.Drawing.Rectangle(0, 0, image.Width, image.Height),

            System.Drawing.GraphicsUnit.Pixel);

            string ImageName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";

            string SavePath = Path.Combine(Path.GetFullPath(info.OutPath), ImageName);

            formatImage.Save(info.OutPath + "\\" + ImageName, info.OutImageFormat);

            g.Dispose();

            return ImageName;
        }


        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        private static string SaveNoFormatImage(ImageInfo info)
        {
            string ImageName = Path.GetFileName(info.FromPath);
            string SavePath = Path.Combine(Path.GetFullPath(info.OutPath), ImageName);
            FileStream fs = null;
            try
            {
                fs = File.Create(info.FromPath);

                File.Copy(info.FromPath, info.OutPath);

                return ImageName;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                fs.Close();
            }
        }
    }

 

链接: http://pan.baidu.com/s/1mgBxB7M 密码: w1gd

图片缩放(附源码)

标签:

原文地址:http://www.cnblogs.com/ranyjax/p/4760830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!