I wrote the following code to save bitmap to a file. But what i am getting is a black image. This is my code Any suggestions
HWND hParent=NULL,hChild = NULL;
bool bRet = IsProcessRunning("view.exe");
hParent = ::FindWindow("WindowViewer", NULL);
counter++;
if (!hParent)
{
AfxMessageBox("not able to find WindowViewer class");
}
if(hParent)
hChild = ::FindWindowEx(hParent,NULL,"ViewPU",NULL);
if (!hChild)
{
AfxMessageBox("not able to find ViewPU class",NULL,NULL);
}
::SendMessage(hChild, WM_SYSCOMMAND, SC_HOTKEY, (LPARAM) hChild);
::SendMessage(hChild, WM_SYSCOMMAND, SC_RESTORE, (LPARAM) hChild);
::ShowWindow(hChild, SW_SHOWNORMAL);
::SetForegroundWindow(hChild);
::SetFocus(hChild);
RECT rc;
BYTE* m_pDrawingSurfaceBits=NULL;
BITMAPINFOHEADER bmpInfoHeader = {0};;
HBITMAP hbmDIBSection = NULL;
char* szAppName = "view.exe";
bRet = IsProcessRunning(szAppName);
if (!bRet)
{
::MessageBox(NULL,"Application is not running",NULL,NULL);
}
::SendMessage(hChild, WM_SYSCOMMAND, SC_HOTKEY, (LPARAM) hChild);
::SendMessage(hChild, WM_SYSCOMMAND, SC_RESTORE, (LPARAM) hChild);
::GetWindowRect(hChild, &rc);
HDC hDC = ::GetDC(hChild);
HDC hSrcMemDC = CreateCompatibleDC(hDC);
HDC hDstMemDC = CreateCompatibleDC(hDC);
long width = rc.right-rc.left;
long height = rc.bottom-rc.top;
bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfoHeader.biBitCount = 24;
bmpInfoHeader.biPlanes = 1;
bmpInfoHeader.biCompression = BI_RGB;
bmpInfoHeader.biWidth =width;
bmpInfoHeader.biHeight = height;
bmpInfoHeader.biSizeImage = ((((bmpInfoHeader.biWidth * bmpInfoHeader.biBitCount) + 31) & ~31) >> 3) * bmpInfoHeader.biHeight;
hbmDIBSection = CreateDIBSection(hDC, (CONST BITMAPINFO*)&bmpInfoHeader, DIB_RGB_COLORS, (void**)&m_pDrawingSurfaceBits, NULL, 0);
HBITMAP hbmOld = (HBITMAP) SelectObject(hSrcMemDC, hbmDIBSection);
bRet = BitBlt(hDstMemDC,0,0,width,height,hSrcMemDC,0,0,SRCCOPY);
if (bRet == 0) {
DWORD d=::GetLastError();
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0,
NULL
);
::MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
::KillTimer(NULL,idTimer);}
BITMAPFILEHEADER bfh = {0};
bfh.bfType = 0X4D42;
bfh.bfOffBits = sizeof(BITMAPINFOHEADER)+sizeof(BITMAPFILEHEADER);
bfh.bfSize = bfh.bfOffBits+bmpInfoHeader.biSizeImage;
bRet = SetCurrentDirectory("c:\\testBitmap");
DWORD ret = GetLastError();
char filename[200];
char *temp = "test";
char *dir= "c:\\testBitmap\\";
int j;
j = sprintf(filename,"%s",dir);
j+= sprintf(filename+j,"%s",temp);
j+= sprintf(filename+j,"%d",counter);
j+= sprintf(filename+j,"%s",".bmp");
HANDLE hFile = CreateFile(filename,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (!hFile)
{
return ;
}
DWORD dwWritten = 0;
bRet = WriteFile(hFile,&bfh,sizeof(bfh),&dwWritten,NULL);
bRet = WriteFile(hFile,&bmpInfoHeader,sizeof(bmpInfoHeader),&dwWritten,NULL);
bRet = WriteFile(hFile,m_pDrawingSurfaceBits,bmpInfoHeader.biSizeImage,&dwWritten,NULL);
bRet = CloseHandle(hFile);
SelectObject(hSrcMemDC, hbmOld);
DeleteDC(hSrcMemDC);
|