我已经尝试了几个小时从文件加载图像的各种方法。请看一下这两种方法:
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);
}
第二种方法大约快两倍。我在这里缺少什么?为什么会这样呢?我不敢相信 .NET 开发人员仅使用我编写的方法就无法更快地实现 Image.FromFile。所以=>我在某个地方错了。请告诉我在哪里。为什么第二种方法快了近2倍?我的代码完全正确吗? (线程安全等)。也许 Image.FromFile 更安全?
AFAIK:
首先,Image.FromFile 包装了 GDI+ GdipLoadImageFromFile* 函数,它们的生活很奇怪。 GDI+图像持有并可以使用源(文件或流)在整个生命周期中,有关它的一些细节http://support.microsoft.com/en-us/kb/814675 http://support.microsoft.com/en-us/kb/814675。因此,这里有一些可能的“多文件 io”与“多流 io”。
MS Reference Source System.Drawing.Image 中还有一些有趣的评论:
http://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Image.cs,181 http://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Image.cs,181
class Image {
............
public static Image FromFile(String filename,
bool useEmbeddedColorManagement)
{
............
//GDI+ will read this file multiple times.
............
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)