2014-11-06 136 views
1

我想重新調整文件夾內的每個圖像的大小。我試圖找到一種方法來指定文件夾,我可以重新調整其中的每個圖像的大小,可能使用插件(類似FolderResizeSyntax,雖然我不完全確定它是如何工作的)。另一種方法是循環訪問我的SQL服務器表,獲取文件路徑,使用該文件路徑在我的計算機上打開文件,然後重新調整它的大小。後者看起來效率不高。當前重新大小代碼張貼在下面。幫助如何重新調整文件夾中的所有圖像的大小,將不勝感激。ImageResizer - 調整文件夾內的所有圖像的大小

Dictionary<string, string> versions = new Dictionary<string, string>(); 
//Define the versions to generate 
versions.Add("_Original", ""); //Original Image 
versions.Add("_1000", "width=1000&height=1000&crop=auto"); //Fit to 1000x1000 area 
versions.Add("_500", "width=500&height=500&crop=auto"); //Fit to 500x500 area 
versions.Add("_250", "width=250&height=250&crop=auto"); //Fit to 250x250 area 

//Loop through each uploaded file 
foreach (string fileKey in HttpContext.Current.Request.Files.Keys) 
{ 
    //Generate each version 
    foreach (string suffix in versions.Keys) 
    { 
     HttpPostedFile file = HttpContext.Current.Request.Files[fileKey]; 
     if (file.ContentLength <= 0) continue; //Skip unused file controls. 

     //Create directory/path based on file type (ex. _Raw, _1000, etc.) 
     string uploadFolder = MapPath("~/myImages/" + suffix); 

     //Get the physical path for the uploads folder and make sure it exists 
     if (!Directory.Exists(uploadFolder)) Directory.CreateDirectory(uploadFolder); 

     string fileName = Path.Combine(uploadFolder, file.FileName + suffix); 

     //Let the image builder add the correct extension based on the output file type 
     fileName = ImageBuilder.Current.Build(new ImageJob(file, fileName, new Instructions(versions[suffix]), false, true)).FinalPath; 
    } 
} 

感謝

+0

您是否打算在網站中使用不同的文件大小? ImageResizer的優點在於它可以即時創建不同的圖像大小,然後緩存它們以便常用的圖像大小很快。 ex> http:// somewebsite/Content/myimage?width = 100這樣你就不需要手動調整每個文件的大小。您保留大圖像和ImageResizer使任何大小的圖像,即時請求。 – 2014-11-06 19:29:10

+0

如果這是一次性的事情,我會建議使用一個爲這個東西而構建的程序,如http://lifehacker.com/5147153/bulk-image-resizer-is-light-and-quick-on-photo-processing – 2014-11-06 19:45:31

+0

我不知道我可以使用多少次。我有一個包含圖像的文件夾,我可能希望將它們全部重新調整爲300x300,並將它們放入新文件夾或250x200中,並將它們放在不同的新文件夾中供將來使用。 – BrettKB 2014-11-06 20:05:05

回答

0

解決:

Dictionary<string, string> versions = new Dictionary<string, string>(); 

//Define the versions to generate 
versions.Add("_Raw", ""); //Original Image 
versions.Add("_1000x1000", "width=1000&height=1000&crop=auto"); //Fit to 1000x1000 area 
versions.Add("_500x500", "width=500&height=500&crop=auto"); //Fit to 500x500 area 

string[] path2 = Directory.GetFiles(@"C:\myPictures\TestImages"); 
//Generate each version 
foreach (string dirFile in path2) 
{ 
    foreach (string suffix in versions.Keys) 
    { 
     //Create directory/path based on file type (ex. _Raw, _1000, etc.) 
     string uploadFolder = MapPath("~/TestImages/" + suffix.Replace("_", "")); 

     //Get the physical path for the uploads folder and make sure it exists 
     if (!Directory.Exists(uploadFolder)) Directory.CreateDirectory(uploadFolder); 

     ////Generate a filename. 
     string filePath = Path.GetFileName(dirFile); 
     string fileName = Path.Combine(uploadFolder, filePath + suffix); 

     //Let the image builder add the correct extension based on the output file type 
     fileName = ImageBuilder.Current.Build(new ImageJob(dirFile, fileName, new Instructions(versions[suffix]), false, true)).FinalPath; 
    } 
} 
相關問題