由於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