何时可以关闭用于在C#中的PictureBox控件中支持GIF动画的MemoryStream? [英] When Is It Ok To Close A MemoryStream Used To Support A GIF Animation In A PictureBox Control In C#?

查看:399
本文介绍了何时可以关闭用于在C#中的PictureBox控件中支持GIF动画的MemoryStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

我在互联网上找不到关于这种情况的任何信息,所以我希望有人可以启发我。



我有一个带有图片框控件的应用程序,用于显示动画GIF图像。我使用OpenFileDialog将图像加载到图片框中,然后使用MemoryStream将图像转换为Base64字符串并将该字符串存储到列表中。从列表中加载图像时,图像将使用MemoryStream从Base64字符串转换回图像并加载到图片框中。



问题:为了动画要在图片框中设置动画的GIF,我必须让MemoryStream保持打开状态。否则动画GIF将不会动画。但每次我将新图像加载到图片框中时,我都会创建一个新的MemoryStream实例。通过这种方式加载图像而不关闭其关联的MemoryStreams,我假设我的应用程序将从图片框中不再显示的所有图像中留下一堆剩余的MemoryStream。我希望通过关闭不再需要的任何MemoryStream来保持我的应用程序的内存使用率低,但我不能在创建它的函数内关闭MemoryStream而不影响当前图像的动画。

解决方案

是的,你需要关闭它。使用它的常规方法是调用 IDisposeable.Dispose();这个界面由所有流和读者/作者类型实现。



此外,你永远不应该错过这个电话。 您必须确保为实现此接口的所有类型的对象调用 IDisposeable.Dispose(),无论如何,之前变得无法到达 。如果你不这样做,那么无法到达的对象将被垃圾收集,但是会丢失处置的副作用;它可能会导致非常不愉快的问题,例如:非托管内存泄漏(或其他资源泄漏),未封闭的文件句柄(文件在应用程序的生命周期内仍然无法访问)以及其他不良内容。



请参阅:

http:// en。 wikipedia.org/wiki/Garbage_collection_%28computer_science%29 [ ^ ],

https://msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.110%29.aspx [ ^ ]。



对于您必须使用的所有类型,请检查它们是否实现此接口。



SA

谢谢,谢尔盖,为你的洞察力。现在我知道在这种情况下该怎么做。


I can't find any information on the Internet about this scenario, so I'm hoping someone can enlighten me.

I have an app with a picturebox control that is used to display animated GIF images. I load an image into the picturebox using an OpenFileDialog and then I convert the image to a Base64 string using a MemoryStream and store that string into a list. When loading an image from the list, the image is converted from Base64 string back into an image using a MemoryStream and loaded into the picturebox.

Question: In order for animated GIFs to animate in the picturebox, I must leave the MemoryStream open. Otherwise the animated GIF won't animate. But every time I load a new image into the picturebox, I create a new instance of MemoryStream. By loading images this way without closing their associated MemoryStreams, I assume that my app will leave a bunch of leftover MemoryStreams from all the images that are no longer displayed in the picturebox. I want to keep my app's memory usage low by closing any MemoryStreams that are no longer needed, but I can't close a MemoryStream inside the function where it is created without affecting the current image's animation.

解决方案

Yes, you need to close it. The regular way to use it is to call IDisposeable.Dispose(); this interface is implemented by all stream and reader/writer types.

Moreover, you should never miss this call. You have to make sure to call IDisposeable.Dispose() for all objects of types implementing this interface, no matter what, before it becomes unreachable. If you fail to to do, the unreachable object will become garbage-collected, but side effect of disposal will be missed; it can lead to very unpleasant problems, such as: unmanaged memory leak (or leak of other resources), unclosed file handles (the files will remain inaccessible during lifetime of your applications), and other bad things.

Please see:
http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^],
https://msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.110%29.aspx[^].

For all types you have to work with, check up if they implement this interface or not.

—SA


Thank you, Sergey, for your insight. Now I know what to do in situations like this.


这篇关于何时可以关闭用于在C#中的PictureBox控件中支持GIF动画的MemoryStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆