I am generating Thumbnails, Everything goes fine and smooth but there is a problem during the creation of sub folder. Suppose:
C:\Users\a\Desktop\b test\Iceland\Haskolinn2 the destination thumbnail folder will look like this: C:\Users\a\Desktop\a test\Iceland *C:\Users\a\Desktop\a test\Haskolinn2*
it must look like C:\Users\a\Desktop\a test\Iceland\Haskolinn2
here is the code:
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("*.*", S开发者_如何学GoearchOption.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);
}
}
}
}
}
From what I can see, I guess the Path.Combine
call is not doing what you think it should.
When given two fully qualified paths, Path.Combine basically ignores the first, and returns the second. Example:
string path1 = @"C:\Test\Path\"
string path2 = @"C:\Other\Path\"
Console.WriteLine(Path.Combine(path1, path2)); //prints C:\Other\Path
In your code this means that you are not using the destination folder for the thumbnails, but the source folder. You could try stripping the full path down to a folder name, like
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
After the question edit:
The problem is that for a structure like
...A
|-B
|-C
your Directory.GetDirectories
call will return strings like "..\A", "..\A\B" "..\A\C".
When those string are used in a DirectoryInfo
constructor you get directories with names of "A", "B" and "C", so in effect, you are linearizing a tree structure (Things could get really interesting if the B folder had a subfolder called A :))
You could do something like:
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
}
}
This example will copy a folder to another location, just replace the File.Copy call with your logic.
精彩评论