从 Silverlight 上的文件夹读取图片(在浏览器应用程序之外)
为了使用 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
};
}
您有什么提示可以提供吗?
谢谢你!
菲利普
For reading a picture from a folder with my Silverlight application, I Set the source of a Bitmap image with the stream of the file. See the code below:
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)));
}
The problem is that the image take a lot of time to show up and when I load a lot of pictures ( >400), I may get a insufficient memory error. I never had this error when loading a picture by the URI and I was wondering if it was possible to load it by the URI from a path. The code I tried:
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
};
}
Do you have any hints to provide ?
Thank you!
Philippe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的猜测是,您正在从本地文件系统加载照片,这些照片的分辨率比您通过 URL 加载的图像高得多。
你想加载 400 个,是的,你会耗尽内存。
对于加载时间你无能为力。您可以通过使用
WriteableBitmap
缩小实际位图来减少内存压力。My guess would be you are loading photos from the local file system that have a much higher resolution than the images you are loading via a URL.
You wanna load 400 of 'em, yes you are going to run out of memory.
There is not much you can do about the loading time. You might be able to reduce the pressure on memory by using a
WriteableBitmap
to scale the actual bitmap down.将图像加载到您的属性中,调整其大小并将该图像设置为预览,然后从文件夹中处理该图像并移至下一张。
简而言之,您应该在该文件夹中创建图像的缩略图并在您的应用程序中显示这些缩略图。
您现在要做的就是将所有图像保留在内存中,即所有大图像,这就是您出现内存不足异常的原因。
Load image into your property resize it and set that image as preview, then dispose the image from the folder and move to the next one.
So in short, you should create thumbnails of the images in that folder and show those in your app.
What you now do is keep all the images in your memory, all the big images that is, thats why you get out of memory exception.
我意识到即使我通过 URI 加载图片,也不会加载所有图片。该软件在占用 1.6g 内存(总共 6gig)时停止加载图片。与流加载图片的区别在于,似乎没有针对内存不足的保护。
由于我不显示所有图像(我意识到正确加载时我有超过 8000 张图片),我只在内存中加载我需要显示的图像。
所以每次我想要一张图片时,我都会从硬盘加载它。当图片不再显示时,垃圾收集会删除它们。这样,应用程序的内存始终稳定在300兆左右。
I realized that loading even when I loaded the pictures by the URI, it wasn't loading every pictures. The software stopped loading pictures when it was taking 1.6gig of ram (out of 6gig). The difference with loading the picture by the stream is that it seems there is no protection against insufficient memory.
As I don't display all the images ( I realized I have over 8000 pictures when they are loaded correctly) I only load in memory the image I need to display.
So every time I want a picture, I load it from the Hard Drive. When the pictures is no more displayed, the garbage collection get rid of them. This way, the application's memory is always stable at around 300 meg.