Speed up loading an image from disk in a windows forms (c#.net) app

前端 未结 6 1701
梦谈多话
梦谈多话 2021-02-15 13:21

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.

相关标签:
6条回答
  • 2021-02-15 13:43

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

    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
    

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

    0 讨论(0)
  • 2021-02-15 13:45

    Easiest might be to put a 'next' and 'previous' button to limit the number of images and preload.

    0 讨论(0)
  • 2021-02-15 13:47

    Check out the concept of double buffering. What you want to be doing is have a second thread that can be loading the next set of images while you are displaying the first set. Once the 1/6 time gate hits, you switch the one set of images out and start loading the next set.

    0 讨论(0)
  • 2021-02-15 13:49

    i would probably create a background thread to continously fetch all the images from disk (keeps the ui responsive) and then publish each newly loaded image via an event

    0 讨论(0)
  • 2021-02-15 13:57

    I think concept of double buffering will be useful. Set "Double Buffer" property of the form to True. This will help you little bit. Following links may be useful to you

    • http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/f15ae586-b63f-4afb-90a7-d9067ccc19d5
    • How to speed up .NET winforms rendering
    0 讨论(0)
  • 2021-02-15 14:00

    If you have 6 images displayed at once, and you change them all every 1/6 of a second, you should be running into performance issues. Loading 150 kb from disk should be a trivial activity even without caching. It sounds like you may be overdoing the file loads. Are you sure you are only loading 6 images at a time? Are you reading images from disk that are not displayed?

    If you can you provide a little more detail of the application flow, I may be able to be a little more helpful.

    0 讨论(0)
提交回复
热议问题