我想做一个屏幕截图程序,然后保存在硬盘中。大家能否赐教啊。
http://www.cnblogs.com/projectdevelop/archive/2009/06/08/1498476.html
不好意思,我以前见过是别人写的函数,API没有
BOOL CMpCurveAnalysisDlg::SaveCurBMP()
{
int nResponse;
CString strFileName;
HDC hScrDC;
HDC hMemDC;
int width=640;
int height=445;
//the pointer will save all pixel point's color value
BYTE *lpBitmapBits = NULL;
//creates a device context for the screen device
hScrDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
//get the screen point size
//width = GetDeviceCaps(hScrDC, HORZRES);
//height = GetDeviceCaps(hScrDC, VERTRES);
//width=lpRect->right-lpRect->left;
//height=lpRect->bottom-lpRect->top;
width=640;
height=445;
//creates a memory device context (DC) compatible with the screen device(hScrDC)
hMemDC = CreateCompatibleDC(hScrDC);
//initialise the struct BITMAPINFO for the bimap infomation,
//in order to use the function CreateDIBSection
// on wince os, each pixel stored by 24 bits(biBitCount=24)
//and no compressing(biCompression=0)
BITMAPINFO RGB24BitsBITMAPINFO;
ZeroMemory(&RGB24BitsBITMAPINFO, sizeof(BITMAPINFO));
RGB24BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
RGB24BitsBITMAPINFO.bmiHeader.biWidth = width;
RGB24BitsBITMAPINFO.bmiHeader.biHeight = height;
RGB24BitsBITMAPINFO.bmiHeader.biPlanes = 1;
RGB24BitsBITMAPINFO.bmiHeader.biBitCount = 24;
//use the function CreateDIBSection and SelectObject
//in order to get the bitmap pointer : lpBitmapBits
HBITMAP directBmp = CreateDIBSection(hMemDC, (BITMAPINFO*)&RGB24BitsBITMAPINFO,
DIB_RGB_COLORS, (void **)&lpBitmapBits, NULL, 0);
HGDIOBJ previousObject = SelectObject(hMemDC, directBmp);
// copy the screen dc to the memory dc
//BitBlt(hMemDC, 0, 0, width, height, hScrDC, 0, 0, SRCCOPY);
BitBlt(hMemDC,0, 0, width, height, hScrDC, 7,70,SRCCOPY);
//if you only want to get the every pixel color value,
//you can begin here and the following part of this function will be unuseful;
//the following part is in order to write file;
//bitmap file header in order to write bmp file
BITMAPFILEHEADER bmBITMAPFILEHEADER;
ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
bmBITMAPFILEHEADER.bfType = 0x4d42; //bmp
bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + ((width*height)*3); ///3=(24 / 8)
//Windows 规定一个扫描行所占的字节数必须是4的倍数。
//有可能宽度所占的像素字节数不是4的倍数,所以必须把它转换成4的倍数。
//int iWidth=800;
int iWidth=width;
int iBitCount=24;
//int iHeight=600;
int iHeight=height;
int DataSizePerLine=(iWidth*iBitCount+31)/8; //ensure the width is the multiple of 4
int DataSize=DataSizePerLine*iHeight;//实际文件的大小
//write into file
CFile BmpFile;
///////////////////////////////////////////////////////////////////////////////////////////////
CFileDialog dlgSaveCurBMP(TRUE,_T("bmp文件(*.bmp)|*.bmp||"),_T("CurveNO"),
OFN_OVERWRITEPROMPT,_T("*.bmp||"),this);
//m_dlgMpAlarmExport.Create();
dlgSaveCurBMP.m_ofn.lpstrInitialDir=_T("\\硬盘\\CurveSave\\");
nResponse=dlgSaveCurBMP.DoModal();
if(nResponse==IDOK)
{
strFileName=dlgSaveCurBMP.GetPathName();
}
BmpFile.Open(strFileName,CFile::modeCreate|CFile::modeWrite,NULL);
BmpFile.Write(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
BmpFile.Write(&(RGB24BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER));
BmpFile.Write(lpBitmapBits, DataSize);
BmpFile.Close();
//delete
DeleteObject(hMemDC);
DeleteObject(hScrDC);
DeleteObject(directBmp);
DeleteObject(previousObject);
return TRUE;
}
我运行调试出现“Disk fullaccessing\NandFlash\Y.bmp"的对话框时怎么回事啊? 帮忙看看代码啊。
void Drawninformation::SaveDCToBMP(HDC hDC,int nStartX,int nStartY, int nWidth,int nHeight,LPCTSTR szFileName)
{
//定义图形色深
int iPixel = 32;
//位图信息头
LPBITMAPINFO lpbmih = new BITMAPINFO;
lpbmih->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
lpbmih->bmiHeader.biWidth = nWidth;
lpbmih->bmiHeader.biHeight = nHeight;
lpbmih->bmiHeader.biPlanes = 1;
lpbmih->bmiHeader.biBitCount = iPixel;
lpbmih->bmiHeader.biCompression = BI_RGB;
lpbmih->bmiHeader.biSizeImage = 0;
lpbmih->bmiHeader.biXPelsPerMeter = 0;
lpbmih->bmiHeader.biYPelsPerMeter = 0;
lpbmih->bmiHeader.biClrUsed = 0;
lpbmih->bmiHeader.biClrImportant = 0;
//创建位图数据
HDC hdcMem;
HBITMAP hBitMap = NULL;
CBitmap *pBitMap = NULL;
CDC *pMemDC = NULL;
BYTE *pBits = NULL;
hdcMem = CreateCompatibleDC(hDC);
hBitMap = CreateDIBSection(hdcMem,lpbmih,DIB_PAL_COLORS,(void **)&pBits,NULL,0);
////
pBitMap = new CBitmap;
pBitMap->Attach(hBitMap);
////
pMemDC = new CDC;
pMemDC->Attach(hdcMem);
//CreateCompatibleBitmap
////
pMemDC->SelectObject(pBitMap);
pMemDC->BitBlt(0,0,nWidth,nHeight,CDC::FromHandle(hDC),nStartX,nStartY,SRCCOPY);
BITMAPFILEHEADER bmfh;
//位图文件头
ZeroMemory(&bmfh,sizeof(BITMAPFILEHEADER));
*((char *)&bmfh.bfType) = 'B';
*(((char *)&bmfh.bfType) + 1) = 'M';
bmfh.bfType = 0x4d42; //bmp
bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfh.bfSize = bmfh.bfOffBits + (nWidth * nHeight) * iPixel / 8;
int iBMPBytes = nWidth * nHeight * iPixel / 8;
/* bmfh.bfSize = (nWidth*32+31)/8;
int iBMPBytes = bmfh.bfSize * nHeight;*/
////
CFile file;
if(file.Open(szFileName,CFile::modeWrite | CFile::modeCreate,NULL))
{
file.Write(&bmfh,sizeof(BITMAPFILEHEADER));
file.Write(&(lpbmih->bmiHeader),sizeof(BITMAPINFOHEADER));
file.Write(pBits,iBMPBytes);//在此处弹出对话框“Disk fullaccessing\NandFlash\Y.bmp",还望大家能够帮忙看看
file.Close();
}
////
DeleteObject(hBitMap);
DeleteObject(hDC);
pMemDC->DeleteDC();
delete pMemDC;
pMemDC = NULL;
delete pBitMap;
pBitMap = NULL;
delete lpbmih;
lpbmih = NULL;
}