Click here to Skip to main content
15,899,126 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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:

What I have tried:

C#
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");
   }
Posted
Updated 31-May-17 17:56pm

Ummmm.... you might want to figure our how to use the debugger so you can follow what your code is really doing.

You're not loading a single image. Your code is loading into a PictureBox (that can only display one image at a time), every image in a folder and the entire subtree under that folder, one at a time.

There is really no way to speed that up because GetFiles is searching the root folder and then searching the entire subtree of that folder, building a list of "*.jpg" files. This can be a time consuming process, especially on a network share like you're doing.

Once that is done, you're looping through each of those returned file paths and telling a PictureBox (which can only display one image at a time) to load and then forget about every one of those files. Only the last filepath in the list returned by GetFiles is going to be displayed.

I have no idea what you're really trying to do with this code.

Oh, and it leaks resources like crazy too. You have to Dispose each Bitmap object you create and you're not doing that.
 
Share this answer
 
v2
Comments
Richard Deeming 31-May-17 17:48pm    
"Your code is loading ... every image in a folder and the entire subtree under that folder, one at a time."

Erm, surely the unconditional break; statement at the end of the foreach loop will prevent that from happening? :)
Dave Kreskowiak 31-May-17 17:59pm    
Whoops. Missed the break statement.
The DirectoryInfo.GetFiles method finds all matching files and loads them into an array before returning control to your code. Your code then reads the first matching file, and ignores the rest.

Try using EnumerateFiles instead; that returns each file as it's found, without waiting for the search to complete:
C#
bool fileFound = false;
DirectoryInfo di = new DirectoryInfo(baseFolder);
foreach (var file in di.EnumerateFiles(imgName, SearchOption.AllDirectories))
{
    pictureBox1.Visible = true;
    pictureBox1.Image = Image.FromFile(file.FullName);
    fileFound = true;
    break;
}
if (!fileFound)
{
    ...
}

You could even use LINQ to simplify that:
C#
DirectoryInfo di = new DirectoryInfo(baseFolder);
FileInfo file = di.EnumerateFiles(imgName, SearchOption.AllDirectories).FirstOrDefault();
if (file != null)
{
    pictureBox1.Visible = true;
    pictureBox1.Image = Image.FromFile(file.FullName);
}
else
{
    ...
}
 
Share this answer
 
I already try it but still slow. :(
 
Share this answer
 
Comments
Richard Deeming 1-Jun-17 11:57am    
But you've obviously solved the problem.

How do I know? Because you've posted this as a "solution", and then accepted it, marking your problem as "solved".

Delete this fake "solution".
Hamizah AHMAD TALHAH 2-Jun-17 3:07am    
Sorry. Maybe I accidentally click that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900