简体   繁体   English

直接从文件中读取大量bmp数据

[英]Reading large amounts of bmp data directly from file

I am working on an application that has to deal with large bmp files that, quite often, are too large to fit into memory. 我正在开发一个必须处理大型bmp文件的应用程序,而大型bmp文件通常太大而无法容纳到内存中。 As part of the software we are developing at project creation my application converts the data from a bmp file to a different format that allows easy retrieval in sections. 作为我们在项目创建时正在开发的软件的一部分,我的应用程序将数据从bmp文件转换为不同的格式,从而可以方便地按节检索。

Currently, since the file is often too large to fit in memory, the software reads a section of byte data directly from the file, processes it, and moves onto the next section. 当前,由于文件通常太大而无法容纳在内存中,因此该软件直接从文件中读取一部分字节数据,对其进行处理,然后移至下一部分。 The code for reading the file is similar to that shown below (simplified for clarity): 读取文件的代码与以下所示类似(为清楚起见已简化):

FileStream fs = File.OpenRead(fileName);
fs.Seek(sectionStart, SeekOrigin.Begin); 

currentSectionAsBytes = new byte[sectionSize];
fs.Read(currentSectionAsBytes, 0, currentSectionAsBytes.Length);

This has worked fine up until now since the files we work with all have a width divisible by 4, and therefore the bmp files have no padding. 到目前为止,这一直很好,因为我们一起使用的文件的宽度都可以被4整除,因此bmp文件没有填充。

Lately, we've been working with different sized images which have padding at the end of each row of data, causing the acquired data to be erroneous. 最近,我们一直在处理不同大小的图像,这些图像在每行数据的末尾都有填充,从而导致获取的数据是错误的。

My question is, does anyone know of any better ways of acquiring bmp data directly from file. 我的问题是,有人知道直接从文件中获取bmp数据的更好方法吗? As previously stated I can't read the Bitmap from the file due to the large file size. 如前所述,由于文件太大,我无法从文件中读取位图。 Currently my best idea is, after getting the currentSectionAsBytes, to calculate the padding size and manually remove it. 目前,我的最佳方法是在获取currentSectionAsBytes之后,计算填充大小并手动将其删除。 This just seems too cumbersome and complicated. 这似乎太麻烦和复杂了。 There must be a better solution. 必须有一个更好的解决方案。

"Manually removing" something from an array is a bad idea, when you are facing the lack of memory (since it will lead to creating another array). 当您面临内存不足的情况时,“手动删除”阵列中的某些内容是个坏主意(因为这将导致创建另一个阵列)。 I think, the right approach to your issue is to add two extra parameters to your processing method, so its siganture will look like: 我认为,解决您的问题的正确方法是在处理方法中添加两个额外的参数,因此其结构看起来像:

void ProcessSection(byte[] data, int offset, int count);

And then iterate data not from 0 to data.Length , but from offset to count . 然后将data不是从0迭代到data.Length ,而是从offset迭代到count This is a common way to deal with arrays in .Net (look at fs.Read() method for example), there is not much else that can be done. 这是处理.Net中数组的一种常用方法fs.Read()例如,查看fs.Read()方法),没有其他可以完成的事情。 And yes, you will have to calculate offset/count according to your bitmap properties. 是的,您将必须根据位图属性计算偏移量/计数。

Ofc, alternatively, you can account for an offset, when you are calling fs.Seek method (eg when you calculate sectionStart ), but i think the first apporach is more universal. OFC,或者,你可以考虑偏移,当你调用fs.Seek方法(例如,当你计算sectionStart ),但我认为首先apporach较为普遍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM