0

I'm currently working on windows form application which allow user to retrieve image after searching at textbox. The problem is the image is load very slow. How can I overcome this problem to speed up the loading? If anyone has suggestions for a faster way to retrieve these images, it would be greatly appreciated. Here is my code:

  string baseFolder = @"\\\\jun01\\hr\\photo";
  string imgName =  "*" + textBoxEmplNo.Text  + "*.jpg";

  //Bool to see if file is found after checking all
  bool fileFound = false;

  DirectoryInfo di = new DirectoryInfo(baseFolder);
  foreach (var file in di.GetFiles(imgName, SearchOption.AllDirectories))

   {
    pictureBox1.Visible = true;
    pictureBox1.Image = Image.FromFile(file.FullName);

    fileFound = true;
    break;
   }

    if (!fileFound)
   {
    pictureBox1.Visible = true;
    pictureBox1.Image = Image.FromFile(@"\\\\jun01\\hr\\photo\\No-image-
    found.jpg");
   }
Miza
  • 49
  • 1
  • 1
  • 8

2 Answers2

0

It's very likely here that the slowness you are experiencing is due to the wildcard search on all files in a remote directory, and/or potentially transferring a large file over the network for display in the PictureBox. You should time these two operations using a profiler (see for example https://www.simple-talk.com/dotnet/net-performance/the-why-and-how-of-net-profiling/) and consider optimizing the operations that take the most time.

Some possibilities might include pre-downloading all the images before the user makes a selection or downloading on a background thread while displaying a "Please wait" message on the UI.

Chris Shain
  • 50,833
  • 6
  • 93
  • 125
  • can u give me any specific code to overcome this problem? I'm a newbie in programming. Then I cannot see the picture in that link. Thank you. – Miza May 31 '17 at 01:22
  • No, unfortunately I can't help with that. Even if I wanted to, I am not on your network and therefore have no way of accurately diagnosing exactly what is causing the issue. This is one of those things that you are going to need to experiment with yourself. I'd suggest starting with a google search for .net asynchronous image download. – Chris Shain May 31 '17 at 02:59
  • Its ok. I will try. Thanks :) – Miza May 31 '17 at 03:44
0

It could be the case that the code slowness is due to the wildcard search on all files: (e.g. di.GetFiles takes too long)

  • Too many files in your top folder/subfolders.

  • The network connection is too slow to fetch file information or photos are too big. (remote directory access could be slow - you can check by copying a big file say 1GB to your remote directory and copy back to your PC)

  • The photos may be stored with different aspect ratios to your picture box size. (Rendering takes a while if resizing needs to happen)

For starters, let us assume your folder has too many files (1000s), then we need to do a smarter search for the first file folder by folder:

string baseFolder = @"\\\\jun01\\hr\\photo";
string imgName =  "*" + textBoxEmplNo.Text  + "*.jpg";

var file = FindFirstFile(baseFolder, imgName);
if (!string.IsNullorEmpty(file))
{
  pictureBox1.Visible = true;
  pictureBox1.Image = Image.FromFile(file);
}
else
{
 pictureBox1.Visible = true;
 pictureBox1.Image = Image.FromFile(@"\\\\jun01\\hr\\photo\\No-image-found.jpg");
}

where FindFirstFile is taken from Markus's answer as:

public static string FindFirstFile(string path, string searchPattern)
{
    string[] files;

    try
    {
        // Exception could occur due to insufficient permission.
        files = Directory.GetFiles(path, searchPattern, SearchOption.TopDirectoryOnly);
    }
    catch (Exception)
    {
        return string.Empty;
    }

    if (files.Length > 0)
    {
        return files[0];
    }
    else
    {
        // Otherwise find all directories.
        string[] directories;

        try
        {
            // Exception could occur due to insufficient permission.
            directories = Directory.GetDirectories(path);
        }
        catch (Exception)
        {
            return string.Empty;
        }

        // Iterate through each directory and call the method recursivly.
        foreach (string directory in directories)
        {
            string file = FindFirstFile(directory, searchPattern);

            // If we found a file, return it (and break the recursion).
            if (file != string.Empty)
            {
                return file;
            }
        }
    }

    return string.Empty;
}
mas_oz2k1
  • 2,851
  • 3
  • 34
  • 41
  • wheres to put _FindFirstFile_ code? Sorry, can you explain me in detail. I don't know much in C#. – Miza Jun 01 '17 at 01:01
  • Just add it to your form code, it is just a helper method – mas_oz2k1 Jun 01 '17 at 12:09
  • I just added but it show error like this _Error 1 'char' does not contain a definition for 'FullName' and no extension method 'FullName' accepting a first argument of type 'char' could be found (are you missing a using directive or an assembly reference?) C:\Users\hamizah\Documents\Visual Studio 2013\WebSites\EV\EV\EV\Form1.cs 208 84 EV _. I don't know the error is cause. What's the problem? – Miza Jun 02 '17 at 02:46
  • See my latest changes regarding file var usage – mas_oz2k1 Jun 05 '17 at 21:05
  • What is slow? please use a profiler and post a comparison between previous and current code so thatwe can compare where the bottlenecks are and whether there has been an improvement. WE need measurements in seconds. – mas_oz2k1 Jun 16 '17 at 08:21