返回首页

大家好,
我已提取视频的总框架和保存在输出目录。现在,我已经随着帧没有显示在WPF窗口的ListBox中的所有farmes。在每一帧。


int FramesCount = 0;

List<stackpanel> splist = new List<stackpanel>();				

System.IO.DirectoryInfo myimagesdir = new System.IO.DirectoryInfo(@"E:\\tmp");

foreach(System.IO.FileInfo myimagesfile in myimagesdir.GetFiles("*.jpg"))

{

    FramesCount = FramesCount + 1;

    StackPanel sp = new StackPanel();

    sp.Height = 100; sp.Width = 125;			

									

    Image img = new Image();

    img.Height = 80; img.Width = 100;

    BitmapImage bitimg = new BitmapImage();

					

    bitimg.BeginInit();

    bitimg.UriSource = new Uri(myimagesfile.FullName);

    bitimg.CacheOption = BitmapCacheOption.OnLoad;

					

    bitimg.EndInit();

    img.Source = bitimg;

				

    TextBlock tb = new TextBlock();

    tb.Height = 20; tb.Width = 30; tb.FontSize = 15;

    tb.Text = FramesCount.ToString();

    sp.Children.Add(tb);					

    sp.Children.Add(img);

    splist.Add(sp);								

}

			

Frameslistbox.ItemsSource = splist;


在这里,我为每个文件创建一个StackPanel,图像和TextBlock。
图像和TextBlock添加到StackPanel中的儿童,并这stackpanels被添加到列表。
现在这里每一次创建新对象。我如何处置旧的对象,在foreach循环?

谢谢

回答

评论会员:游客 时间:2012/02/06
处置对象,我们可以调用{,A0}方法或包装与使用块对象。在您的情况下,你创建的BitmapImage,图片,TextBlock和StackPanel中。这些类实现{A}没有将他们释放
lukeer
评论会员:游客 时间:2012/02/06
只要什穆埃尔臧说,你不需要关心,在给定的上下文。您正在创建一些对象,并把它们在列表。当前迭代循环完成后,持有你的对象的唯一引用。而且,由于该列表应该持有到对象的引用,处理,他们将不会是一个好主意。如果一段时间后,列表中抛出其参考外,垃圾收集器将未引用的对象的照顾。至于这个例子:你不能创建一个新的图像(),因为图像是一个抽象类。相反,你可以直接图片IMG=新的Bitmap(myimagesfile.FullName);
阿米尔Mahfoozi
评论会员:游客 时间:2012/02/06
在添加新的对象遍历检索SPList儿童,其中一个处理一个,然后添加新的儿童。检索SPList