用C#缩小照片上传到各种空间

  中秋到了,首先祝各位猿友节日快乐!!!

  本博文的原名称是“跟我一起用C#压缩照片上传到各种空间”,评论上有人开骂,没办法我这人就是自信霸气,但是既然有人提出来我还是改掉吧,如果文章写得不好的地方欢迎大家指正,如果是单纯骂人的话我想除了显现出骂人者的不良形象外对我无任何消极影响。

  本人一般也很少上传照片之类的女生喜欢玩的东西,但是偶尔还是要传一传的,为什么?因为现在与各种以前的朋友同学都很少联系,但是只要一发有个人照片的微博或日志便引来各种鲜花鸡蛋。

  周末和几个同学去了西涌露营,这么美丽的海滩不上传照片分享着实可惜,可是现在的相机拍出来的照片很大,特别是单反,而咱们的网络带宽又何其可怜,所以先压缩再上传会是非常好的选择,可是呢这么多张照片一张张压缩太麻烦了(鄙人对作图是小白,不懂得使用做图工具),而咱是码农,码农就要用码农的方式,于是乎就想做个程序了。

  好了废话了那么多开工了。

  第一次迭代开始,先完成单张相片压缩的Demo。我始终坚信好的代码是重构出来的,因而在这里我将使用迭代开发的思想逐步完成这个程序。先看下第一次迭代的代码

        static void Main(string[] args)
        {
            string savePath = @"E:\2013相片\上传前\";
            string sourcePath = @"E:\2013相片\QQ空间使用\";
            string sourceName = "DSC_0216.JPG";
            int scale = 5;

            Image img = Image.FromFile(sourcePath + sourceName);
            int width = img.Width / scale;
            int height = img.Height / scale;

            Image imgNew = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(imgNew);
            g.DrawImage(img, new System.Drawing.Rectangle(0, 0, width, height),
            new System.Drawing.Rectangle(0, 0, img.Width, img.Height),
            System.Drawing.GraphicsUnit.Pixel);

            imgNew.Save(savePath + "a.jpg", ImageFormat.Jpeg);
        }
首次迭代代码

  这样就保证了这个代码已经能处理单张照片了。

 

  第二次迭代,对其进行重构。

经过分析,该功能的实现需要两步,第一获取要缩小的图片集合,第二就是缩小图片的逻辑,故而就有了以下重构后的代码

class Program
    {
        static readonly string[] IMAGE_EXTNAME = new string[] { "jpg"};
        static void Main(string[] args)
        {
            string savePath = @"E:\2013相片\上传前\";
            string sourcePath = @"E:\2013相片\QQ空间使用\";
            int scale = 5;

            List<string> imageNames = GetImageNames(sourcePath);
            if (imageNames != null && imageNames.Count > 0)
            {
                foreach (var item in imageNames)
                {
                    SaveSmallPhoto(sourcePath + item, scale, savePath + item);
                }
            }
        }

        /// <summary>
        /// 获取路径下所有符合指定图片后缀文件名
        /// </summary>
        /// <param name="path">路径</param>
        /// <returns></returns>
        static List<string> GetImageNames(string path)
        {
            string[] fileNames = Directory.GetFiles(path);
            if (fileNames == null || fileNames.Length == 0)
            {
                return null;
            }

            List<string> imageNames = new List<string>();
            foreach (var item in fileNames)
            {
                if (ExistsInExtName(item, IMAGE_EXTNAME))
                {
                    imageNames.Add(item.Substring(item.LastIndexOf('\\') + 1));
                }
            }

            return imageNames;
        }

        /// <summary>
        /// 判断文件名是否符合指定后缀名
        /// </summary>
        /// <param name="name">文件名</param>
        /// <param name="extNames">符合要求的后缀名</param>
        /// <returns></returns>
        static bool ExistsInExtName(string name, string[] extNames)
        {
            if (string.IsNullOrEmpty(name) || extNames == null || extNames.Length == 0)
            {
                return false;
            }

            foreach (var item in extNames)
            {
                if (name.ToLower().EndsWith(item))
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 将图片按比例缩小保存
        /// </summary>
        /// <param name="fromPath">原图片路径名</param>
        /// <param name="scale">缩小比例</param>
        /// <param name="toPath">缩小后保存的路径名</param>
        static void SaveSmallPhoto(string fromPath, int scale, string toPath)
        {
            int width, height;
            using (Image img = Image.FromFile(fromPath))
            {
                width = img.Width / scale;
                height = img.Height / scale;

                using (Image imgNew = new Bitmap(width, height))
                {
                    using (Graphics g = Graphics.FromImage(imgNew))
                    {
                        g.DrawImage(img, new Rectangle(0, 0, width, height),
                            new Rectangle(0, 0, img.Width, img.Height), System.Drawing.GraphicsUnit.Pixel);
                    }

                    imgNew.Save(toPath, ImageFormat.Jpeg);
                }
            }
        }
    }
终结代码

  是不是很简单啊?在这里使用的一些路径比例的变量,可将其写在配置文件,后续再压缩照片,只要修改相应配置即可轻松实现。

  完了之后,大家有更好的关于压缩照片的办法不妨拿出来分享下!

  好了过程完毕上传张周末去深圳西涌玩的照片吧(照片是全景拍摄的,但是不知道为啥上传后不能全看到)

posted @ 2013-09-19 00:29  dong.net  阅读(3608)  评论(27编辑  收藏  举报