这段内容是添加到单文档的应用程序的 视图类的OnDraw方法中的,将设备上下文中的内容保存成位图文件。
pDC->TextOut(100, 30,"弃我去者,昨日之日不可留;乱我心者,");
pDC->TextOut(60, 50,"今日之日多烦扰。长空万里送秋雁,对此可以");
pDC->TextOut(60, 70,"酣高楼。蓬莱文章建安骨,中间小谢又青发。");
pDC->TextOut(60, 90,"具怀逸兴壮思飞,欲上青天揽明月。抽刀断水");
pDC->TextOut(60,110,"水更流,举杯消愁愁更愁。人生在世不称意,");
pDC->TextOut(60,130,"明朝散发弄扁舟。");
CBitmap bmp;
bmp.CreateCompatibleBitmap(pDC,400,200);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
memDC.SelectObject(&bmp);
memDC.BitBlt(0,0,400,200,pDC,0,0,SRCCOPY);
BITMAP bInfo;
bmp.GetBitmap(&bInfo);
int szPanel = 0;
if (bInfo.bmBitsPixel<16)
szPanel = pow(2,bInfo.bmBitsPixel)*sizeof(RGBQUAD);
BITMAPINFO* pBitInfo = (BITMAPINFO*) LocalAlloc(LPTR,sizeof(BITMAPINFO)+szPanel);
pBitInfo->bmiHeader.biBitCount = bInfo.bmBitsPixel;
pBitInfo->bmiHeader.biClrImportant = 0;
pBitInfo->bmiHeader.biCompression = 0;
pBitInfo->bmiHeader.biHeight = bInfo.bmHeight;
pBitInfo->bmiHeader.biPlanes = bInfo.bmPlanes;
pBitInfo->bmiHeader.biSize = sizeof(BITMAPINFO);
pBitInfo->bmiHeader.biSizeImage = bInfo.bmWidthBytes* bInfo.bmHeight;
pBitInfo->bmiHeader.biWidth = bInfo.bmWidth;
pBitInfo->bmiHeader.biXPelsPerMeter = 0;
pBitInfo->bmiHeader.biYPelsPerMeter = 0;
char* pData = new char[bInfo.bmWidthBytes*bInfo.bmHeight];
GetDIBits(memDC.m_hDC,bmp,0,bInfo.bmHeight,pData,pBitInfo,DIB_RGB_COLORS);
BITMAPFILEHEADER FileHeader;
FileHeader.bfType = 0x4d42; //BM
FileHeader.bfReserved1 = 0;
FileHeader.bfReserved2 = 0;
FileHeader.bfSize = sizeof(BITMAPFILEHEADER);
FileHeader.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFO)+szPanel;
CFile file;
file.Open("E:\\bmp.bmp",CFile::modeCreate|CFile::modeReadWrite);
file.WriteHuge(&FileHeader,sizeof(BITMAPFILEHEADER));
file.WriteHuge(pBitInfo,sizeof(BITMAPINFO));
file.WriteHuge(pData,bInfo.bmWidthBytes*bInfo.bmHeight);
file.Close();
LocalFree(pBitInfo);
delete pData;
阅读(2095) | 评论(0) | 转发(0) |