手机
当前位置:查字典教程网 >编程开发 >C#教程 >用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);

}

}

}

}

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

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

【用C#缩小照片上传到各种空间的具体方法】相关文章:

C# Console类的具体用法

C#中隐式运行CMD命令行窗口的方法

使用C#开发Socket通讯的方法

用C#编写获取远程IP,MAC的方法

C#中动态显示当前系统时间的实例方法

基于c#图像灰度化、灰度反转、二值化的实现方法详解

c#生成缩略图的实现方法

用C#实现启动另一程序的方法实例

使用C#获取系统特殊文件夹路径的解决方法

C# 设置系统日期格式的方法

精品推荐
分类导航