Windows 스크린샷을 메모리 및 BMP에 저장
HWND hWnd = ::GetDesktopWindow();// HWND.
HDC hScreenDC = ::GetDC(hWnd); // HDC.
HDC MemDC = ::CreateCompatibleDC(hScreenDC);
RECT rect;
::GetWindowRect(hWnd,&rect);
SIZE screensize;
screensize.cx=600;
screensize.cy=400;
// hBitmap = ::CreateCompatibleBitmap(hScreenDC,screensize.cx,screensize.cy);
HBITMAP hBitmap = ::CreateCompatibleBitmap(hScreenDC,screensize.cx,screensize.cy);// 。
HGDIOBJ hOldBMP = ::SelectObject(MemDC,hBitmap);
// ::BitBlt(MemDC,0,0,screensize.cx,screensize.cy,hScreenDC,rect.left,rect.top,SRCCOPY);//by tang wei
::BitBlt(MemDC,0,0,screensize.cx,screensize.cy,hScreenDC,rect.left,rect.top,SRCCOPY);// ,
::SelectObject(MemDC,hOldBMP);
::DeleteObject(MemDC);
::ReleaseDC(hWnd,hScreenDC);
////////////////////////////////////////////////////////
//by Tang Wei
SYSTEMTIME st;
GetLocalTime(&st);
new_filename.Format("%.2d-%.2d-%.2d",st.wHour,st.wMinute,st.wSecond);
CString addr("data_bmp//");
CString new_format1(".bmp");
CString new_format2(".txt");
CString new_name1 = addr + new_filename + new_format1;
CString new_name2 = addr + new_filename + new_format2;
UpdateData();
WriteBmpToFile(hBitmap,new_name1,new_name2);
void CTestbmpcpDlg::WriteBmpToFile(HBITMAP hBitmap,CString strFileName1,CString strFileName2)
{
HDC hDC =::CreateDC("DISPLAY",NULL,NULL,NULL);
int iBits = ::GetDeviceCaps(hDC, BITSPIXEL) * ::GetDeviceCaps(hDC, PLANES);//
::DeleteDC(hDC);
WORD wBitCount; //
if (iBits <= 1)
wBitCount = 1;
else if (iBits <= 4)
wBitCount = 4;
else if (iBits <= 8)
wBitCount = 8;
else if (iBits <= 24)
wBitCount = 24;
else
wBitCount = iBits;
DWORD dwPaletteSize=0; // ,
if (wBitCount <= 8)
dwPaletteSize = (1 << wBitCount) * sizeof(RGBQUAD);
BITMAP bm; //
::GetObject(hBitmap, sizeof(bm), (LPSTR)&bm);
BITMAPINFOHEADER bi,bi1; //
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wBitCount;
bi.biCompression = BI_RGB; //BI_RGB
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
bi1=bi;
bi1.biBitCount=24;
DWORD dwBmBitsSize = ((bm.bmWidth * wBitCount+31)/32) * 4 * bm.bmHeight;
HANDLE hDib = ::GlobalAlloc(GHND,dwBmBitsSize + dwPaletteSize + sizeof(BITMAPINFOHEADER)); //
LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
*lpbi = bi;
HANDLE hPal = ::GetStockObject(DEFAULT_PALETTE); //
HANDLE hOldPal=NULL;
if (hPal)
{
hDC = ::GetDC(NULL);
hOldPal = SelectPalette(hDC,(HPALETTE)hPal, FALSE);
RealizePalette(hDC);
}
int nOutputBytes = 0;
unsigned char *pJpp,*m_pDibBits;
pJpp = new unsigned char [dwBmBitsSize];
m_pDibBits = new unsigned char [dwBmBitsSize];
//::GetDIBits(hDC, hBitmap, 0, (UINT) bm.bmHeight,m_pDibBits,(BITMAPINFO*)lpbi,DIB_RGB_COLORS);
::GetDIBits(hDC, hBitmap, 0, (UINT) bm.bmHeight,(LPSTR)lpbi + sizeof(BITMAPINFOHEADER)+dwPaletteSize,(BITMAPINFO*)lpbi,DIB_RGB_COLORS);//
if (hOldPal)//
{
SelectPalette(hDC, (HPALETTE)hOldPal, TRUE);
RealizePalette(hDC);
::ReleaseDC(NULL, hDC);
}
memcpy(m_pDibBits,(LPSTR)lpbi+sizeof(BITMAPINFOHEADER)+dwPaletteSize,dwBmBitsSize); // by tang wei m_pDibBits BMP
/* int i=0,j=0;
for(j=0;j<dwBmBitsSize;j++)
{
if(j%4==3)
j++;
m_pDibBits[i]=m_pDibBits[j];
i++;
}*/
BITMAPFILEHEADER bmfHdr; //
bmfHdr.bfType = 0x4D42; // "BM" //
DWORD dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmBitsSize;
bmfHdr.bfSize = dwDIBSize;
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize;
HANDLE hFile = CreateFile(strFileName1, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);//
DWORD dwWritten;
WriteFile(hFile, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL); //
WriteFile(hFile, (LPSTR)lpbi, dwDIBSize, &dwWritten, NULL);//
//By Tang Wei txt 。
//FILE* pFile;
//pFile = fopen(strFileName2,"wb");
//fwrite(m_pDibBits,dwBmBitsSize,1,pFile);
ofstream outfile(strFileName2);
int i;
byte temp;
int temp_int;
for (i=0 ; i<dwBmBitsSize; i++)
{
temp = *(m_pDibBits+i);
temp_int = temp;
outfile<<temp_int<<' ';
}
//
char* BmpBuffer;
BmpBuffer = new char [sizeof(BITMAPFILEHEADER) + dwDIBSize];
memcpy(BmpBuffer,(LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER));
memcpy(BmpBuffer+sizeof(BITMAPFILEHEADER),(LPSTR)lpbi, dwDIBSize);
GlobalUnlock(hDib); //
GlobalFree(hDib);
CloseHandle(hFile);
delete []pJpp;
delete []m_pDibBits;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
제한된 크기의 디렉토리를 만드는 방법오늘 저는 장치에 공간이 없을 때 백업 중에 응용 프로그램이 어떻게 작동하는지 테스트(및 수정)하는 작업이 있습니다. 결과적으로 "남은 공간 없음"오류로 백업이 실패하면 새 파일이 없어야 합니다. 지금까지 문제를 재...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.