本文介绍了图像加载性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试与不同的方式从文件加载图像几个小时。请看看这两种方法:

I've been experimenting for a few hours with various ways to load an image from file. Please have a look at these two methods:

    public Image SlowLoad(string path)
    {
        return Image.FromFile(path);
    }

    public Image FastLoad(string path)
    {
        using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(path)))
            return Image.FromStream(ms);  
    }



的第二种方法是像快2倍。我缺少的是在这里吗?为什么会这样呢?我不能相信,更快速简单地使用我写的方法.NET开发人员无法实施Image.FromFile。所以=>我错了地方。请告诉我在哪里。为什么第二个方法快了近2倍?是我的代码完全正确吗? (线程安全等)。 ?也许Image.FromFile是更安全的。

The second method is like 2 times faster. What am I missing here? Why is it so? I can't believe that .NET developers couldn't implement Image.FromFile faster simply using the method I wrote. So => I am wrong somewhere. Please tell me where. Why is the second method almost 2 times faster? Is my code fully correct? (thread-safe, etc.). Maybe Image.FromFile is more secure?

推荐答案

据我所知:
所有Image.FromFile首先包装GDI + GdipLoadImageFromFile *函数,那些有一个奇怪的生活。 GDI +图像的保存,并且可以使用源(文件或流)整个生命周期中,它 http://support.microsoft.com/en-us/kb/814675 。所以,这里是一些可能的多个文件IO与多流IO。
也有在MS参考源为System.Drawing.Image一些有趣的评论:

AFAIK:First of all Image.FromFile wraps GDI+ GdipLoadImageFromFile* functions, ones has a strange life. GDI+ image holds and can use source (file or stream) during whole lifetime, some details about it http://support.microsoft.com/en-us/kb/814675. So, here is some possible "multiple file io" vs "multiple stream io".Also there is some interesting comment in MS Reference Source System.Drawing.Image:

class Image {
    ............
    public static Image FromFile(String filename,
                                     bool useEmbeddedColorManagement) 
    {
        ............    
        //GDI+ will read this file multiple times. 
        ............
    }
}

这篇关于图像加载性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 14:51