处理位图图像的通用列表
如何处理 WPF 中通用列表中的所有图像?
这是我的尝试:
//piclist is a global variable pointing to a folder on harddrive
foreach (string s in this.piclist)
{
this.picsToDisplay.Add(this.BitmapFromUri(new Uri(s)));
}
private BitmapImage LoadImage(string myImageFile)
{
BitmapImage myRetVal = null;
if (myImageFile != null)
{
BitmapImage image = new BitmapImage();
using (FileStream stream = File.OpenRead(myImageFile))
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = stream;
image.EndInit();
}
myRetVal = image;
}
return myRetVal;
}
这就是我清除和处置所有内容的方式:
if (this.picsToDisplay.Count > 0)
{
foreach (BitmapImage b in this.picsToDisplay)
b.StreamSource.Dispose();
this.picsToDisplay.Clear();
}
Array.ForEach(Directory.GetFiles(this.tempPics),
delegate(string path) { File.Delete(path); });
它首先在 b.StreamSource.Dispose();
处崩溃 说streamsource为null'引用未设置到对象的实例'
如果我注释掉该行,它会在这部分崩溃:File.Delete(path);
消息显示我要删除的图片正在使用中。
图片显示在 WPF 中的图像控件中。 在我继续处理等之前,它的源已经设置为 NULL。
我错过了什么?
=========== 编辑
private void btnLoadPictures_Click(object sender, EventArgs e)
{
this.ClearPicturesFromList();
DirectoryInfo Dir = new DirectoryInfo(this.pictures);
List<string> stringList = new List<string>();
FileInfo[] picList = Dir.GetFiles();
stringList = (from FI in picList
where FI.LastWriteTime.Date.ToString("dd-MM-yyyy") ==
this.dpPictureDate.SelectedDate.Value.ToString("dd-MM-yyyy")
select (FI.FullName)).ToList();
try
{
if (Directory.Exists(this.tempPics))
{
if (stringList.Count > 0)
{
foreach (string s in stringList)
{
string destFolder = System.IO.Path.Combine(this.tempPics, System.IO.Path.GetFileName(s));
File.Copy(s, destFolder, true);
}
DirectoryInfo Dir2 = new DirectoryInfo(this.tempPics);
FileInfo[] pics = Dir2.GetFiles();
this.picsInTempFolder = (from FI in pics
select (FI.FullName)).ToList();
foreach (string s in this.picsInTempFolder)
{
this.picsToDisplay.Add(this.LoadImage(s));
}
this.indexCounter = 0;
this.imgBox.Source = (BitmapImage)this.picsToDisplay[this.indexCounter];
this.tbxPictureName.Text = System.IO.Path.GetFileName(this.picsInTempFolder[this.indexCounter]);
stringList.Clear();
pics = null;
}
}
}
catch (Exception exp)
{
this.WriteToRichTextBox(exp.ToString());
}
}
第一次按下clearimages按钮,我收到有关文件正在使用的异常。做第二次,确实效果很好。
在清除所有列表和内容之间放置一个 thread.sleep 比删除临时目录中的文件更有效。但不知怎的,它需要某种延迟。
How can i dispose all the images in a generic list in WPF?
Here is my try:
//piclist is a global variable pointing to a folder on harddrive
foreach (string s in this.piclist)
{
this.picsToDisplay.Add(this.BitmapFromUri(new Uri(s)));
}
private BitmapImage LoadImage(string myImageFile)
{
BitmapImage myRetVal = null;
if (myImageFile != null)
{
BitmapImage image = new BitmapImage();
using (FileStream stream = File.OpenRead(myImageFile))
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = stream;
image.EndInit();
}
myRetVal = image;
}
return myRetVal;
}
and this is how i clear and dispose everything:
if (this.picsToDisplay.Count > 0)
{
foreach (BitmapImage b in this.picsToDisplay)
b.StreamSource.Dispose();
this.picsToDisplay.Clear();
}
Array.ForEach(Directory.GetFiles(this.tempPics),
delegate(string path) { File.Delete(path); });
It crashes first at b.StreamSource.Dispose();
Saying that streamsource is null 'reference not set to an instance of an object'
If i comment out that line, it crashes at this part: File.Delete(path);
With the message that the picture i'm trying to delete is in use.
The pictures getting displayed in an image control in WPF.
Its source is already set to NULL before i proceed to dispose etc.
What am i missing guys?
============
EDIT
private void btnLoadPictures_Click(object sender, EventArgs e)
{
this.ClearPicturesFromList();
DirectoryInfo Dir = new DirectoryInfo(this.pictures);
List<string> stringList = new List<string>();
FileInfo[] picList = Dir.GetFiles();
stringList = (from FI in picList
where FI.LastWriteTime.Date.ToString("dd-MM-yyyy") ==
this.dpPictureDate.SelectedDate.Value.ToString("dd-MM-yyyy")
select (FI.FullName)).ToList();
try
{
if (Directory.Exists(this.tempPics))
{
if (stringList.Count > 0)
{
foreach (string s in stringList)
{
string destFolder = System.IO.Path.Combine(this.tempPics, System.IO.Path.GetFileName(s));
File.Copy(s, destFolder, true);
}
DirectoryInfo Dir2 = new DirectoryInfo(this.tempPics);
FileInfo[] pics = Dir2.GetFiles();
this.picsInTempFolder = (from FI in pics
select (FI.FullName)).ToList();
foreach (string s in this.picsInTempFolder)
{
this.picsToDisplay.Add(this.LoadImage(s));
}
this.indexCounter = 0;
this.imgBox.Source = (BitmapImage)this.picsToDisplay[this.indexCounter];
this.tbxPictureName.Text = System.IO.Path.GetFileName(this.picsInTempFolder[this.indexCounter]);
stringList.Clear();
pics = null;
}
}
}
catch (Exception exp)
{
this.WriteToRichTextBox(exp.ToString());
}
}
pressing the clearimages button for the first time, i get the exception about the file being in use. doing it the second time, actually works fine.
putting a thread.sleep between clearing all lists and stuff than delete the files in the temp directory doenst work. but somehow it needs a sort of delay.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这种加载图像的方法
try this method for loading images