帮我优化C#中的代码 [英] Help me to optimize code in C#

查看:55
本文介绍了帮我优化C#中的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我有一个保存.jpg文件路径的列表

私有列表< string> images = new List< string>();

我要用图片填充它.例如,用户选择了一个文件夹
使用FolderBrowserDialog

Hi everyone!

I have a list to save .jpg files path

private List<string> images = new List<string>();

I want to fill it with pictures. For example, user selected a folder
with FolderBrowserDialog

  if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  {
          // --- clear previous results ---
         this.images.Clear();
      
      { // I intentionally put brackets here to create a scope ---
         FileInfo[] files = 
(new DirectoryInfo(folderBrowserDialog1.SelectedPath)).GetFiles("*.jpg");
         
         foreach(FileInfo pic in files)
             this.images.Add(pic.FullName);

       } 
     
   using(FileStream fs = new FileStream(images[0], FileMode.Open))
   {
       this.pictureBox.Image = Image.FromStream(fs);
    }

  }



我有两个问题
首先,如果我创建了该范围",并在其中放置了FileInfo引用
因此,在结束括号之后,它们将超出范围,因此
他们将从堆栈中弹出....对吗?它会节省内存吗?
我的应用程序使用?还是只是破坏引用并会
无法释放FileInfo引用指向的堆内存?

其次,像我一样在Stream中打开图像是否有效?
我曾经创建一个Bitmap对象,然后将其加载到PictureBox
而且速度较慢(即使没有深入检查也是如此)...

还有其他方法可以优化图像加载,例如流缓存"之类吗?
请帮助



I have two questions
First, if I created that "scope" and i put FileInfo references there
so after the closing bracket they would be out of scope and thus
they will be popped out of stack....Right? Will it save memory that
my application uses? Or will it simply destroy references and will
not free up heap memory where FileInfo references point???

Second, is it effective to open images in Stream like I did??
I used to create a Bitmap object and then I loaded it onto PictureBox
and it was slower (even without deep examing)...

Is there any another way to optimize image loading like "stream cache" or something??
Please help

推荐答案

尼克,

1.
您根本不需要FileInfo[]数组,因为您使用的只是名称.看看Directory.GetFiles();返回一个字符串数组,价格便宜得多,速度更快.

2.
假设images List<string>或类似的东西,则不需要foreach循环,只需尝试images.AddRange()

3.
我认为将图像加载到内存和PictureBox的方法并不重要.最简单的可能是pictureBox.ImageLocation=images[0];

4.
影响性能的是您丢弃不再需要的图像,因此,如果计划将其循环到PictureBox,则可能需要这样做:
Hi Nick,

1.
you don''t need the FileInfo[] array at all, as all you use is the names anyway. Have a look at Directory.GetFiles(); that one returns an array of strings, much cheaper, much faster.

2.
assuming images is a List<string> or something similar, you don''t need the foreach loop, just try images.AddRange()

3.
I don''t think the way to load the image into memory and into the PictureBox will matter much. The simplest probably is pictureBox.ImageLocation=images[0];

4.
What will affect performance is you disposing of images you no longer need, so if you plan on cycling them towards your PictureBox, you may want to do it like this:
foreach(string s in images) {
    Bitmap bm=new Bitmap(s);
    pictureBox.Image=bm;
    // insert something that takes a while so you can see the image...
    pictureBox.Image=null;
    bm.Dispose();
}



:)



:)


Nick Reshetinsky写道:
Nick Reshetinsky wrote:

f我创建了作用域",并在其中放置了FileInfo引用
因此,在结束括号之后,它们将超出范围,因此
他们将从堆栈中弹出....对吗?它会节省内存吗?
我的应用程序使用?还是只是破坏引用并会
不能释放FileInfo引用指向的堆内存?

f I created that "scope" and i put FileInfo references there
so after the closing bracket they would be out of scope and thus
they will be popped out of stack....Right? Will it save memory that
my application uses? Or will it simply destroy references and will
not free up heap memory where FileInfo references point?



是的,没有.当文件"数组在块末尾超出范围时,该数组的所有元素(所有FileInfo变量)都可用于垃圾回收,因为没有别的东西引用它们了.但是,在GC真正解决之前,它们不会被删除-可能会立即生效,也可能是明年.真的没关系!

如果没有实际测量(并且我现在没有时间),如果



Yes and no. When the "files" array goes out of scope at the end of the block, all the elements of the array (all the FileInfo variables) are available for garbage collection, because nothing else refers to them. They won''t however be deleted until the GC actually gets round to it - which may be immediately, or may be next year. It really doesn''t matter!

Without actually measuring it (and I don''t have time at the moment) I would be suprised if

this.pictureBox.Image = Image.FromStream(fs);

this.pictureBox.Image = new Bitmap(pathNameString);

慢得多,我会感到惊讶,因为我希望Bitmap构造函数仍然使用流...
您是否尝试过

as I would expect the Bitmap constructor to use a stream anyway...
Did you try

this.pictureBox.Image = Image.FromFile(pathNameString);

,如果这样做有什么不同?

and if so did that make any difference?


这篇关于帮我优化C#中的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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