简体   繁体   English

读取单色位图文件并将其转换为 CByteArray MFC

[英]Read and convert Monochrome bitmap file into CByteArray MFC

In my MFC project, I need to read and convert a Monochrome bitmap file into CByteArray.在我的 MFC 项目中,我需要读取一个 Monochrome 位图文件并将其转换为 CByteArray。 While reading the bitmap file by using 'CFile' class with the mode of 'Read', it seems like it gives more length than its original.通过使用具有“读取”模式的“CFile”类读取位图文件时,它似乎提供了比原始文件更长的长度。

My MFC code:-我的 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();

Note: File length is been set to bytearray obj.注意:文件长度设置为 bytearray obj。

I checked with C# with the following sample:-我使用以下示例检查了 C#:-

        Bitmap bmpImage = (Bitmap)Bitmap.FromFile("D:\\Test\\Graphics0.bmp");
        ImageConverter ic = new ImageConverter();
        byte[] ImgByteArray = (byte[])ic.ConvertTo(bmpImage, typeof(byte[]));

While comparing the size of "pBinaryImage" and "ImgByteArray", its not same and I guess "ImgByteArray" size is the correct one since from this array value, I can get my original bitmap back.在比较“pBinaryImage”和“ImgByteArray”的大小时,它不一样,我猜“ImgByteArray”大小是正确的,因为从这个数组值中,我可以得到我的原始位图。

As I noted in comments, by reading the whole file with CFile , you are also reading the bitmap headers, which will be corrupting your data. 正如我在评论中指出的那样,通过使用CFile读取整个文件,您还将读取位图标题,这将损坏您的数据。

Here is an example function, showing how to load a monochrome bitmap from file, wrap it in MFC's CBitmap object, query the dimensions etc. and read the pixel data into an array: 这是一个示例函数,显示了如何从文件加载单色位图,将其包装在MFC的CBitmap对象中,查询尺寸等以及如何将像素数据读取到数组中:

void LoadMonoBmp(LPCTSTR szFilename)
{
    // load bitmap from file
    HBITMAP hBmp = (HBITMAP)LoadImage(NULL, szFilename, IMAGE_BITMAP, 0, 0,
                                          LR_LOADFROMFILE | LR_MONOCHROME);

    // wrap in a CBitmap for convenience
    CBitmap *pBmp = CBitmap::FromHandle(hBmp);

    // get dimensions etc.
    BITMAP pBitMap;
    pBmp->GetBitmap(&pBitMap);

    // allocate a buffer for the pixel data
    unsigned int uBufferSize = pBitMap.bmWidthBytes * pBitMap.bmHeight;
    unsigned char *pPixels = new unsigned char[uBufferSize];

    // load the pixel data
    pBmp->GetBitmapBits(uBufferSize, pPixels);

    // ... do something with the data ....

    // release pixel data
    delete [] pPixels;
    pPixels = NULL;

    // free the bmp
    DeleteObject(hBmp);
}

The BITMAP structure will give you information about the bitmap ( MSDN here ) and, for a monochrome bitmap, the bits will be packed into the bytes you read. BITMAP结构将为您提供有关位图的信息( 此处MSDN ),对于单色位图,这些位将打包为您读取的字节。 This may be another difference with the C# code, where it is possible that each bit is unpacked into a whole byte. 这可能与C#代码的另一个不同之处,在C#代码中,每个位都可能被解包为一个完整的字节。 In the MFC version, you will need to interpret this data correctly. 在MFC版本中,您将需要正确解释此数据。

Roger Rowland, After processing data pPixels, how we can display them? Roger Rowland, 处理数据 pPixels 后,我们如何显示它们? how we can make Bitmap again?我们如何再次制作位图? Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM