【发布时间】:2013-04-01 13:13:46
【问题描述】:
因为 BMP 文件是从下到上(以像素为单位)写入的,所以我需要反向读取 BMP 文件(并去掉 54 字节的标头)。到目前为止我的代码:
public string createNoHeaderBMP(string curBMP) //copies everything but the header from the curBMP to the tempBMP
{
string tempBMP = "C:\\TEMP.bmp";
Stream inStream = File.OpenRead(curBMP);
BinaryReader br = new BinaryReader(inStream);
byte[] fullBMP = new byte[(width * height * 3) + 138];
byte[] buffer = new Byte[1];
long bytesRead;
long totalBytes = 0;
while ((bytesRead = br.Read(buffer, 0, 1)) > 0)
{
fullBMP[fullBMP.Length - 1 - totalBytes] = buffer[0];
totalBytes++;
}
FileStream fs = new FileStream(tempBMP, FileMode.Create, FileAccess.Write);
fs.Write(fullBMP, 54, fullBMP.Length - 54);
fs.Close();
fs.Dispose();
return tempBMP;
}
由于某种原因,它无法完全完成这项工作,这导致图像右侧的一部分位于左侧。为什么不完全反转文件?此外,这些 BMP 文件非常大(600MB),所以我不能使用简单的内存流进行查找和交换操作,因为我会得到“内存不足”异常。
【问题讨论】:
-
为什么你提到你不能阅读整个文件,而你却读了?此外,永远不要像您那样对数组的值进行硬编码。不保证会有 138 字节的标头数据。
-
如果使用 MemoryStream 会给您带来 OOM 异常,您如何为
fullBMP数组分配空间而不会收到相同的异常? -
MemoryStream 和 Byte[] 本质上是一样的,MS 有一些额外的包装器可以使它更容易处理...见stackoverflow.com/questions/16939/… 和stackoverflow.com/questions/11828599/…
标签: c# filestream reverse