在我的 MFC 项目中,我需要读取单色位图文件并将其转换为 CByteArray。使用“CFile”类以“Read”模式读取位图文件时,似乎它提供的长度比原来的长。
我的 MFC 代码:-
CFile ImgFile;
CFileException FileExcep;
CByteArray* pBinaryImage = NULL;
strFilePath.Format("%s", "D:\\Test\\Graphics0.bmp");
if(!ImgFile.Open((LPCTSTR)strFilePath,CFile::modeReadWrite,&FileExcep))
{
return NULL;
}
pBinaryImage = new CByteArray();
pBinaryImage->SetSize(ImgFile.GetLength());
// get the byte array's underlying buffer pointer
LPVOID lpvDest = pBinaryImage->GetData();
// perform a massive copy from the file to byte array
if(lpvDest)
{
ImgFile.Read(lpvDest,pBinaryImage->GetSize());
}
ImgFile.Close();
注意:文件长度设置为 bytearray obj。
我使用以下示例检查了 C#:-
Bitmap bmpImage = (Bitmap)Bitmap.FromFile("D:\\Test\\Graphics0.bmp");
ImageConverter ic = new ImageConverter();
byte[] ImgByteArray = (byte[])ic.ConvertTo(bmpImage, typeof(byte[]));
在比较“pBinaryImage”和“ImgByteArray”的大小时,它不一样,我猜“ImgByteArray”的大小是正确的,因为从这个数组值,我可以得到我原来的位图。