需要MFC库,具体算法看代码
1 CDC* pDC = GetDC(); 2 CDC* MemDC = new CDC(); 3 CBitmap* mBitmap = new CBitmap(); 4 CRect rect; 5 GetClientRect(rect); 6 MemDC->CreateCompatibleDC(NULL); 7 mBitmap->CreateCompatibleBitmap(pDC,rect.Width(),rect.Height()); 8 MemDC->SelectObject(mBitmap); 9 /**//// 拷贝图像到 mBitmap 上 10 MemDC->StretchBlt(0, 0, rect.Width(),rect.Height(), pDC, 11 rect.left, rect.top,rect.Width(),rect.Height(), SRCCOPY); 12 13 14 /**//// 保存mBitmap 头信息到 bi上 15 HDC hDC; 16 HPALETTE hPal; 17 ASSERT( mBitmap->GetSafeHandle()); 18 BITMAP* bm = new BITMAP; 19 mBitmap->GetBitmap(bm); 20 int bitCount = 24; 21 22 if(bm->bmBitsPixel <=1) 23 bitCount = 1; 24 else if(bm->bmBitsPixel <=4) 25 bitCount = 4; 26 else if(bm->bmBitsPixel <=8) 27 bitCount = 8; 28 else 29 bitCount = 24; 30 int cNumber = 0; 31 if(bitCount <= 8) 32 cNumber = 1<<bitCount; 33 34 //设置BMP位图结构 35 36 BITMAPINFOHEADER bi; 37 bi.biWidth = bm->bmWidth; 38 bi.biHeight = bm->bmHeight; 39 bi.biPlanes = 1; 40 bi.biBitCount = bitCount; 41 bi.biSize = 40; 42 bi.biCompression = BI_RGB ; 43 bi.biSizeImage = 0; 44 bi.biXPelsPerMeter = 0; 45 bi.biYPelsPerMeter = 0; 46 bi.biClrUsed = 0 ; 47 bi.biClrImportant = 0; 48 49 int dwsize = bi.biSize + cNumber *sizeof(RGBQUAD); 50 51 LPBITMAPINFO pbi = (LPBITMAPINFO) new byte[dwsize]; 52 ::memcpy(pbi,&bi,sizeof(BITMAPINFOHEADER)); 53 54 hPal = (HPALETTE)::GetStockObject(DEFAULT_PALETTE); 55 if (hPal) 56 { 57 hDC = MemDC->m_hDC; 58 HPALETTE hOldPal = ::SelectPalette(hDC, (HPALETTE)hPal, FALSE); 59 ::RealizePalette(hDC); 60 } 61 //获取数据大小 并修改信息头中数据大小 62 ::GetDIBits(hDC, (HBITMAP)(mBitmap->GetSafeHandle()), 0l, (DWORD)bi.biHeight, 63 (LPBYTE)NULL, (LPBITMAPINFO)pbi, (DWORD)DIB_RGB_COLORS); 64 memcpy(&bi,pbi,sizeof(BITMAPINFOHEADER)); 65 if (bi.biSizeImage == 0) 66 { 67 bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8) * bi.biHeight; 68 } 69 dwsize += bi.biSizeImage; 70 LPBYTE pdata = new byte[dwsize]; 71 memcpy(pdata,pbi,bi.biSize + cNumber *sizeof(RGBQUAD)); 72 //获取数据 73 if(0 == ::GetDIBits(hDC, (HBITMAP)(mBitmap->GetSafeHandle()), 0l, (DWORD)bi.biHeight, 74 (LPBYTE)(pdata+(bi.biSize + cNumber *sizeof(RGBQUAD))), (LPBITMAPINFO)pbi, (DWORD)DIB_RGB_COLORS)) 75 { 76 MessageBox("数据读取失败!"); 77 return; 78 } 79 //文件头 80 BITMAPFILEHEADER bf; 81 bf.bfType = 'M'<<8|'B'; 82 bf.bfSize = 14 +dwsize; 83 bf.bfOffBits = 54 + cNumber *sizeof(RGBQUAD); 84 85 //写入文件 86 FILE* fp = NULL; 87 CTime nowtime = CTime::GetCurrentTime(); 88 time_t nt = nowtime.GetTime(); 89 CString str; 90 str.Format("images\\%ld.bmp",nt); 91 fp = fopen((LPCTSTR)(str),"wb"); 92 if(fp == NULL) return; 93 fwrite(&bf,sizeof(BITMAPFILEHEADER),1,fp); 94 fwrite(pdata,1,dwsize,fp); 95 fclose(fp); 96 97 //清理垃圾 98 MemDC->Detach(); 99 delete MemDC; 100 delete mBitmap; 101 delete bm; 102 delete pbi; 103 delete[] pdata;
|
|
|
CALENDER
| | 日 | 一 | 二 | 三 | 四 | 五 | 六 |
|---|
| 28 | 29 | 30 | 31 | 1 | 2 | 3 | | 4 | 5 | 6 | 7 | 8 | 9 | 10 | | 11 | 12 | 13 | 14 | 15 | 16 | 17 | | 18 | 19 | 20 | 21 | 22 | 23 | 24 | | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|
常用链接
留言簿(2)
随笔分类
随笔档案
文章档案
A.I.
Libs
Net Resources
VR & 3DGame
搜索
最新评论

阅读排行榜
评论排行榜
Powered By: 博客园 模板提供:沪江博客
|