Visual c++ 读取位图并显示到对话框上的某个范围
问题:无法将图像显示到屏幕上?? 我正在寻找一种方法,我可以从相机和一个文件,我可以修改位图使用GetDIBits和SetDIBits和写到屏幕上 到目前为止,从文件到屏幕。。。不起作用Visual c++ 读取位图并显示到对话框上的某个范围,visual-c++,Visual C++,问题:无法将图像显示到屏幕上?? 我正在寻找一种方法,我可以从相机和一个文件,我可以修改位图使用GetDIBits和SetDIBits和写到屏幕上 到目前为止,从文件到屏幕。。。不起作用 HDC hdcScreen; HDC hdcWindow; HDC hdcMemDC = NULL; HBITMAP hbmScreen = NULL; BITMAP bmpScreen; BITMAPFILEHEADER bmfHeader; BITMAPINFOHEADER bi; BIT
HDC hdcScreen;
HDC hdcWindow;
HDC hdcMemDC = NULL;
HBITMAP hbmScreen = NULL;
BITMAP bmpScreen;
BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bi;
BITMAPINFO bif;
// Retrieve the handle to a display device context for the client
// area of the window.
hdcScreen = ::GetDC(NULL);
// hdcWindow = ::GetDC(hWndC);
// Create a compatible DC which is used in a BitBlt from the window DC
hdcMemDC = CreateCompatibleDC(hdcWindow);
HANDLE hFile = ::CreateFile("c:\\captureqwsx.bmp",
GENERIC_READ,
0,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
DWORD nBytesRead = 0;
ReadFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &nBytesRead, NULL);
ReadFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &nBytesRead, NULL);
//HANDLE hDIB = GlobalAlloc(GHND,dwBmpSize);
char *lpbitmap;// = (char *)GlobalLock(hDIB);
DWORD dwBmpSize;// = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpScreen.bmHeight;
ReadFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &nBytesRead, NULL);
//Close the handle for the file that was created
CloseHandle(hFile);
//CRect rect;
//GetClientRect(&rect);
bif.bmiHeader = bi;
HDC hDC = hdcWindow;
HBITMAP hBitmap;
HDC hMemDC;
hBitmap = CreateCompatibleBitmap(hDC, bi.biWidth, bi.biHeight);
hMemDC = CreateCompatibleDC(hDC);
SetDIBits(hDC, hBitmap, 0, bi.biHeight, lpbitmap, &bif, DIB_RGB_COLORS);
SelectObject(hMemDC, hBitmap);
BitBlt(hDC, 0, 0, bi.biWidth, bi.biHeight, hMemDC, 0, 0, SRCCOPY);
DeleteObject(SelectObject(hMemDC, hBitmap));
DeleteDC(hMemDC);
dcMem.DeleteDC();
hbmScreen=CreateCompatibleBitmap(dc,m_rectFrame2.right-m_rectFrame2.left,m_rectFrame2.bottom-m_rectFrame2.top);// 在这里,您可以检索整个屏幕的DC。这就是你真正想要的吗
GetDC(NULL);
也许您应该删除它并取消注释以下行:
// hdcWindow = ::GetDC(hWndC);
hOldBitmap = SelectObject(hMemDC, hBitmap);
根据发布的示例,您正在使用未初始化的“hdcWindow”参数调用CreateCompatibleDC()
hdcMemDC = CreateCompatibleDC(hdcWindow);
读取BITMAPFILEHEADER和BITMAPINFOHEADER后,应将文件指针移动到BITMAPFILEHEADER::bfOffBits offest。
然后调用ReadFile()读取位图本身。顺便说一句,所需的缓冲区大小在BitMapInfo标头::biSizeImage中
由于hdcWindow未初始化,hDC也保持未初始化状态:
hDC = hdcWindow;
是否检查了SetDIBits()的返回值
尝试修改以下行:
// hdcWindow = ::GetDC(hWndC);
hOldBitmap = SelectObject(hMemDC, hBitmap);
然后,在BitBlt()之后:
我不知道这是否是你问题的最终解决方案,但这只是一个开始