缩略图生成问题 [英] thumbnails generation problem
本文介绍了缩略图生成问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
其中strin [] bb = Directory.GetDirectories("c:\ images \","*.*",SearchOption.AllDirectories);
问题在于,它会生成缩略图,但是会忽略文件夹结构,假设
文件夹a->子文件夹b->子文件夹c,
结果将是
文件夹a,
文件夹b,
文件夹c.
并且应该与原始文件夹相同.
where as strin []bb = Directory.GetDirectories("c:\images\", "*.*", SearchOption.AllDirectories);
the problem is that it generate thumbnails but it ignore the folder strudcture, suppose
folder a->sub folder b->sub folder c,
so the result will be
folder a,
folder b,
folder c.
and it should be the same as the original folder .
<pre lang="cs">public void CreateThumbnail(string[] bb, double wid, double hght, bool Isprint)
{
string saveAt = "i:\\check\\a test\\";
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, f.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);
}
}
}
}
推荐答案
对于此问题,您应该使用像这样的递归方法:
For this problem, you should use a recursive method like this:
public void ProcessDirectory(string directory)
{
var files = Directory.GetFiles(directory);
foreach (var fileName in files)
{
/* do the file processing with the filename */
}
var subDirectories = Directory.GetDirectories(directory);
if (subDirectories.Length > 0)
{
foreach (var subDirectory in subDirectories)
{
ProcessDirectory(subDirectory);
}
}
}
并且仅获取当前目录的文件(并遍历所有子目录).
希望这会有所帮助.
最好的问候,
停止
and only get the files for the current directory (and cycle over all sub-directories).
Hope this helps.
Best regards,
Stops
这篇关于缩略图生成问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文