加快将图像检索到图片框中 [英] Speed up retrieve image into picturebox

查看:95
本文介绍了加快将图像检索到图片框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Windows窗体应用程序,它允许用户在文本框中搜索后检索图像。问题是图像加载速度很慢。如何克服此问题以加快加载?如果有人建议以更快的方式检索这些图像,我们将不胜感激。这是我的代码:



我尝试过:



< pre lang =c#> string baseFolder = @ \\\\jun01\\hr\\photo;
string imgName = * + textBoxEmplNo.Text + *。jpg;

// Bool查看是否在检查完所有文件后找到了文件
bool fileFound = false ;

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

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

fileFound = true ;
break ;
}

if (!fileFound)
{
pictureBox1.Visible = ;
pictureBox1.Image = Image.FromFile( @ \\\\ jun01 \\\ \\\\\\\ No-image-
found.jpg
);
}

解决方案

嗯...你可能想知道如何使用调试器,这样你就可以按照你的代码真的在做。



你没有加载单个图像。您的代码加载到PictureBox(一次只能显示一个图像),文件夹中的每个图像以及该文件夹下的整个子树,一次一个。



实际上没办法加快速度,因为GetFiles正在搜索根文件夹,然后搜索该文件夹的整个子树,构建一个* .jpg文件列表。这可能是一个耗时的过程,尤其是在您正在进行的网络共享上。



一旦完成,您将遍历每个返回的文件路径和告诉PictureBox(一次只能显示一个图像)加载然后忘记这些文件中的每一个。只会显示GetFiles返回的列表中的最后一个文件路径。



我不知道你真正想用这段代码做什么。 />


哦,它也像疯了一样泄漏资源。你必须处理你创建的每个Bitmap对象而你没有这样做。


DirectoryInfo.GetFiles 方法找到所有匹配的文件并在将控制权返回给代码之前将它们加载到数组中。然后你的代码读取第一个匹配的文件,忽略其余的。



尝试使用 EnumerateFiles ;返回找到的每个文件,而不等待搜索完成:

  bool  fileFound = < span class =code-keyword> false ; 
DirectoryInfo di = new DirectoryInfo(baseFolder);
foreach var 文件 in di.EnumerateFiles(imgName,SearchOption.AllDirectories))
{
pictureBox1.Visible = true ;
pictureBox1.Image = Image.FromFile(file.FullName);
fileFound = true ;
break ;
}
if (!fileFound)
{
...
}



您甚至可以使用LINQ来简化:

 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);
}
其他
{
...
}


我已经尝试过但仍然很慢。 :(

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:

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");
   }

解决方案

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.


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:

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:

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
{
    ...
}


I already try it but still slow. :(


这篇关于加快将图像检索到图片框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆