2011-03-22 61 views
0

我生成縮略圖,一切順利,但在創建子文件夾時出現問題。假設:縮略圖生成期間的文件夾創建問題c#

C:\用戶\ A \桌面\ B測試\冰島\ Haskolinn2 目標縮略圖文件夾將是這樣的: C:\用戶\ A \桌面\測試\冰島 * C:\用戶\ A \桌面\測試\ Haskolinn2 *

它必須看起來像 C:\用戶\ A \桌面\測試\冰島\ Haskolinn2

這裏是代碼:

public void CreateThumbnail(double wid, double hght, bool Isprint) 
    { 


     string saveAt = "C:\\Users\\a\\Desktop\\a test"; 

string b= "C:\\Users\\a\\Desktop\\b test\\iceland"   
string [] bb = Directory.GetDirectories(b, "*.*", SearchOption.AllDirectories); 

     foreach (string path in bb) 
     { 
      var directory = new DirectoryInfo(path); 

      string outputPath = Path.Combine(saveAt, directory.Name); 
      foreach (FileInfo f in directory.GetFiles("*.*", SearchOption.AllDirectories)) 
      { 



       if (f.DirectoryName != directory.FullName) 
       { 
        outputPath = Path.Combine(saveAt, directory.Name); 

       } 
       if (!Directory.Exists(outputPath)) 
       { 
        Directory.CreateDirectory(outputPath); 

       } 


       using (Image imagesize = Image.FromFile(f.FullName)) 

       using (Bitmap bitmapNew = new Bitmap(imagesize)) 
       { 
        double maxWidth = wid; 
        double maxHeight = hght; 
        int w = imagesize.Width; 

        int h = imagesize.Height; 
        // Longest and shortest dimension 
        int longestDimension = (w > h) ? w : h; 

        int shortestDimension = (w < h) ? w : h; 
        // propotionality 
        float factor = ((float)longestDimension)/shortestDimension; 

        // default width is greater than height  
        double newWidth = maxWidth; 
        double newHeight = maxWidth/factor; 

        // if height greater than width recalculate 
        if (w < h) 
        { 
         newWidth = maxHeight/factor; 

         newHeight = maxHeight; 
        } 

        string fileName = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(f.Name) + ".jpeg"); 

        bitmapNew.GetThumbnailImage((int)newWidth, (int)newHeight,() => false, IntPtr.Zero) 

         .Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); 



       } 

      } 
     } 
    } 


} 
+0

問題是什麼(也有任何錯誤消息可能有幫助)? – SWeko 2011-03-22 09:16:05

+0

@SWeko不存在錯誤,但在物理文件系統上,目錄結構與原始文件系統不同。子文件夾不是作爲子文件夾創建的,而是作爲父文件夾創建的。假設文件夾A包含兩個子文件夾(B,C),所以在目的地它會像(A,B,C)這是錯誤的。 – safi 2011-03-22 09:18:26

+0

'jak'中的文件夾結構是什麼?在上面的例子中你沒有使用過'b'。 – 2011-03-22 09:25:26

回答

1

從我所看到的,我猜Path.Combine呼叫沒有做什麼,你認爲它應該。

當給出兩個完全合格的路徑時,Path.Combine基本上忽略第一個,並返回第二個。例如:

string path1 = @"C:\Test\Path\" 
string path2 = @"C:\Other\Path\" 
Console.WriteLine(Path.Combine(path1, path2)); //prints C:\Other\Path 

在您的代碼中,這意味着您沒有使用縮略圖的目標文件夾,而是使用源文件夾。你可以嘗試剝離的完整路徑到一個文件夾的名稱,如

string path1 = @"C:\Test\Path\" 
string path2 = @"C:\Other\FolderToCombine\" 
DirectoryInfo di = new DirectoryInfo(path2); 

Console.WriteLine(Path.Combine(path1, di.Name)); //prints C:\Test\Path\FolderToCombine 

問題編輯後:
的問題是,對於像

...A 
    |-B 
    |-C 

Directory.GetDirectories呼叫結構返回字符串,如「.. \ A」,「.. \ A \ B」「.. \ A \ C」。 當這些字符串在DirectoryInfo構造函數中使用時,您將得到名稱爲「A」,「B」和「C」的目錄,因此實際上,您正在線性化樹結構(如果B文件夾有一個子文件夾名爲A :))

你可以這樣做:

public void CreateThumbnail(double wid, double hght, bool Isprint) 
{ 
    string saveAt = "C:\\Users\\a\\Desktop\\a test"; 
    string b= "C:\\Users\\a\\Desktop\\b test\\iceland" 
    ProcessFolder(b, saveAt, wid, hght, Isprint); 
} 

public static void ProcessFolder(string sourceFolder, string destFolder, double wid, double hght, bool Isprint) 
{ 
    //create the dest folder if it does not exist 
    Directory.CreateDirectory(destFolder); 

    //get info about the source folder 
    DirectoryInfo diSource = new DirectoryInfo(sourceFolder); 

    //get the source files (only in the current source folder) 
    foreach (FileInfo f in diSource.GetFiles("*.*")) 
    { 
    //calculate the destination file name 
    string destFileName = Path.Combine(destFolder, f.Name); 
    //thumbnail processing here 

    //quick test 
    File.Copy(f.FullName, destFileName); 
    } 

    //get all subfolders for the current folder 
    foreach (string dir in Directory.GetDirectories(sourceFolder, "*.*")) 
    { 
    //calculate the new output folder for a given subfolder 
    // if the source folder is \src\a\, and the dest folder is \dest\ 
    // this results in \dest\a 
    DirectoryInfo diSubfolder = new DirectoryInfo(dir); 
    string outputPath = Path.Combine(destFolder, diSubfolder.Name); 

    ProcessFolder(dir, outputPath, wid, hght, Isprint); //call recursively 
    } 
} 

這個例子將一個文件夾複製到另一個位置,只需更換File.Copy通話用你的邏輯。

+0

@SWeko所以我需要改變Path.Combine?或者我應該用其他函數替換Path.Combine? – safi 2011-03-22 09:24:13

+0

@Sekek我已經更新了代碼,你可以看到它的類是如何工作的。並可以建議我。 – safi 2011-03-22 09:30:54

+1

@safi,我現在看到你的問題了,你需要遞歸來解決這個問題。將改變答案來反映它。 – SWeko 2011-03-22 09:34:48