.net – Silverlight:来自流的BitmapImage抛出异常(灾难性故障(HRESULT异常:0x8000FFFF(E_UNEXPECTED)))

.net – Silverlight:来自流的BitmapImage抛出异常(灾难性故障(HRESULT异常:0x8000FFFF(E_UNEXPECTED))),第1张

概述我需要动态加载许多(有时数百个)缩略图.出于性能原因,我需要在有限数量的请求中执行此 *** 作,我使用单个请求/响应进行测试.我正在为响应中的图像发送二进制数据,并使用MemoryStream将它们加载到Bitmap Image中.这正常工作,直到我加载超过约80个缩略图,然后我得到灾难性失败例外.为了确保我的数据没有损坏,我尝试使用相同的字节数组多次加载BitmapImage,并在80次左右加载后崩溃 我需要动态加载许多(有时数百个)缩略图.出于性能原因,我需要在有限数量的请求中执行此 *** 作,我使用单个请求/响应进行测试.我正在为响应中的图像发送二进制数据,并使用MemoryStream将它们加载到Bitmap Image中.这正常工作,直到我加载超过约80个缩略图,然后我得到灾难性失败例外.为了确保我的数据没有损坏,我尝试使用相同的字节数组多次加载BitmAPImage,并在80次左右加载后崩溃.

下面是一个如何从字节数组加载图像的示例,已知字节数组具有有效的图像数据(png):

private BitmAPImage LoadImage(byte[] imageData){    BitmAPImage img = new BitmAPImage();    MemoryStream stream = new MemoryStream(imageData);    img.SetSource(stream); // Exception thrown here after too many images loaded.    return img;}

然后我使用BitmAPImage作为页面上Image元素的源,但错误发生在上面的img.SetSource(…)行中.

将GC.Collect()添加到我正在加载缩略图图像的循环中让我加载了更多图像,所以我认为这与内存管理有关但我不知道我能做些什么来解决问题.

解决方法 我认为引用微软在上述错误报告中提供的答案是值得的,因为它非常简洁,描述了问题并提供了推荐的解决方案:

When Silverlight loads an image,the framework keeps a reference and caches the decoded image until flow control is returned to the UI thread dispatcher. When you load images in a tight loop like that,even though your application doesn’t retain a reference,the GC can’t free the image until we release our reference when flow control is returned.

After processing 20 or so images,you Could stop and queue the next set using dispatcher.BeginInvoke just to break up the work that is processed in one batch. This will allow us to free images that aren’t retained by your application.

I understand with the current decode behavior it’s not obvIoUs that Silverlight is retaining these references,but changing the decoder design Could impact other areas,so for Now I recommend processing images like this in batches.

Now,if you’re actually trying to load 500 images and retain them,you are still likely to run out of memory depending on image size. If you’re dealing with a multi-page document,you may want to instead load pages on demand in the background and release them when out of vIEw with a few pages of buffer so that at no point do you exceed reasonable texture memory limits.

总结

以上是内存溢出为你收集整理的.net – Silverlight:来自流的BitmapImage抛出异常(灾难性故障(HRESULT异常:0x8000FFFF(E_UNEXPECTED)))全部内容,希望文章能够帮你解决.net – Silverlight:来自流的BitmapImage抛出异常(灾难性故障(HRESULT异常:0x8000FFFF(E_UNEXPECTED)))所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1002752.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存