Windows窗体应用程序中的图像缓存 [英] Image caching in Windows Forms Application

查看:78
本文介绍了Windows窗体应用程序中的图像缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我有一个Windows窗体应用程序,它显示了从数据集派生的产品列表。

我这样做不使用gridviews而是所有控件(标签按钮和面板)都是动态生成的,并且当它们通过单击按钮展开或折叠时定位在代码区中。



每个时间有一个展开,折叠或滚动事件它从数据集重新绑定整个列表是非常快的。



我正在努力一个新的要求是在列表中的每个项目旁边添加一个图像缩略图。



ImageBox也是动态生成的,但我一直在推导来自网络服务器的图像通过http例如



 用于 ...循环遍历数据集
' 绑定其他控件
SKUThumbNail(计数器).LoadAsync( http://127.0.0.1/images/43234.jpg
下一步 ...迭代 循环





每次数据集重新绑定时,图像也会从网络服务器重新加载,效率稍低。



在我为我的应用程序构建内存映像缓存模块之前,有没有人有这方面的经验或知道更好的方法?



感谢任何意见,谢谢。



M:)

解决方案

< blockquote>这是一个非常好的主意,易于实现。只有您还需要一种删除某些缓存图像的机制。你怎么知道他们是否永远不会改变?



您可以将图像作为常规图像文件存储在本地;设计一些命名它们的系统。它可以像数字一样简单。您还需要一些图像名称集的碎片整理机制。图像名称应存储在其中一个基于密钥的集合中,如 System.Collections.Generic.Dictionary< System.Uri,string> ,其中图像 Uri 用作字典键,第二个字符串泛型参数表示图像存储中的本地文件名。当您关闭应用程序并重新启动时,您将需要存储然后恢复字典本身。字典不可序列化,但您可以创建自己的简单序列化机制。毕竟,这就像Uri /字符串对的表一样简单,所以它可以像文本文件一样简单,这些元素用行尾标记分隔。



请参阅:

http://msdn.microsoft.com /en-us/library/xfhwa508.aspx [ ^ ],

http:// msdn .microsoft.com / zh-cn / library / system.uri.aspx [ ^ ]。



您可以考虑基于密钥对存储和检索的其他两个集合类,提供O(1)的计算时间复杂度,这意味着对于大量数据,渐近的搜索时间不依赖于音量。请参阅:

http://en.wikipedia.org/wiki/Big_O_notation [< a href =http://en.wikipedia.org/wiki/Big_O_notationtarget =_ blanktitle =New Window> ^ ],

http://en.wikipedia.org/wiki/Time_complexity [ ^ ],

http ://en.wikipedia.org/wiki/Computational_complexity_theory [ ^ ]。



另外两个类是 SortedDictionary< Key,Value> SortedList< Key,Value>

http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx [ ^ ]。



所有三个类都具有几乎相同的功能,一个类的选择可以基于内存中性能和存储之间的不同权衡。只需阅读MSDN帮助页面并进行实验。



-SA


Hi
I''ve got a Windows Forms Application which displays a listing of products derived from a dataset.
I''m doing this without using gridviews instead all the controls (labels buttons, and panels) are generated on the fly and positioned in codeland as they are expanded or collapsed by clicking buttons.

Each time there is an expand, collapse or scroll event it rebinds the entire listing from a dataset is which works quite well as its very fast.

I''m working on a new requirement which is to add an image thumbnail next to each item in the listing.

The ImageBox is also being generated dynamically but instead I''ve been deriving the image from the web server via http e.g.

For ... Looping through dataset
  'bind other controls here
  SKUThumbNail(counter).LoadAsync("http://127.0.0.1/images/43234.jpg")
Next ... Iteration of Loop



Every time the dataset rebinds, the image is also reloaded from the webserver which is somewhat inefficient.

Before I go down the path of building an in memory image caching module for my application does anyone have any experience with this or know of any better approaches?

Any input appreciated, Thank you.

M:)

解决方案

This is a pretty good idea, which is easy to implement. Only you additionally would need to have a mechanism of deleting some of the cached images. How do you know if they never change?

You can store the images locally as regular image files; devise some system of naming them. It could be as simple as numeric numbers. You also would need some mechanism of defragmentation of image name sets. The image names should be stored in one of the key-based collections like System.Collections.Generic.Dictionary<System.Uri, string>, where the image Uri serves as a dictionary key, and second string generic parameter represents the local file name in your image storage. When you close the application and start again, you will need to store and then restore the dictionary itself. Dictionaries are not serializeable, but you can make your own simple mechanism of serialization. After all, this is as simple as a table of Uri/string pairs, so it can be as simple as a text file with those elements separated by end-of-line markers.

Please see:
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx[^],
http://msdn.microsoft.com/en-us/library/system.uri.aspx[^].

You can consider two other collection classes based on key-pair storage and retrieval, which provide the computational time complexity of O(1), which means that the search time asymptotically, for large volumes of data, does no depends on volume. Please see:
http://en.wikipedia.org/wiki/Big_O_notation[^],
http://en.wikipedia.org/wiki/Time_complexity[^],
http://en.wikipedia.org/wiki/Computational_complexity_theory[^].

Two other classes are SortedDictionary<Key, Value> and SortedList<Key, Value>:
http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx[^].

All three classes have nearly identical functionality, the selection of one can be based on different trade-off between performance and storage in memory. Just read MSDN help pages and experiment a bit.

—SA


这篇关于Windows窗体应用程序中的图像缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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