加载图片文件 Image.FromFile VS FileStream

发布于 2024-09-15 14:30:55 字数 625 浏览 7 评论 0原文

我必须承认,我从来不明白流到底是什么——我一直认为这是互联网的事情。但现在我遇到了一个使用流在本地加载文件的代码,我想知道使用流是否比我总是加载文件的方式有优势:

private void loadingfromStream()
{
   DirectoryInfo dirInfo = new DirectoryInfo("c:/");
   FileInfo[] fileInfoArr = dirInfo.GetFiles();
   FileInfo fileInfo = fileInfoArr[0];       

   // creating a bitmap from a stream
   FileStream fileStream = fileInfo.OpenRead();            
   Bitmap bitmap = new Bitmap(fileStream);  
   Image currentPicture = (Image)bitmap       
}

private void loadingUsingImageClass
{    
   Image currentPicture = Image.FromFile(originalPath);
}

I must admit that I never understood what are the streams are all about- I always thought it's an internet thing. But now I run into a code that used a stream to load a file localy and I wonder if there is advantage for using a stream over... well the way I always loaded files:

private void loadingfromStream()
{
   DirectoryInfo dirInfo = new DirectoryInfo("c:/");
   FileInfo[] fileInfoArr = dirInfo.GetFiles();
   FileInfo fileInfo = fileInfoArr[0];       

   // creating a bitmap from a stream
   FileStream fileStream = fileInfo.OpenRead();            
   Bitmap bitmap = new Bitmap(fileStream);  
   Image currentPicture = (Image)bitmap       
}

vs.

private void loadingUsingImageClass
{    
   Image currentPicture = Image.FromFile(originalPath);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

朦胧时间 2024-09-22 14:30:56

如果您知道您的代码将从文件加载数据,请使用Image.FromFile - 这显然是相当简单的代码,并且框架内可能存在优化当它处理文件时。

使用流更加灵活,但除非您需要这种灵活性,否则请使用文件解决方案。

If you know your code will be loading the data from a file, use Image.FromFile - it's obviously rather simpler code, and it's just possible that there are optimizations within the framework when it's dealing with files.

Using a stream is more flexible, but unless you need that flexibility, go with the file solution.

茶色山野 2024-09-22 14:30:56

如果要处理图像文件,当然第二种方案更好。在第一部分中,您有 Bitmap bitmap = new Bitmap(fileStream); 您知道图像文件并不总是 Bitmap,它也可以是 JPEG/PNG/TIFF 等。而Image.FromFile对于处理不同扩展名的图像文件来说是相当专业的。

一般来说,FileStream 常见于文件问题,而 Image.FromFile 则更特殊于图像文件。这取决于您要处理什么类型的文件。

If you want to deal with image files, of course the second solution is better. In your first section, you have Bitmap bitmap = new Bitmap(fileStream); you know that an image file is not always Bitmap, it also can be JPEG/PNG/TIFF and so on. While Image.FromFile is quite professional to deal with image files with different extensions.

Generally speaking, FileStream is common at file issues, while Image.FromFile is more particular at image files. It depends on what kind of files you are going to deal with.

甜心 2024-09-22 14:30:56

嗯,文件通常也被视为流。这就是为什么打开文件的主要类称为 FileStream。但有一个特定的操作系统功能可以使处理图像文件更加高效。它称为“内存映射文件”,该功能将文件的内容直接映射到内存。虽然其中涉及一些烟雾和镜子,但它本质上使文件直接可用,而无需读取它。存储文件数据所需的内存不占用页面文件中的空间。

非常高效,当您对 .bmp 格式的图像使用 FromFile() 或 Bitmap(string) 构造函数时,您将免费获得它。从流加载图像往往需要两倍的内存量,这对于大图像来说总是一个问题。

Well, a file is often treated as a stream as well. That's why the primary class to open files is called FileStream. But there's a specific operating system feature that can make dealing with image files a lot more efficient. It is called 'memory mapped files', a feature that maps the content of a file directly to memory. There's some smoke and mirrors involved, but it essentially makes the file directly available without having to read it. The memory you need to store the file data doesn't take space in the paging file.

Very efficient, you'll get it for free when you use FromFile() or the Bitmap(string) constructor for an image in the .bmp format. Loading an image from a stream tends to require twice the amount of memory, always a problem with big images.

秉烛思 2024-09-22 14:30:56

作为乔恩答案的补充:

据我所知,这两种方法也不做同样的事情。第一个为您提供 "C:\" 中的第一张图像,第二个仅为您提供路径中的图像。因此,第一个增加的复杂性不仅仅是因为它使用了流。

这将是等效的:

using (var fs = File.OpenRead(path))
using (var img = Image.FromStream(fs))
{
    //...
}

在这种情况下,正如乔恩所解释的那样,使用 Image.FromFile 来完成它当然更好。

As an a addition to Jon´s answer:

As far as I see, the two methods don't do the same thing either. The first is given you the first image in "C:\" where the second just give you a image from a path. So the added complexity in the first is not just because it is using streams.

This would be equivalent:

using (var fs = File.OpenRead(path))
using (var img = Image.FromStream(fs))
{
    //...
}

and in that case, it is certainly better to just do it with Image.FromFile as Jon explained.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文