|
给你个输出BMP的代码吧,至于JPG等其他类型,需要相应的编码器,看机器是否支持了.
void 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;
hdcMem = CreateCompatibleDC(hDC);
hBitMap = CreateDIBSection(hdcMem,lpbmih,DIB_RGB_COLORS,(void **)&pBits,NULL,0);
pBitMap = new CBitmap;
pBitMap->Attach(hBitMap);
pMemDC = new CDC;
pMemDC->Attach(hdcMem);
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.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfh.bfSize = bmfh.bfOffBits + (nWidth * nHeight) * iPixel / 8;
int iBMPBytes = nWidth * nHeight * iPixel / 8;
CFile file;
if(file.Open(szFileName,CFile::modeCreate|CFile::modeWrite )>0)
{
file.Write(&bmfh,sizeof(BITMAPFILEHEADER));
file.Write(&(lpbmih->bmiHeader),sizeof(BITMAPINFOHEADER));
file.Write(pBits,iBMPBytes); file.Close();
}
DeleteObject(hBitMap);
DeleteObject(hDC);
pMemDC->DeleteDC();
delete pMemDC;
pMemDC = NULL;
delete pBitMap;
pBitMap = NULL;
delete lpbmih;
lpbmih = NULL;
} |
|