首页 > 代码库 > vc/mfc获取rgb图像数据后动态显示及保存图片的方法
vc/mfc获取rgb图像数据后动态显示及保存图片的方法
vc/mfc获取rgb图像数据后动态显示及保存图片的方法
该情况可用于视频通信中获取的位图数据回放显示或显示摄像头捕获的本地图像
第一种方法
#include<vfw.h>
加载 vfw32.lib 链接库
[cpp] view plaincopy
- //------------------------------设置位图头结构信息----------------------------------------------------------------------
- // 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);
- }
//***********************************************
第二种方法
[cpp] view plaincopy
- 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图片文件
[cpp] view plaincopy
- // 创建位图文件
- 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;
- }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。