C# 缩略图生成过程中的文件夹创建问题#

C# 缩略图生成过程中的文件夹创建问题#,c#,C#,我正在生成缩略图,一切都很顺利,但在创建子文件夹的过程中出现了问题。假设: C:\Users\a\Desktop\b test\冰岛\Haskolinn2 目标缩略图文件夹将如下所示: C:\Users\a\Desktop\a test\冰岛 *C:\Users\a\Desktop\a test\Haskolinn2* 看起来一定像 C:\Users\a\Desktop\a test\冰岛\Haskolinn2 代码如下: public void CreateThumbnail(double

我正在生成缩略图,一切都很顺利,但在创建子文件夹的过程中出现了问题。假设:

C:\Users\a\Desktop\b test\冰岛\Haskolinn2 目标缩略图文件夹将如下所示: C:\Users\a\Desktop\a test\冰岛 *C:\Users\a\Desktop\a test\Haskolinn2*

看起来一定像 C:\Users\a\Desktop\a test\冰岛\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);



                }

            }
        }
    }


}
public void CreateThumbnail(双宽、双高宽、bool-Isprint)
{
string saveAt=“C:\\Users\\a\\Desktop\\a test”;
string b=“C:\\Users\\a\\Desktop\\b test\\冰岛”
字符串[]bb=Directory.GetDirectories(b,“***”,SearchOption.AllDirectories);
foreach(bb中的字符串路径)
{
var directory=newdirectoryinfo(路径);
字符串outputPath=Path.Combine(saveAt,directory.Name);
foreach(directory.GetFiles(“*.*”,SearchOption.AllDirectories)中的FileInfo f)
{
if(f.DirectoryName!=directory.FullName)
{
outputPath=Path.Combine(saveAt,directory.Name);
}
如果(!Directory.Exists(outputPath))
{
CreateDirectory(outputPath);
}
使用(Image imagesize=Image.FromFile(f.FullName))
使用(位图bitmapNew=新位图(图像大小))
{
双最大宽度=wid;
双最大高度=hght;
int w=图像大小。宽度;
int h=图像大小。高度;
//最长和最短尺寸
int最长维度=(w>h)?w:h;
int最短尺寸=(wfalse,IntPtr.Zero)
.Save(文件名,System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
}
}

从我看到的情况来看,我猜
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调用。

问题是什么(还有任何错误消息可能会有帮助)?@SWeko没有错误,但在物理文件系统上,目录的结构与原始目录的结构不同。子文件夹不是创建为子文件夹,而是创建为父文件夹。假设文件夹A包含两个子文件夹(B,C),那么在目的地中它将类似于(A,B,C),这是错误的?在上面的示例中,您没有使用
b
。@conqenator我忘了更新问题,现在jak被b替换了。@conqenator我更新了代码,您可以看到类的工作方式。我可以建议我。@SWeko所以我需要改变什么路径。合并?或者我应该替换Path.Combine和其他函数吗?@SWeko我已经更新了代码,你可以看到这个类是如何工作的。我可以建议我。@safi,我现在明白你的问题了,你需要递归来解决这个问题。将更改答案以反映它。@SWeko,谢谢,您更改了答案吗?我正在等待解决此问题。@Safi,已修改,现在您应该看到问题所在。