开头已经写好了:
BOOL SaveToBMP(HDC hdc, //设备场景
LPCTSTR path, //保存路径
long width, //高度
long hetght //宽度
)
{
...后面怎么写?帮帮我好吗?
}
精简的代码,可以编译运行,可以方便的直接改成单独的函数.
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{
int sWidth, sHeight,i;
HDC hdc,mhdc ;
TCHAR szFileName[100],szMsg[200];
BYTE *bmBuffer;
DWORD size;
unsigned long ensize=0;
HBITMAP bm;
BITMAP bitmap;
HANDLE hFile;
hdc = CreateDC (TEXT ("DISPLAY"), NULL, NULL, NULL);
sWidth=GetSystemMetrics(SM_CXSCREEN);
sHeight=GetSystemMetrics(SM_CYSCREEN);
bm=CreateCompatibleBitmap(hdc,sWidth,sHeight);
mhdc=CreateCompatibleDC(hdc);
SelectObject(mhdc,bm);
// pOld=(HBITMAP)SelectObject(mhdc,bm);
BitBlt(mhdc,0,0,sWidth,sHeight,hdc,0,0,SRCCOPY);
// SelectObject(mhdc,pOld);
GetObject(bm,sizeof(BITMAP),(void *)&bitmap);
size=bitmap.bmWidthBytes *bitmap.bmHeight ;
bmBuffer=new BYTE[size];
BITMAPINFOHEADER bih;
bih.biBitCount=bitmap.bmBitsPixel;
bih.biClrImportant=0;
bih.biClrUsed=0;
bih.biCompression=0;
bih.biHeight=bitmap.bmHeight;
bih.biPlanes=1;
bih.biSize=sizeof(BITMAPINFOHEADER);
bih.biSizeImage=size;
bih.biWidth=bitmap.bmWidth ;
bih.biXPelsPerMeter=0;
bih.biYPelsPerMeter=0;
GetDIBits(mhdc,bm,0,sHeight,bmBuffer,(BITMAPINFO*)&bih,DIB_RGB_COLORS);
BITMAPFILEHEADER bfh;
bfh.bfReserved1=bfh.bfReserved2=0;
bfh.bfType=((WORD)('M'<< 8)|'B');
bfh.bfSize=0x36+size;
bfh.bfOffBits=0x36;
/* hFile=_lcreat(szFileName,0);
_lwrite(hFile,(char *)&bfh,sizeof(BITMAPFILEHEADER));
_lwrite(hFile,(char *)&bih,sizeof(BITMAPINFOHEADER));
_lwrite(hFile,(char *)bmBuffer,size);
_lclose(hFile);
*/
wsprintf(szFileName,TEXT("d:\\ScreenShoter.bmp"));
hFile=CreateFile(szFileName,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
WriteFile(hFile,&bfh,sizeof(BITMAPFILEHEADER),&ensize,NULL);
WriteFile(hFile,&bih,sizeof(BITMAPINFOHEADER),&ensize,NULL);
WriteFile(hFile,bmBuffer,size,&ensize,NULL);
CloseHandle(hFile);
wsprintf(szMsg,TEXT("%d Height , %d Width , %d BITS ,%d WidthBytes\n%l Bytes Saved to bmp"),bitmap.bmWidth ,bitmap.bmHeight,bitmap.bmBits,bitmap.bmWidthBytes,(long)size);
MessageBox(NULL,szMsg,"Screen",0);
return 0;
}