The code:
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");
}
files is IEnumerable<string>
and filesl
is List<string>
The problem is here: Bitmap bmp1 = new Bitmap(filesl[i]);
Before it was like this: Bitmap bmp1 = Bitmap(@"c:\coneimages\Cone_Images1.gif");
The problem is that i have many files in the List<string>
First in the List i have all th Cone_images files for example:
Cone_images1,Con_images2,Con_images3......Con_images360
Then in the same List after i have:
PictureBox_Images1,PictureBox_Images2....PictureBox_Images360
In the loop what i need to do is to take each itertion two files and merge them. For example the files: Con_images1 with PictureBox_Images1
Next itertion in the loop next couple of files: Con_images2 with PictureBox_Images2
.....the last itertion will be: Con_images360 with PictureBox_Images360
So the filename for the one type starts always with "Cone_images"
whereas the other always starts with "PictureBox_Images"
and ends with the numeric part? Then i'd separate them in the first place. Finally you could use Enumerable.Zip
after you've ordered them by the number-part:
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");
}
Here is another approach similar to Sayse's using Enumerable.GroupBy
. It does not rely on the order and doesn't zip files which possibly don't belong together. It groups by the number at the end and identifies the type of each file:
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);
// ...
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다