我目前正在开发一款应用程序,允许用户播放(自动滚动)一系列本地图像.通常一次屏幕上会有五六个.
目前的主要瓶颈似乎是从磁盘实际加载图像数据.计时器线程要求每1/6秒更新一次图像,应用程序正在努力跟上这个速度.每张图片大约25Kb.
我尝试创建一个滚动缓存来尝试和预加载图像,但这也被赶上了自己,所以最终放慢了同样的速度.
计时器的每个节拍,我循环使用标准加载下一个图像的六个图像占位符
Image img = Image.FromFile("filename");
Run Code Online (Sandbox Code Playgroud)
方法,但认为有人可能知道更快的方式从磁盘上获取图像.
在六组中的每一组中都有500到20,000个图像,所以它太大而无法在开始时将整个内容加载到内存中.
如果有人建议以更快的方式拉动这些图像,那将非常感激.
编辑以添加应用程序流的更多细节.
好的,这就是发生的事情:
用户点击"播放"按钮.定时器线程以1/6秒超时开始.
定时器回调:
Update image index (_index++)
for each viewer in list of visible viewers (the forms to display images)
{
get the filename from the id stored in the viewer
check to see if the file exists
if it does exist,
create new bitmap from image
and return that image
otherwise return null
if returned image isn't null, display it on screen
}
Run Code Online (Sandbox Code Playgroud)
这显然是跨越几层 - 图像加载在服务层继续,然后将其传递到演示文稿,然后传递到UI,但这是发生的事情的要点.
我遇到了这个页面,它描述了如何直接使用GDI + API来加载图像.使用非常简单:
ImageFast.FromFile(@"C:\MyPhoto.JPG");
Run Code Online (Sandbox Code Playgroud)
添加以显示ImageFast over Image From File方法的速度
这使用了此处的源代码.代码已复制并粘贴,无需更改.
Stopwatch watch = Stopwatch.StartNew();
string filePath = @"C:\TestImage25k.png";
Image fromFile = Image.FromFile(filePath);
watch.Stop();
Console.WriteLine("Image.FromFile Ticks = {0:n}", watch.ElapsedTicks);
long fromFileTicks = watch.ElapsedTicks;
watch.Reset();
watch.Start();
Image fastImage = ImageFast.FromFile(filePath);
watch.Stop();
long fastFileTicks = watch.ElapsedTicks;
Console.WriteLine("ImageFast.FromFile Ticks = {0:n}", watch.ElapsedTicks);
Console.WriteLine("fromFileTicks - fastFileTicks = {0:n}", fromFileTicks - fastFileTicks);
Run Code Online (Sandbox Code Playgroud)
控制台输出是
Image.FromFile Ticks = 19,281,605.00 ImageFast.FromFile Ticks = 7,557,403.00 fromFileTicks - fastFileTicks = 11,724,202.00
您可以看到ImageFast的影响.随着时间的推移,这1100万个节省的蜱将加起来.
| 归档时间: |
|
| 查看次数: |
9101 次 |
| 最近记录: |