建立縮圖
當我在更新我的Ajax示例的時候,我需要為某個目錄下的圖片創建縮略圖。可用的工具實在是多不勝數,不過我還是想分享一下我所使用的一段非常簡單的代碼。
這段程式會提取根目錄下的所有jpg檔,為它們創建160*120的縮略圖,並將圖片拷貝到完整路徑下。
namespace ThumbNailer
{
class Program
{
static void Main(string[] args)
{
string rootPath = @"C:\Users\brada\Desktop\ForDemo";
string thumbPath = Path.Combine(rootPath, "Thumb");
if (Directory.Exists(thumbPath)) DirectoryDelete(thumbPath);
Directory.CreateDirectory(thumbPath);
int imageNumber = 0;
foreach (string s in Directory.GetFiles(rootPath, "*.jpg"))
{
imageNumber++;
Console.WriteLine("{0}:{1}", imageNumber, s);
Image i = Image.FromFile(s);
Image thumb = i.GetThumbnailImage(160, 120, null, IntPtr.Zero);
thumb.Save(Path.Combine(thumbPath, GetName(imageNumber)));
}
}
static void DirectoryDelete(string directoryName)
{
foreach (string filename in Directory.GetFiles(directoryName))
{
File.Delete(filename);
}
Directory.Delete(directoryName);
}
static string GetName(int imageNumber)
{
return String.Format("{0}.jpg", imageNumber);
}
}
}
更新:有些人問我如何在ASP.NET中實現……Bertrand有一個完整得多的例子在此:
http://dotnetslackers.com/articles/aspnet/Generating-Image-Thumbnails-in-ASP-NET.aspx
http://weblogs.asp.net/bleroy/archive/2007/12/05/what-interpolationmode-and-compositingquality-to-use-when-generating-thumbnails-via-system-drawing.aspx
本文章來自於神魂顛倒論壇 http://bbs.flash2u.com.tw
原文網址:http://bbs.flash2u.com.tw/dispbbs_58_86043_3.html
這段程式會提取根目錄下的所有jpg檔,為它們創建160*120的縮略圖,並將圖片拷貝到完整路徑下。
namespace ThumbNailer
{
class Program
{
static void Main(string[] args)
{
string rootPath = @"C:\Users\brada\Desktop\ForDemo";
string thumbPath = Path.Combine(rootPath, "Thumb");
if (Directory.Exists(thumbPath)) DirectoryDelete(thumbPath);
Directory.CreateDirectory(thumbPath);
int imageNumber = 0;
foreach (string s in Directory.GetFiles(rootPath, "*.jpg"))
{
imageNumber++;
Console.WriteLine("{0}:{1}", imageNumber, s);
Image i = Image.FromFile(s);
Image thumb = i.GetThumbnailImage(160, 120, null, IntPtr.Zero);
thumb.Save(Path.Combine(thumbPath, GetName(imageNumber)));
}
}
static void DirectoryDelete(string directoryName)
{
foreach (string filename in Directory.GetFiles(directoryName))
{
File.Delete(filename);
}
Directory.Delete(directoryName);
}
static string GetName(int imageNumber)
{
return String.Format("{0}.jpg", imageNumber);
}
}
}
更新:有些人問我如何在ASP.NET中實現……Bertrand有一個完整得多的例子在此:
http://dotnetslackers.com/articles/aspnet/Generating-Image-Thumbnails-in-ASP-NET.aspx
http://weblogs.asp.net/bleroy/archive/2007/12/05/what-interpolationmode-and-compositingquality-to-use-when-generating-thumbnails-via-system-drawing.aspx
本文章來自於神魂顛倒論壇 http://bbs.flash2u.com.tw
原文網址:http://bbs.flash2u.com.tw/dispbbs_58_86043_3.html
留言