由于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'数组分配空间? – cdhowie
MemoryStream和Byte []基本上是一样的东西,MS有一些额外的包装,可以使它更容易处理,虽然...看到http://stackoverflow.com/questions/16939/difference-between-a-byte-array -and-memorystream和http://stackoverflow.com/questions/11828599/c-sharp-memorystream-vs-byte-array – Nevyn