该情况可用于视频通信中获取的位图数据回放显示或显示摄像头捕获的本地图像
第一种方法
#include<vfw.h>
加载 vfw32.lib 链接库
- //------------------------------设置位图头结构信息----------------------------------------------------------------------
- // Setup bmpinfo structure yourself
- m_bmpinfo=new BITMAPINFO; // PBITMAPINFO *m_bmpinfo;
- // m_bmpinfo->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
- m_bmpinfo->bmiHeader.biSize=sizeof(BITMAPINFO);
- m_bmpinfo->bmiHeader.biWidth=IMG_WIDTH;
- m_bmpinfo->bmiHeader.biHeight=IMG_HEIGHT;
- m_bmpinfo->bmiHeader.biPlanes=1;
- m_bmpinfo->bmiHeader.biBitCount=24;
- m_bmpinfo->bmiHeader.biCompression=0;
- m_bmpinfo->bmiHeader.biSizeImage=0;
- m_bmpinfo->bmiHeader.biXPelsPerMeter=0;
- m_bmpinfo->bmiHeader.biYPelsPerMeter=0;
- m_bmpinfo->bmiHeader.biClrUsed=0;
- m_bmpinfo->bmiHeader.biClrImportant=0;
- //-------------在内存中绘制位图头信息-----------------------------------------------------------------------------------
- // Initialize DIB for drawing...
- hdib=::DrawDibOpen(); // HDRAWDIB hdib;
- if(hdib!=NULL)
- {
- ::DrawDibBegin(hdib,
- m_hdc,
- -1, // don't stretch
- -1, // don't stretch
- &m_bmpinfo->bmiHeader,
- IMG_WIDTH, // width of image
- IMG_HEIGHT, // height of image
- 0
- );
- }
- //---------------绘制位图数据 buf--------------char* buf--------------------------------------------------------------------
- ::DrawDibDraw(hdib,
- m_hdc,
- local_wnd_x, // dest : left pos
- local_wnd_y, // dest : top pos
- -1, // -1 don't zoom x
- -1, // -1 don't zoom y
- &m_bmpinfo->bmiHeader, // bmp header info
- buf, // bmp data
- 0, // src :left
- 0, // src :top
- IMG_WIDTH, // src : width
- IMG_HEIGHT, // src : height
- DDF_SAME_DRAW // use prev params....
- );
- //------------------------------结束绘制---------------------------------------------------------------------
- // Close graphics....
- if(hdib!=NULL)
- {
- ::DrawDibEnd(hdib);
- ::DrawDibClose(hdib);
- }
//***********************************************
第二种方法
- BITMAPINFO *m_bmphdr;
- DWORD dwBmpHdr = sizeof(BITMAPINFO);
- m_bmphdr = new BITMAPINFO[dwBmpHdr];
- m_bmphdr->bmiHeader.biBitCount = 24;
- m_bmphdr->bmiHeader.biClrImportant = 0;
- m_bmphdr->bmiHeader.biSize = dwBmpHdr;
- m_bmphdr->bmiHeader.biSizeImage = 0;
- m_bmphdr->bmiHeader.biWidth = IMG_WIDTH;
- m_bmphdr->bmiHeader.biHeight = IMG_HEIGHT;
- m_bmphdr->bmiHeader.biXPelsPerMeter = 0;
- m_bmphdr->bmiHeader.biYPelsPerMeter = 0;
- m_bmphdr->bmiHeader.biClrUsed = 0;
- m_bmphdr->bmiHeader.biPlanes = 1;
- m_bmphdr->bmiHeader.biCompression = BI_RGB;
- int nResult = ::StretchDIBits(m_hdc,
- local_wnd_x,
- local_wnd_y,
- IMG_WIDTH,//rc.right - rc.left,
- IMG_HEIGHT,//rc.top,
- 0, 0,
- IMG_WIDTH, IMG_HEIGHT,
- buf,
- m_bmphdr,
- DIB_RGB_COLORS,
- SRCCOPY);
保存成bmp图片文件
- // 创建位图文件
- BOOL SaveBitmap(BYTE *pBuffer, long lBufferLen)
- {
- HANDLE hf = CreateFile(m_szFileName, GENERIC_WRITE,
- FILE_SHARE_READ, NULL, CREATE_ALWAYS, NULL, NULL);
- if (hf == INVALID_HANDLE_VALUE) return 0;
- // 写文件头
- BITMAPFILEHEADER fileheader;
- ZeroMemory(&fileheader, sizeof(BITMAPFILEHEADER));
- fileheader.bfType = 'MB';
- fileheader.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+lBufferLen;
- fileheader.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
- DWORD dwWritter = 0;
- WriteFile(hf, &fileheader, sizeof(BITMAPFILEHEADER), &dwWritter, NULL);
- // 写文图格式
- BITMAPINFOHEADER infoHeader;
- ZeroMemory(&infoHeader, sizeof(BITMAPINFOHEADER));
- infoHeader.biSize = sizeof(BITMAPINFOHEADER);
- infoHeader.biSizeImage = lBufferLen;
- infoHeader.biWidth = lWidth;
- infoHeader.biHeight = lHeight;
- infoHeader.biBitCount = 24;
- WriteFile(hf, &infoHeader, sizeof(BITMAPINFOHEADER), &dwWritter, NULL);
- // 写位图数据
- WriteFile(hf, pBuffer, lBufferLen, &dwWritter, NULL);
- CloseHandle(hf);
- MessageBox(NULL, _T("Save bmp file succeed!"), _T("warn"), MB_OK|MB_ICONINFORMATION);
- return 0;
- }