【发布时间】:2014-05-23 17:19:45
【问题描述】:
大家好,我的程序从IsolatedStorage 文件中获取图像,然后将其显示在我的仪表板上。它工作正常,直到它到达仪表板上的第四张图片,然后它抛出一个OutOfMemoryException。下面是代码:
public class StringToBitmapConverter : IValueConverter
{
public object Convert(object value, Type TargetType, object parameter, CultureInfo culture)
{
byte[] data;
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isf.FileExists(value.ToString()))
{
using (IsolatedStorageFileStream isfs = isf.OpenFile(value.ToString(), FileMode.Open, FileAccess.Read))
{
// Allocate an array large enough for the entire file
data = new byte[isfs.Length];
// Read the entire file and then close it
isfs.Read(data, 0, data.Length);
isfs.Close();
}
// Create memory stream and bitmap
MemoryStream ms = new MemoryStream(data);
BitmapImage bi = new BitmapImage();
// Set bitmap source to memory stream
bi.SetSource(ms);
ms.Dispose();
return bi;
}
}
return null;
}
public object ConvertBack(object value, Type TargetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
请你告诉我代码有什么问题,以便我可以解决他的问题?
这是我在应用崩溃时遇到的错误:
“System.OutOfMemoryException”类型的异常发生在 System.Windows.ni.dll 但未在用户代码中处理
【问题讨论】:
标签: c# exception memory bitmap converter