问题描述
大家好,
我已经提取了视频的总帧数",并保存在输出目录"中.
现在,我已经在WPF窗口的列表框中显示了所有服务器场以及框架号.在每个框架上.
Hi all,
I have extracted the Total frames of a video and saved in a Output Directory.
Now i have displayed all the farmes in a listbox on WPF window along with the frame no. on each frame.
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;
在这里,我为每个文件创建一个堆栈面板,图像和文本块.图像和文本块作为子代添加到堆栈面板,并且此堆栈面板被添加到列表中.
现在,每次创建新对象时都在这里.如何在foreach循环中放置旧对象?
感谢
Here for each file I am creating a stackpanel, image and textblock. image and textblock are added as children to stackpanel and this stackpanels are added to a list.
Now here every time new objects are created. How can i dispose the old objects in foreach loop?
Thanks
推荐答案
在您的情况下,您创建BitmapImage
,Image
,TextBlock
和StackPanel
.这些类均未实现 IDisposable .因此,您不需要处理这些对象,只需将它们从列表中删除(然后没人会对其进行引用),然后将释放它们.
In your case, you create BitmapImage
, Image
, TextBlock
and StackPanel
. None of these classes implement IDisposable. So, you don''t need to dispose the objects, just remove them from the list (and then no one will have a reference to them), and the GC will free them.
这篇关于处置在C#中的循环中创建的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!