阅读位图和dispay到对话框范围

问题:无法获取的图像在屏幕上...? 我找的,我可以从相机,我可以使用的GetDIBits和SetDIBits修改位图和写入屏幕处理文件的方法。

所以,从文件到屏幕远...不工作

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);

--------------解决方案-------------

在这里,您检索DC整个屏幕。 那是你真正想要什么?

GetDC(NULL);

也许你应该删除并取消对以下行:

// hdcWindow = ::GetDC(hWndC);

据公布的例子,您呼叫CreateCompatibleDC()未初始化的“hdcWindow”的说法。

hdcMemDC = CreateCompatibleDC(hdcWindow);

阅读BITMAPFILEHEADER和BITMAPINFOHEADER后,你应该将文件指针指向BITMAPFILEHEADER :: bfOffBits offest。 那么你应该调用的ReadFile()读取位图本身。 顺便说一句,所需的缓冲区大小是BITMAPINFOHEADER :: biSizeImage。

由于hdcWindow未初始化,为HDC仍然未初始化过:

hDC = hdcWindow;

你检查SetDIBits的返回值()?

尝试修改下面一行:

hOldBitmap = SelectObject(hMemDC, hBitmap);

然后,之后的BitBlt():

DeleteObject(SelectObject(hMemDC, hOldBitmap));

我不知道,如果这是你的问题的最终解决方案,但它是一个开始。

dcMem.DeleteDC();
hbmScreen = CreateCompatibleBitmap(dc, m_rectFrame2.right-m_rectFrame2.left, m_rectFrame2.bottom-m_rectFrame2.top);// <-- needed
dcMem.CreateCompatibleDC(&dc);
hbmScreen = (HBITMAP)LoadImage(NULL, _T("c:\\captureqwsx.bmp") ,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
//for setDiBits
SetDIBits(hdcWindow, hbmScreen, 0,
(UINT)bmpScreen.bmHeight,
lpbitmap,
(BITMAPINFO *)&bi, DIB_RGB_COLORS);
dcMem.SelectObject(hbmScreen);
dc.StretchBlt(
m_rectFrame2.left, m_rectFrame2.top,
m_rectFrame2.right-m_rectFrame2.left,//GetSystemMetrics (SM_CXSCREEN),
m_rectFrame2.bottom-m_rectFrame2.top,//GetSystemMetrics (SM_CYSCREEN),
&dcMem,
0,0,
m_rectFrame2.right-m_rectFrame2.left,//GetSystemMetrics (SM_CXSCREEN),
m_rectFrame2.bottom-m_rectFrame2.top,//GetSystemMetrics (SM_CYSCREEN),
SRCCOPY);

分类:VISUAL C ++ 时间:2012-12-09 人气:0
本文关键词: VISUAL C ++
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.272 (s). 10 q(s)