VC++(MFC中)根据位图数据流直接绘图的疑问? 200
近期在做vc++一个嵌入式系统的电脑软件时,需要PC根据嵌入式系统的位图数据流来进行绘图显示,参考书籍,是通过调用StretchDiBits(GetDC()->m_hDC...
近期在做vc++一个嵌入式系统的电脑软件时,需要PC根据嵌入式系统的位图数据流来进行绘图显示,参考书籍,是通过调用StretchDiBits(GetDC()->m_hDC,0,0,x,y,0,0,x,y,pHeader,&BitInfo,DIB_RGB_COLORS,SRCCOPY)来实现;我在测试时,定义一个文件对象,打开位图,然后读取文件数据到pHeader缓冲器中,确实就能根据我提取的数据流画出原来的图像,下面我做了进一步测试,通过Image2Lcd软件提取(位图)图片信息为16进C数组数据,(为了排除是传输过程中出现数据出错的影响),我直接把这个数组的数据,直接复制在MFC程序中进行调用,通过StretchDiBits函数来实现图像的重新绘制,仍然参考书中的教程使pHeader只想我提取的数据缓冲器先初始化位图对象,获取位图对象的值,高度,宽度,,但是单文档应用程序却不能画出任何图形,(没有任何改变应用程序),我考虑过是否是由于我用软件提取出来的数据为16进制数据而造成的错误呢,希望高手能指教,在此谢谢各位,
展开
2个回答
展开全部
// int i, j;
HANDLE hMapFile;
HBITMAP hBmp;
HBITMAP hbmOld;
void* lpbi = NULL;
HDC hdc = GetDC(hWnd);
unsigned short* pBuf = (unsigned short*)malloc(800*480*sizeof(unsigned short));
HDC mdc = CreateCompatibleDC(hdc);
BITMAPINFO* bmp_info = (BITMAPINFO*)malloc(sizeof(BITMAPINFO)+sizeof(RGBQUAD)*(2));
// BITMAPFILEHEADER hdr;
BITMAP bmp;
while((hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
szName)) == NULL);
pBuf = (unsigned short*) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
800*480*sizeof(unsigned short));
if (pBuf == NULL)
{
MessageBox(NULL, TEXT("Could not map view of file"), TEXT("error"), MB_OK);
return 1;
}
bmp_info->bmiHeader.biHeight = -480;
bmp_info->bmiHeader.biWidth = 800;
bmp_info->bmiHeader.biPlanes = 1;
bmp_info->bmiHeader.biBitCount = 16;
bmp_info->bmiHeader.biCompression = BI_RGB;
bmp_info->bmiHeader.biSizeImage = 0;
bmp_info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); hBmp = CreateDIBSection(NULL, bmp_info, DIB_RGB_COLORS, (void **)&lpbi, hMapFile, 0);
if(lpbi == NULL)
{
MessageBox(NULL, TEXT("Vp is NULL"), TEXT("error"), MB_OK);
return 1;
}
hbmOld = (HBITMAP)::SelectObject(mdc, hBmp);
BitBlt(hdc, 0, 0, 800, 480, mdc, 0, 0, SRCCOPY);
::SelectObject(mdc, hbmOld);
GetObject(hBmp, sizeof(BITMAP), &bmp);
DWORD dwBmpSize = ((bmp.bmWidth * bmp_info->bmiHeader.biBitCount + 15) / 16) * 2 * bmp.bmHeight;
HANDLE hDIB = GlobalAlloc(GHND,dwBmpSize);
char* Vp = (char*)GlobalLock(hDIB);
GetDIBits(hdc, hBmp, (UINT)0, (UINT)bmp.bmHeight,
(LPVOID)Vp,
(BITMAPINFO *)bmp_info, DIB_RGB_COLORS);
HANDLE hFile = CreateFile(L"image_source.bmp",
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
BITMAPFILEHEADER bmfHeader;
bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
bmfHeader.bfSize = dwSizeofDIB;
bmfHeader.bfType = 0x4d42;
DWORD dwByteWritten = 0;
WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &dwByteWritten, NULL);
WriteFile(hFile, (LPSTR)&bmp_info->bmiHeader, sizeof(BITMAPINFOHEADER), &dwByteWritten, NULL);
WriteFile(hFile, (LPSTR)Vp, dwBmpSize, &dwByteWritten, NULL);
CloseHandle(hFile);
DeleteDC(mdc);
delete bmp_info;
UnmapViewOfFile(pBuf);
GlobalUnlock(hDIB);
GlobalFree(hDIB);
CloseHandle(hMapFile);
上面是我存的16位rgb565的图, 共享内存是获取像素数组的文件. 你参考一下, 主要是把头写好了.
你也可以用directdraw. 有个蚊子飞的程序挺经典的. 看看吧. 不知道是不是你想要的, 前段时间我也是折腾这个, 蛋都碎了的说.
HANDLE hMapFile;
HBITMAP hBmp;
HBITMAP hbmOld;
void* lpbi = NULL;
HDC hdc = GetDC(hWnd);
unsigned short* pBuf = (unsigned short*)malloc(800*480*sizeof(unsigned short));
HDC mdc = CreateCompatibleDC(hdc);
BITMAPINFO* bmp_info = (BITMAPINFO*)malloc(sizeof(BITMAPINFO)+sizeof(RGBQUAD)*(2));
// BITMAPFILEHEADER hdr;
BITMAP bmp;
while((hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
szName)) == NULL);
pBuf = (unsigned short*) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
800*480*sizeof(unsigned short));
if (pBuf == NULL)
{
MessageBox(NULL, TEXT("Could not map view of file"), TEXT("error"), MB_OK);
return 1;
}
bmp_info->bmiHeader.biHeight = -480;
bmp_info->bmiHeader.biWidth = 800;
bmp_info->bmiHeader.biPlanes = 1;
bmp_info->bmiHeader.biBitCount = 16;
bmp_info->bmiHeader.biCompression = BI_RGB;
bmp_info->bmiHeader.biSizeImage = 0;
bmp_info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); hBmp = CreateDIBSection(NULL, bmp_info, DIB_RGB_COLORS, (void **)&lpbi, hMapFile, 0);
if(lpbi == NULL)
{
MessageBox(NULL, TEXT("Vp is NULL"), TEXT("error"), MB_OK);
return 1;
}
hbmOld = (HBITMAP)::SelectObject(mdc, hBmp);
BitBlt(hdc, 0, 0, 800, 480, mdc, 0, 0, SRCCOPY);
::SelectObject(mdc, hbmOld);
GetObject(hBmp, sizeof(BITMAP), &bmp);
DWORD dwBmpSize = ((bmp.bmWidth * bmp_info->bmiHeader.biBitCount + 15) / 16) * 2 * bmp.bmHeight;
HANDLE hDIB = GlobalAlloc(GHND,dwBmpSize);
char* Vp = (char*)GlobalLock(hDIB);
GetDIBits(hdc, hBmp, (UINT)0, (UINT)bmp.bmHeight,
(LPVOID)Vp,
(BITMAPINFO *)bmp_info, DIB_RGB_COLORS);
HANDLE hFile = CreateFile(L"image_source.bmp",
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
BITMAPFILEHEADER bmfHeader;
bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
bmfHeader.bfSize = dwSizeofDIB;
bmfHeader.bfType = 0x4d42;
DWORD dwByteWritten = 0;
WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &dwByteWritten, NULL);
WriteFile(hFile, (LPSTR)&bmp_info->bmiHeader, sizeof(BITMAPINFOHEADER), &dwByteWritten, NULL);
WriteFile(hFile, (LPSTR)Vp, dwBmpSize, &dwByteWritten, NULL);
CloseHandle(hFile);
DeleteDC(mdc);
delete bmp_info;
UnmapViewOfFile(pBuf);
GlobalUnlock(hDIB);
GlobalFree(hDIB);
CloseHandle(hMapFile);
上面是我存的16位rgb565的图, 共享内存是获取像素数组的文件. 你参考一下, 主要是把头写好了.
你也可以用directdraw. 有个蚊子飞的程序挺经典的. 看看吧. 不知道是不是你想要的, 前段时间我也是折腾这个, 蛋都碎了的说.
东莞大凡
2024-08-07 广告
2024-08-07 广告
OpenCV标定板是东莞市大凡光学科技有限公司在相机标定中常用的工具。它通常由黑白格点按一定规则排列在平面上组成,如棋盘格或圆形格等。在相机标定时,将标定板置于不同位置和姿态下拍摄图像,利用OpenCV库中的函数检测标定板上的角点或圆心,进...
点击进入详情页
本回答由东莞大凡提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询