【问题标题】:folder creation problem during thumbnail generation c#缩略图生成期间的文件夹创建问题c#
【发布时间】:2011-03-22 19:54:31
【问题描述】:

我正在生成缩略图,一切顺利,但在创建子文件夹时出现问题。假设:

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

它必须看起来像 C:\Users\a\Desktop\a test\Iceland\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);



                }

            }
        }
    }


}

【问题讨论】:

  • 问题出在哪里(任何错误信息也可能有所帮助)?
  • @SWeko 没有错误,但是在物理文件系统上,目录结构和原来的不一样。子文件夹不是作为子文件夹创建的,而是作为父文件夹创建的。假设文件夹 A 包含两个子文件夹 (B,C),所以在目的地它会像 (A,B,C) 这是错误的。
  • jak 中的文件夹结构是什么?而且你在上面的示例中没有使用b
  • @conqenator 我忘了更新问题,现在 jak 被 b 替换了。
  • @conqenator 我已经更新了代码,你可以看到这个类是如何工作的。并可以建议我。

标签: c#


【解决方案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 调用替换为您的逻辑即可。

【讨论】:

  • @SWeko 那么我需要在 Path.Combine 中更改什么?还是应该将 Path.Combine 替换为其他功能?
  • @SWeko 我已经更新了代码,你可以看到这个类是如何工作的。并可以建议我。
  • @safi,我现在看到了你的问题,你需要递归来解决这个问题。将更改答案以反映它。
  • @SWeko,谢谢,您是否更改了答案?我正在等待解决这个问题。
  • @Safi,已修改,现在您应该看到问题出在哪里了。
猜你喜欢
  • 2014-01-28
  • 2010-11-26
  • 2015-12-25
  • 2011-02-25
  • 2011-02-08
  • 1970-01-01
  • 2012-02-18
  • 2019-02-14
  • 2019-04-10
相关资源
最近更新 更多