Skip to main content
 首页 » 编程设计

c# 如何打开二进制文件并将其存储在 16 位缓冲区中

2024年02月20日16pengyingh

我正在编写一个程序来进行一些图像处理。我正在处理原始二进制文件,我需要将原始文件中的数据写入 16 位缓冲区。我几乎明白了,但我不太确定如何进行转换。到目前为止,这是我的代码:

        Int16[] pixelBuffer; 
        String inFile; 
        String outFile;         
 
        /// Constructor. Allocates space for 3032x2016 16-bit values. 
        /// <param name="inputFile">Name of the binary input file to be read.</param> 
        /// <param name="outputFile">Name of the binary output file to be written.</param> 
        public ColorCorrector(String inputFile, String outputFile) 
        { 
        this.inFile = inputFile; 
        this.outFile = outputFile; 
        this.pixelBuffer = new Int16[6112512]; 
        //I need to open the binary file 'inputFile' and store 16-bit values in pixelBuffer. 
 
    } 

如有任何帮助,我们将不胜感激!

请您参考如下方法:

using (var inputStream = File.Open(inputFile))       
using (var reader = new BinaryReader(inputStream))       
{            
    int index = 0; 
    while (inputStream.Position < inputStream.Length) 
        pixelBuffer[index++] = reader.ReadInt16();       
}