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

前端 未结 6 1722
梦谈多话
梦谈多话 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.

提交回复
热议问题