为了使用我的 Silverlight 应用程序从文件夹中读取图片,我使用文件流设置位图图像的源。请看下面的代码:
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Images", String.Format("{0}.jpg", _imageName));
if (File.Exists(path))
{
Image = new BitmapImage();
Image.SetSource(new MemoryStream(File.ReadAllBytes(path)));
}
问题是图像需要很长时间才能显示出来,当我加载大量图片(>400)时,可能会出现内存不足错误。通过 URI 加载图片时,我从来没有遇到过这个错误,我想知道是否可以通过 URI 从路径加载它。我试过的代码:
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Images", String.Format("{0}.jpg", _imageName));
if (File.Exists(path))
{
Image = new BitmapImage()
{
UriSource = new Uri(path),
CreateOptions = BitmapCreateOptions.DelayCreation
};
}
你有什么提示可以提供吗?
谢谢!
菲利普