How can i get from the List<string> of files each time another file?

Helem Shoshi

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

Tim Schmelter

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How i can iterate list to get 10 elements each time in java

분류에서Dev

How can I get the contents of a file one line at a time?

분류에서Dev

How can I recreate a list of list of floats from the contents of a file?

분류에서Dev

How can I print lines from a file to separate files

분류에서Dev

Is there anyway I can get the time of cached file?

분류에서Dev

How can I capitalize a letter from a word one at a time, then add each instance of the word with a caps letter into a array?

분류에서Dev

How can I copy a text file into another?

분류에서Dev

How can I read a text file from the terminal and save the output to another file in java?

분류에서Dev

how can I move files with specific names from one folder to another

분류에서Dev

How can I get a list of files on my computer that are not "owned" by any package?

분류에서Dev

How to Read string from file and compare to each line of second file

분류에서Dev

How can I post data to another file and render from it using DomPDF

분류에서Dev

How can i parse a xml file from the http URL without downloading the file and print the desired string?

분류에서Dev

How can I call a batch file within another batch file?

분류에서Dev

How can I get Git to ignore/drop files I have deleted or that are only file-system artifacts (ending in ~)?

분류에서Dev

How can I check if a line contains specific string from a list of strings?

분류에서Dev

how can i parse data from the url from one activity containing using a list view to another activity according to these codes?

분류에서Dev

How do i copy files from one directory to another directory?

분류에서Dev

How can I get an array from an If statement?

분류에서Dev

How can I get a multiple page pdf output from php file?

분류에서Dev

Android - How can i create a "black list" for the files of my application

분류에서Dev

How to get string from List<String> by JBOSS Drools

분류에서Dev

How can I reduce the live date/time string with javascript?

분류에서Dev

How can I read in excel files by looking for a string pattern?

분류에서Dev

How can I find all files that do NOT contain a text string?

분류에서Dev

How can I convert date time from time zone A to time zone B in Joda time?

분류에서Dev

grep search pattern from file containing list of patterns, write result of each pattern to individual files

분류에서Dev

How could I search pattern of one file in to another and save the result of each pattern in new file

분류에서Dev

How can I preview Windows files without file extension?

Related 관련 기사

  1. 1

    How i can iterate list to get 10 elements each time in java

  2. 2

    How can I get the contents of a file one line at a time?

  3. 3

    How can I recreate a list of list of floats from the contents of a file?

  4. 4

    How can I print lines from a file to separate files

  5. 5

    Is there anyway I can get the time of cached file?

  6. 6

    How can I capitalize a letter from a word one at a time, then add each instance of the word with a caps letter into a array?

  7. 7

    How can I copy a text file into another?

  8. 8

    How can I read a text file from the terminal and save the output to another file in java?

  9. 9

    how can I move files with specific names from one folder to another

  10. 10

    How can I get a list of files on my computer that are not "owned" by any package?

  11. 11

    How to Read string from file and compare to each line of second file

  12. 12

    How can I post data to another file and render from it using DomPDF

  13. 13

    How can i parse a xml file from the http URL without downloading the file and print the desired string?

  14. 14

    How can I call a batch file within another batch file?

  15. 15

    How can I get Git to ignore/drop files I have deleted or that are only file-system artifacts (ending in ~)?

  16. 16

    How can I check if a line contains specific string from a list of strings?

  17. 17

    how can i parse data from the url from one activity containing using a list view to another activity according to these codes?

  18. 18

    How do i copy files from one directory to another directory?

  19. 19

    How can I get an array from an If statement?

  20. 20

    How can I get a multiple page pdf output from php file?

  21. 21

    Android - How can i create a "black list" for the files of my application

  22. 22

    How to get string from List<String> by JBOSS Drools

  23. 23

    How can I reduce the live date/time string with javascript?

  24. 24

    How can I read in excel files by looking for a string pattern?

  25. 25

    How can I find all files that do NOT contain a text string?

  26. 26

    How can I convert date time from time zone A to time zone B in Joda time?

  27. 27

    grep search pattern from file containing list of patterns, write result of each pattern to individual files

  28. 28

    How could I search pattern of one file in to another and save the result of each pattern in new file

  29. 29

    How can I preview Windows files without file extension?

뜨겁다태그

보관