我正在生成缩略图,一切顺利,但在创建子文件夹时出现问题。假设:
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);
}
}
}
}
}
答案 0 :(得分: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调用。