如果我之后的任何人感兴趣,这就是我发现的工作:
for (int i = 0; i < reverseImageFiles.Length; i++)
{
string curBMP = reverseImageFiles[i];
using(Stream inStream = File.OpenRead(curBMP))
using (Stream writeStream = new FileStream(outputBMP,FileMode.Append,FileAccess.Write,FileShare.None))
{
BinaryReader reader = new BinaryReader(inStream);
BinaryWriter writer = new BinaryWriter(writeStream);
byte[] buffer = new Byte[134217728];
int bytesRead;
int totalBytes = 0;
while ((bytesRead = inStream.Read(buffer, 0, 134217728)) > 0)
{
totalBytes += bytesRead;
if (totalBytes <= 134217729) //if it's the first round of reading to the buffer, you need to get rid of 54-byte BMP header
{
writeStream.Write(buffer, 54, bytesRead - 54);
}
else
{
writeStream.Write(buffer, 0, bytesRead);
}
}
}
}
两件事情:
特别是对于 BMP,我发现您需要在追加时反转文件列表。例如,如果要追加的三个文件分别标记为 001.bmp、002.bmp、003.bmp,其中 001.bmp 应该在顶部,则实际上需要从 003.bmp 开始并向下工作。显然 BMP 是向后编码的。
如您所见,我使用了 128MB 的缓冲区。如果我想减少硬盘搜索,使用不同的大小会更好吗? 我的驱动器最近进行了碎片整理。感谢所有的帮助!