Hi All,

I was wondering if anyone could tell me a better doing this.
I have a thousand image in each folder going from 0 to 999 and i want to be able to select an image in any folder and load it.

           Thanks in advance for the reply. if (imageCounter_img2 > -1 && imageCounter_img2 < 999)

                                 path_img2 = path_img2.Remove(path_img2.LastIndexOf('\\'));
                                 path_img2 = path_img2 + "\\0";
                                 imageCounter_img2 = imageCounter_img2 - 1000;
                                 imageno_right.Text = path_img2;
                                 imageno_right.Text = imageCounter_img2.ToString();
                             }

                if (imageCounter_img2 >999 && imageCounter_img2 <1999)
                             {
                                 path_img2 = path_img2.Remove(path_img2.LastIndexOf('\\'));
                                 path_img2 = path_img2 + "\\1";
                                 imageCounter_img2 = imageCounter_img2 - 1000;
                                 dir_right.Text = path_img2;  
                                 imageno_right.Text = imageCounter_img2.ToString();
                             }

etc

       Khan

No need to repeat statements that are exactly the same. Just move them outside the if statements. Also, you don't need to use "&&", just order them from high numbers to low numbers (ex: 3999, 2999, 1999, 999, -1). Why remove the "\" only to add it back in. Additionally, LastIndexOf finds the last occurrence of "\", which may or may not be at the end of the path.

if (!path_img2.EndsWith(@"\"))
{
    path_img2 = path_img2 + @"\";
}//if

if (imageCounter_img2 > 999)
{
    path_img2 += "1";
}//if
else if (imageCounter_img2 > -1)
{
    path_img2 += "0";
}//else if

imageCounter_img2 = imageCounter_img2 - 1000;
dir_right.Text = path_img2;
imageno_right.Text = imageCounter_img2.ToString();

Edited 2 Years Ago by cgeier

hi cgeier,

thanks for the reply.
what im trying to is avoid having lots of conditions.

there will be a directory called say images and within the directory there will be lots of folders say 0 to 15 for a total of 15000 images and in each folder there is be 1000 images.

So what i want to is be able to press a button in a form that will load an image for any folder.
For example if i type load image 12050, i neeed to take the the last \ in directory path and add 12 to the path directory and load image 50 in that folder , so therefore i also need to subtract 12000 for the load parameter button.

doing this way needs alot of conditions and i would like to come up with a better solutions.

Thanks again.
Khan.

This sounds like a variation of a repository library I wrote. The ID structure wasn't quite that simplistic, but the concept is similar. Something like this would be a start for you:

private void buttonLoad_Click(object sender, EventArgs args)
{
    var path = GetImagePath(
        Folder, 
        Convert.ToInt32(numericUpDownImage.Value), 
        ".tif");

    if (File.Exists(path))
    {
        // Load the image
    }
    else
    {
        // Handle no image
    }
}

private string GetImagePath(string root, int imageId, string ext)
{
    int folderId = imageId / 1000;
    int imageId = imageId % 1000;

    if (!ext.StartsWith("."))
    {
        ext = "." + ext;
    }

    return Path.Combine(root, folderId.ToString(), imageId + ext);
}
This question has already been answered. Start a new discussion instead.