像素矩阵存为文件的几种方式

有时候从某些帧中取得像素矩阵之后,需要存为文件或者作为图片数据使用,些时需要将内存转换为相关的句柄或者文件

相关知识:

不压缩的格式通常:

bmp 文件比较大,常作为windows壁纸

png 从bmp进行了进行了压缩,可还原,当前阶段比较合适的文件格式

jpg 有损压缩不可还原,早期web图片常用格式

 

方式一:完全构造文件(构造文件信息,格式信息,像素信息)

void  SavePixels(const char* data, int width, int heigh, int size)
{
    BITMAPFILEHEADER     bmfHdr;
    BITMAPINFOHEADER     bi;
    const int count = 4;//通道数
    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = width;        //1920;
    bi.biHeight = heigh;    //1200;
    bi.biPlanes = 1;
    bi.biBitCount = count * 8;
    bi.biCompression = BI_RGB;// BI_JPEG;//BI_RGB;
    bi.biSizeImage = 0;// size;// 0;
    bi.biXPelsPerMeter = 0;
    bi.biYPelsPerMeter = 0;
    bi.biClrImportant = 0;
    bi.biClrUsed = 0;

    auto biBitCount = count * 8;

    auto dwBmBitsSize = ((width * biBitCount + biBitCount - 1) / biBitCount) * count * heigh;

    auto dwPaletteSize = 1;

    auto dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize * dwBmBitsSize;

    bmfHdr.bfType = 0x4D42;     //     "BM"              
    bmfHdr.bfSize = dwDIBSize;
    bmfHdr.bfReserved1 = 0;
    bmfHdr.bfReserved2 = 0;
    bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) * dwPaletteSize;

    //创建位图文件  
    auto fh = CreateFile(IMGE_PATH_BMP, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);

    if (fh == INVALID_HANDLE_VALUE)
        return;

    DWORD dwWritten = 0;

    //     写入位图文件头              
    WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
    //     写入位图文件其余内容              
    WriteFile(fh, (LPSTR)&bi, sizeof(bi), &dwWritten, NULL);

    WriteFile(fh, (LPSTR)data /*&data[0]*/, dwBmBitsSize, &dwWritten, NULL);

    CloseHandle(fh);
}

方式二:gdiplus方式保存,构造图片格式,较为省事

void CImgeSave::savepng(const char* data, int bits, int width, int heigh)
{
    void* pBuf = (void*)data;

    //像素转换为句柄
    {
        // 构建位图头
        BITMAPINFOHEADER bmpInfoHeader;
        memset(&bmpInfoHeader, 0, sizeof(BITMAPINFOHEADER));
        bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
        bmpInfoHeader.biWidth = width;
        bmpInfoHeader.biHeight = heigh;
        bmpInfoHeader.biPlanes = 1;         // MSDN:This value must be set to 1
        bmpInfoHeader.biBitCount = bits * 8;
        bmpInfoHeader.biCompression = BI_RGB;

        BITMAPINFO BitmapInfo;
        ZeroMemory(&BitmapInfo, sizeof(BITMAPINFO));
        CopyMemory(&BitmapInfo.bmiHeader, &bmpInfoHeader, sizeof(BITMAPINFOHEADER));

        HBITMAP hBitmap = ::CreateDIBitmap(::GetDC(NULL), &bmpInfoHeader, CBM_INIT, pBuf, &BitmapInfo, DIB_RGB_COLORS);

        // 保存图像,查看验证
        CImage  image;
        image.Attach(hBitmap);
        image.Save(TEXT("c:\\2013_05_09.jpg"));

        if (hBitmap)
        {
            HDC   hdc = GetDC(m_view.m_hWnd);;
            HDC hdcBits;
            BITMAP bm;
            hdcBits = CreateCompatibleDC(hdc);
            GetObject(hBitmap, sizeof(BITMAP), &bm);
            SelectObject(hdcBits, hBitmap);
            BitBlt(hdc, 20, 10, bm.bmWidth, bm.bmHeight, hdcBits, 0, 0, SRCCOPY);
            //BitBlt(hdc, 20, 10, bm.bmWidth, bm.bmHeight, hdcBits, 0, 0, SRCCOPY);
            DeleteDC(hdcBits);
            DeleteObject(hBitmap);
        }
    }
}

 

posted @ 2020-08-03 19:28  IceArrow  阅读(323)  评论(0编辑  收藏  举报