代码:
var files = Directory.EnumerateFiles("C:\\coneimages", "*.*", SearchOption.AllDirectories)
.Where(s => s.EndsWith(".mp3") || s.EndsWith(".gif"));
foreach (string f in files)
{
filesl.Add(f);
}
for (int i = 0; i < filesl.Count; i++)
{
Bitmap bmp1 = new Bitmap(filesl[i]);
Bitmap bmp2 = new Bitmap(@"c:\coneimages\PictureBox_Images1.gif");
Bitmap bmp3 = new Bitmap(GenerateImage(bmp2, bmp1));
bmp3.Save(@"c:\coneimages\merged.bmp");
}
文件为IEnumerable<string>
,filesl
为List<string>
问题在于:Bitmap bmp1 = new Bitmap(filesl[i]);
在此之前:Bitmap bmp1 = Bitmap(@"c:\coneimages\Cone_Images1.gif");
问题是我在List<string>
列表中的第一个我拥有所有Cone_images文件,例如:
Cone_images1,Con_images2,Con_images3......Con_images360
然后我在同一个列表中:
PictureBox_Images1,PictureBox_Images2....PictureBox_Images360
在循环中我需要做的是每个迭代两个文件并合并它们。
例如文件:Con_images1 with PictureBox_Images1
接下来迭代循环中的几个文件:Con_images2 with PictureBox_Images2
.....最后一次迭代将是:Con_images360 with PictureBox_Images360
答案 0 :(得分:3)
因此,一种类型的文件名始终以"Cone_images"
开头,而另一种始终以"PictureBox_Images"
开头,以数字部分结尾?然后我首先将它们分开。最后,您可以在使用数字部分命令它们之后使用Enumerable.Zip
:
var allFiles =Directory.EnumerateFiles("C:\\coneimages", "*.*", SearchOption.AllDirectories)
.Where(s => s.EndsWith(".mp3") || s.EndsWith(".gif"));
var coneFiles = allFiles
.Where(f => Path.GetFileNameWithoutExtension(f).StartsWith("Cone_images", StringComparison.InvariantCultureIgnoreCase))
.Select(f => new
{
File = f,
NumberPart = String.Concat(Path.GetFileNameWithoutExtension(f).SkipWhile(c => !char.IsDigit(c)).TakeWhile(char.IsDigit))
}).Where(x => x.NumberPart.Any())
.Select(x => new { x.File, Number = int.Parse(x.NumberPart) })
.OrderBy(x => x.Number);
var pictureBoxFiles = allFiles
.Where(f => Path.GetFileNameWithoutExtension(f).StartsWith("PictureBox_Images", StringComparison.InvariantCultureIgnoreCase))
.Select(f => new
{
File = f,
NumberPart = String.Concat(Path.GetFileNameWithoutExtension(f).SkipWhile(c => !char.IsDigit(c)).TakeWhile(char.IsDigit))
}).Where(x => x.NumberPart.Any())
.Select(x => new { x.File, Number = int.Parse(x.NumberPart) })
.OrderBy(x => x.Number);
var bothFileTypes = coneFiles.Zip(pictureBoxFiles, (cone, pic) => new { cone, pic });
foreach (var xy in bothFileTypes)
{
Bitmap bmp1 = new Bitmap(xy.cone.File);
Bitmap bmp2 = new Bitmap(xy.pic.File);
Bitmap bmp3 = new Bitmap(GenerateImage(bmp2, bmp1));
bmp3.Save(@"c:\coneimages\merged.bmp");
}
这是另一种类似于使用Enumerable.GroupBy
的Sayse的方法。它不依赖于订单,也不会压缩可能不属于一起的文件。它按最后的数字分组,并标识每个文件的类型:
string coneIdentifier = "Cone_images";
string picIdentifier = "PictureBox_Images";
var numberGroups = Directory.EnumerateFiles("C:\\coneimages", "*.*", SearchOption.AllDirectories)
.Where(f => Path.GetExtension(f).Equals(".mp3", StringComparison.InvariantCultureIgnoreCase) || Path.GetExtension(f).Equals(".gif", StringComparison.InvariantCultureIgnoreCase))
.Select(f => new { File = f, FileName = Path.GetFileNameWithoutExtension(f) })
.Where(x => x.FileName.StartsWith(coneIdentifier, StringComparison.InvariantCultureIgnoreCase) || x.FileName.StartsWith(picIdentifier, StringComparison.InvariantCultureIgnoreCase))
.Select(x => new
{
x.File,
x.FileName,
NumberPart = String.Concat(x.FileName.SkipWhile(c => !char.IsDigit(c)).TakeWhile(char.IsDigit)),
Type = String.Concat(x.FileName.TakeWhile(c => !char.IsDigit(c)))
})
.Where(x => x.NumberPart.Length > 0 && x.Type.Length > 0)
.GroupBy(x => x.NumberPart);
foreach (var grp in numberGroups)
{
var cone = grp.FirstOrDefault(x => x.Type.Equals(coneIdentifier, StringComparison.InvariantCultureIgnoreCase));
var pic = grp.FirstOrDefault(x => x.Type.Equals(picIdentifier, StringComparison.InvariantCultureIgnoreCase));
if (cone == null || pic == null) continue;
Bitmap bmp1 = new Bitmap(cone.File);
Bitmap bmp2 = new Bitmap(pic.File);
// ...
}
答案 1 :(得分:0)
如果您的Cone_images&amp;的命名PictureBox总是一样的,这可能会有所帮助:
Bitmap bmp1 = new Bitmap(filesl[i]);
string filePictureBox = string.Format(@"c:\coneimages\PictureBox_Images{0}.gif", i);
// check if file exists
if(File.Exists(filePictureBox))
{
Bitmap bmp2 = new Bitmap(filePictureBox);
Bitmap bmp3 = new Bitmap(GenerateImage(bmp2, bmp1));
bmp3.Save(@"c:\coneimages\merged.bmp");
}