【发布时间】: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#