加快Windows窗体加载从磁盘映像(C#.NET)的应用程序 [英] Speed up loading an image from disk in a windows forms (c#.net) app

查看:126
本文介绍了加快Windows窗体加载从磁盘映像(C#.NET)的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个应用程序,允许用户通过一系列本地图片的播放(自动滚动)。通常将有在屏幕上五个或六个一次

I'm currently working on an app that allows the user to play (automatically scroll) through a series of local images. Usually there will be five or six on screen at once.

此刻的主要瓶颈似乎是从磁盘的图象数据的实际负荷。计时器线程调用的图像秒每1/6更新和应用程序正在努力跟上这个速度。每幅图像的大小约为25KB。

The major bottleneck at the moment seems to be the actual loading of the image data from disk. A timer thread calls for the images to be updated every 1/6 of a second and the app is struggling to keep up with that speed. Each image is around 25Kb.

我试图创建一个滚动缓存,试图预先载入图像,但这也陷入了自己这么结束了减速一样多。

I tried creating a rolling cache to try and preload images but this was also getting caught up with itself so ended up slowing down just as much.

定时器的每一次跳动,我通过循环的六个图像占位符使用标准

Every beat of the timer, I'm looping through the six image placeholders loading the next image using the standard

Image img = Image.FromFile("filename");



方法,但想到有人会知道一个更快的方法来获取图像关闭磁盘。

method but thought someone might know of a faster way to get the images off disk.

有500和20000图像之间每个六套所以它太大,整个事情加载到起步的记忆。

There are between 500 and 20,000 images in each of the six sets so it's too large to load the whole thing into memory at the start.

如果任何人有通过拉这些图像更快地建议,将不胜感激。

If anyone has suggestions for a faster way to pull these images through, it would be greatly appreciated.

。编辑添加应用程序流的一些细节。

Edit to add some more detail of application flow.

好吧,这是发生了什么:

Okay, this is what's happening:

用户点击播放按钮。定时器线程开始用1/6秒超时

User hits 'play' button. Timer thread starts with 1/6 second timeout.

计时器回调:

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
}

这显然跨越了几个去层 - 图像加载的推移在服务层,然后通过对演示通过这个,然后到用户界面,但是这就是正在发生的要点

That's obviously going across a few layers - the image loading goes on in the services layer and then passes this through to presentation and then to the UI but that's the gist of what's happening.

推荐答案

我碰到的其中介绍了如何使用GDI + API直接加载图像此页面。使用方法很简单:

I came across this page which describes how to use the GDI+ API directly to load images. Very simple to use:

ImageFast.FromFile(@"C:\MyPhoto.JPG");






添加到显示速度ImageFast超过映像从文件的方法


Added to show speed of ImageFast over Image From File method

这使用源代码中发现的此处。该代码被复制和粘贴,并无需任何更改

This uses the source code found here. The code was copied and pasted and required no changes.

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);



控制台输出是

The console output was


Image.FromFile     Ticks = 19,281,605.00

ImageFast.FromFile Ticks = 7,557,403.00

fromFileTicks - fastFileTicks = 11,724,202.00

您可以看到ImageFast的影响。随着时间的推移这些11000000保存蜱会积少成多。

You can see the impact of the ImageFast. Over time those 11 million saved ticks will add up.

这篇关于加快Windows窗体加载从磁盘映像(C#.NET)的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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