中秋到了,首先祝各位猿友節日快樂!!!
本博文的原名稱是“跟我一起用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);
}
}
}
}
終結代碼
是不是很簡單啊?在這裡使用的一些路徑比例的變量,可将其寫在配置檔案,後續再壓縮照片,隻要修改相應配置即可輕松實作。
完了之後,大家有更好的關于壓縮照片的辦法不妨拿出來分享下!
好了過程完畢上傳張周末去深圳西湧玩的照片吧(照片是全景拍攝的,但是不知道為啥上傳後不能全看到)
感謝閱讀,請留下您的意見或疑問! 能力有限,錯漏難免,歡迎指點!
分割線:我的個人原創,請認準 http://freedong.cnblogs.com/ (轉摘不标原文出處可恥)