MFC对话框播放8位512*512的像素数据

关键代码:

UINT playAllFrame(LPVOID lpParameter){
    //showOneFrame(0,TRUE);
    CMFCDialogDlg *mydlg =(CMFCDialogDlg *)  lpParameter;

    //获取原始数据文件
    CString selectPath;
    mydlg->GetDlgItemTextW(IDC_MFCEDITBROWSE,selectPath);
    string StrSelectPath(CW2A(selectPath.GetString()));
    const char * fileName = StrSelectPath.data();

    FILE *fin ; 
    byte *pBmpData = (BYTE*)new char[bitmapWidth*bitmapHeight];//位图数据
    
    BITMAPINFO * pBmpInfo = (BITMAPINFO *)new char[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256];

    for(int i = 0;i<256;i++){
        pBmpInfo->bmiColors[i].rgbRed = i;
        pBmpInfo->bmiColors[i].rgbGreen = i;
        pBmpInfo->bmiColors[i].rgbBlue = i;
        pBmpInfo->bmiColors[i].rgbReserved = 0;
    }

    //BITMAPINFO * pBmpInfo = new BITMAPINFO;
    

    pBmpInfo->bmiHeader.biBitCount = 8;
    pBmpInfo->bmiHeader.biClrImportant = 0;
    pBmpInfo->bmiHeader.biClrUsed = 0;
    pBmpInfo->bmiHeader.biCompression = 0;
    pBmpInfo->bmiHeader.biHeight = bitmapHeight;//这里是512
    pBmpInfo->bmiHeader.biPlanes = 0x01;
    pBmpInfo->bmiHeader.biSize = 40;
    pBmpInfo->bmiHeader.biSizeImage =262144;
    pBmpInfo->bmiHeader.biWidth = bitmapWidth;//这里是512
    pBmpInfo->bmiHeader.biXPelsPerMeter = 3780;
    pBmpInfo->bmiHeader.biYPelsPerMeter = 3780;

    errno_t er =fopen_s(&fin,fileName,"rb");
    if(er!=0){
        mydlg->MessageBox(L"文件打开失败!");
        //break;
    }
    fseek(fin,0,SEEK_END);
    LONG fileSize = ftell(fin);
    fseek(fin,0,0);

    //计算总帧数
    int totalFrame = fileSize/(bitmapWidth*bitmapHeight);

    //设置下拉条范围
    mydlg->m_slider.SetRange(1,totalFrame);

    //总帧数
    mydlg->m_lbl_total=totalFrame;

    CWnd *pWnd=mydlg->GetDlgItem(IDC_STATIC_PIC_2); //获得pictrue控件窗口的句柄
    CRect rect;
    pWnd->GetClientRect(&rect); //获得pictrue控件所在的矩形区域
    CDC *pDC=pWnd->GetDC(); //获得pictrue控件的DC
    pDC->SetStretchBltMode(COLORONCOLOR);
    while (TRUE)
    {
        if(flag){
            long cur = ftell(fin)/(bitmapWidth*bitmapHeight);
            if(cur!=currentFrame&&(totalFrame>currentFrame||totalFrame>currentFrame)){
                fseek(fin,currentFrame*bitmapWidth*bitmapHeight,0);
            }
            if(!feof(fin)){
                fread(pBmpData,sizeof(BYTE),bitmapWidth*bitmapHeight,fin);
                StretchDIBits(pDC->GetSafeHdc(),0,0,rect.Width(),rect.Height(),0,0,bitmapWidth,bitmapHeight,pBmpData,pBmpInfo,DIB_RGB_COLORS,SRCCOPY);
                //Sleep(1);
                //当前帧显示值
                mydlg->m_lbl = currentFrame;
                //设置拉条位置
                mydlg->m_slider.SetPos(currentFrame);
                currentFrame = currentFrame +1;
            }else{
                flag = !flag;
                fseek(fin,0,0);
                currentFrame = 0;
                CButton *pBtn = (CButton *)mydlg->GetDlgItem(IDC_BUTTON2);
                pBtn->SetWindowTextW(_T("播放"));
            }
        }else{
            Sleep(200);
        }
    }

    fclose(fin);
    delete pBmpInfo;
    delete pBmpData;
    ExitThread(0);
    return 0;
}

运行效果:

原文地址:https://www.cnblogs.com/airduce/p/9744810.html