CreatBMPView.cpp
资源名称:CreatBMP.rar [点击查看]
上传用户:hfwmdy
上传日期:2016-01-14
资源大小:83k
文件大小:9k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- // CreatBMPView.cpp : implementation of the CCreatBMPView class
- //
- #include "stdafx.h"
- #include "CreatBMP.h"
- #include "CreatBMPDoc.h"
- #include "CreatBMPView.h"
- /*
- #include ".atldebugapi.h"
- #include ".atltrace.h"
- #include ".atlexcept.h"
- #include ".atlchecked.h"
- #include ".atlcore.h"
- #include ".atlsimpstr.h"
- #include ".cstringt.h"
- #include ".atlstr.h"
- #include ".atlimage.h"
- */
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CCreatBMPView
- IMPLEMENT_DYNCREATE(CCreatBMPView, CView)
- BEGIN_MESSAGE_MAP(CCreatBMPView, CView)
- //{{AFX_MSG_MAP(CCreatBMPView)
- ON_COMMAND(ID_CREATE_BMP, OnCreateBmp)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CCreatBMPView construction/destruction
- CCreatBMPView::CCreatBMPView()
- {
- // TODO: add construction code here
- }
- CCreatBMPView::~CCreatBMPView()
- {
- }
- BOOL CCreatBMPView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CCreatBMPView drawing
- void CCreatBMPView::OnDraw(CDC* pDC)
- {
- CCreatBMPDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- }
- /////////////////////////////////////////////////////////////////////////////
- // CCreatBMPView printing
- BOOL CCreatBMPView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CCreatBMPView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CCreatBMPView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CCreatBMPView diagnostics
- #ifdef _DEBUG
- void CCreatBMPView::AssertValid() const
- {
- CView::AssertValid();
- }
- void CCreatBMPView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CCreatBMPDoc* CCreatBMPView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCreatBMPDoc)));
- return (CCreatBMPDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CCreatBMPView message handlers
- void CCreatBMPView::OnCreateBmp()
- {
- // TODO: Add your command handler code here
- /* CClientDC dc(this);
- dc.TextOut(100,100,"thsi is a test");
- CDC memDC;
- CRect rect;
- GetClientRect(rect);
- CBitmap m_bitmap;
- memDC.CreateCompatibleDC(&dc);
- int Width = rect.Width();
- int Height = rect.Height();
- int iBits; //当前显示分辨率下每个像素所占字节数
- WORD wBitCount; ////位图中每个像素所占字节数
- DWORD dwPaletteSize=0, dwBmBitsSize;//定义调色板大小,位图中像素字节大小
- // if(m_OpenState != 1)
- // {
- m_bitmap.CreateCompatibleBitmap(&dc, Width, Height);
- // }
- CBitmap* pOld = memDC.SelectObject(&m_bitmap);
- memDC.BitBlt(0, 0, Width, Height, &dc, 0, 0, SRCCOPY);
- memDC.SelectObject(pOld);
- BITMAP btm;
- m_bitmap.GetBitmap(&btm);
- DWORD dwBytesSize = btm.bmWidthBytes * btm.bmHeight;
- iBits = GetDeviceCaps(memDC, BITSPIXEL);
- if (iBits <= 1)
- wBitCount = 1;
- else if (iBits <= 4)
- wBitCount = 4;
- else if (iBits <= 8)
- wBitCount = 8;
- else if (iBits <= 24)
- wBitCount = 24;
- //计算调色板大小
- if (wBitCount <= 8)
- dwPaletteSize = (1 << wBitCount) *sizeof(RGBQUAD);
- /////////////////////////////////////////////
- BITMAPFILEHEADER bfh;
- BITMAPINFOHEADER bih;
- HANDLE hPal,hOldPal=NULL;
- bih.biBitCount = wBitCount;//btm.bmBitsPixel;
- bih.biClrImportant = 0;
- bih.biClrUsed = 0;
- bih.biCompression = BI_RGB;
- bih.biHeight = btm.bmHeight;
- bih.biPlanes = 1;
- bih.biSize = sizeof(BITMAPINFOHEADER);
- bih.biSizeImage = dwBytesSize ;
- bih.biWidth = btm.bmWidth;
- bih.biXPelsPerMeter = 0;
- bih.biYPelsPerMeter = 0;
- bfh.bfReserved1 = bfh.bfReserved2 = 0;
- bfh.bfType = 0x4D42; // bm
- dwBmBitsSize = ((btm.bmWidth*wBitCount+31)/32)*4*btm.bmHeight;
- LPSTR lpData = (LPSTR)GlobalAlloc(GPTR, dwBmBitsSize);
- //// 处理调色板
- hPal = GetStockObject(DEFAULT_PALETTE);
- if (hPal)
- {
- dc.m_hDC = ::GetDC(NULL);
- hOldPal = ::SelectPalette(dc, (HPALETTE)hPal, FALSE);
- RealizePalette(dc);
- }
- //// 获取该调色板下新的像素值
- GetDIBits(dc,m_bitmap,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);
- ////恢复调色板
- if (hOldPal)
- {
- ::SelectPalette(dc, (HPALETTE)hOldPal, TRUE);
- RealizePalette(dc);
- ::ReleaseDC(NULL, dc);
- }
- bfh.bfSize =sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmBitsSize;
- bfh.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize;
- CFileDialog dlg(FALSE,NULL,NULL,NULL,_T( "位图文件(*.bmp)|*.bmp| "));
- if (dlg.DoModal()!=IDOK)
- return;
- CFile bf;
- CString szSaveFileName=dlg.GetPathName() + ".bmp ";
- if(bf.Open(szSaveFileName, CFile::modeCreate | CFile::modeWrite))
- {
- bf.WriteHuge(&bfh, sizeof(BITMAPFILEHEADER));
- bf.WriteHuge(&bih, sizeof(BITMAPINFOHEADER));
- bf.WriteHuge(lpData,dwBmBitsSize);
- bf.Close();
- AfxMessageBox( "保存成功 ");
- }
- GlobalFree(lpData);
- memDC.DeleteDC();
- */
- HDC hActiveDC=::GetDC(this-> m_hWnd);
- // SetMapMode(hActiveDC,MM_LOMETRIC);
- BITMAPINFO bi;//信息头
- void *pBits=NULL;
- // CRect rect;//客户区窗口
- // GetClientRect(&rect);
- // nWidth=rect.right;
- // nHeight=rect.bottom;
- // int a4_w = 1000;//0.1mm
- // int a4_h = 2000;
- int a4_w = 2050-2*191;//0.1mm
- int a4_h = 2970-2*191;
- // CPoint p = CPoint(a4_w,-a4_h);
- CPoint p = CPoint(a4_h,-a4_w);
- SetMapMode(hActiveDC,MM_LOMETRIC);
- LPtoDP(hActiveDC,&p,1);
- SetMapMode(hActiveDC,MM_TEXT);
- DPtoLP(hActiveDC,&p,1);
- int nWidth = p.x;
- int nHeight = p.y;
- // int nWidth=800;
- // int nHeight=600;
- //拷贝客户区至内存DC
- //HDC hActiveDC=::GetDC(GetSafeHwnd());//参数
- HDC hActiveWndCompatibleDC=CreateCompatibleDC(hActiveDC);
- HBITMAP hActiveWndCompactibleBitmap=CreateCompatibleBitmap(hActiveDC,nWidth,nHeight);
- SelectObject(hActiveWndCompatibleDC,hActiveWndCompactibleBitmap);
- //把hActiveDC句柄所指定的当前客户区图复制到hActiveWndCompatibleDC中
- BitBlt(hActiveWndCompatibleDC,0,0,nWidth,nHeight,hActiveDC,0,0,SRCCOPY);
- SetMapMode(hActiveWndCompatibleDC,MM_LOMETRIC);
- TextOut(hActiveWndCompatibleDC,100,-100,"this is a test",14);
- TextOut(hActiveWndCompatibleDC,200,-200,"this is a test",14);
- SetMapMode(hActiveWndCompatibleDC,MM_TEXT);
- //保存内存DC
- HDC hBmpFileDC=CreateCompatibleDC(hActiveWndCompatibleDC);
- //填充信息头
- ZeroMemory(&bi,sizeof(bi));
- bi.bmiHeader.biSize=sizeof(bi.bmiHeader);
- bi.bmiHeader.biHeight=nHeight;
- bi.bmiHeader.biWidth=nWidth;
- bi.bmiHeader.biPlanes=1;
- bi.bmiHeader.biBitCount=24;
- bi.bmiHeader.biCompression=BI_RGB;
- bi.bmiHeader.biSizeImage=3*nWidth*nHeight;
- //创建一个位图可用作直接进行写操作
- // HBITMAP hBmpFileBitmap=CreateDIBSection(hActiveWndCompatibleDC,&bi,DIB_RGB_COLORS,&pBits,NULL,0);
- HBITMAP hBmpFileBitmap=CreateDIBSection(hActiveWndCompatibleDC,&bi,DIB_PAL_COLORS,&pBits,NULL,0);
- SelectObject(hBmpFileDC,hBmpFileBitmap);
- BitBlt(hBmpFileDC,0,0,nWidth,nHeight,hActiveWndCompatibleDC,0,0,SRCCOPY);
- CString tempFileName;//目标文件名
- tempFileName = "D:\MAP.bmp ";
- HANDLE hFile=CreateFile(tempFileName,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
- if(hFile!=INVALID_HANDLE_VALUE)
- {
- DWORD dwRet=0;
- //填充文件头
- BITMAPFILEHEADER bmfHeader;
- ZeroMemory(&bmfHeader,sizeof(bmfHeader));
- bmfHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
- bmfHeader.bfSize=bi.bmiHeader.biSizeImage+bmfHeader.bfOffBits;
- bmfHeader.bfType= 'MB';
- WriteFile(hFile,&bmfHeader,sizeof(bmfHeader),&dwRet,NULL);
- WriteFile(hFile,&bi.bmiHeader,sizeof(bi.bmiHeader),&dwRet,NULL);
- WriteFile(hFile,pBits,bi.bmiHeader.biSizeImage,&dwRet,NULL);
- CloseHandle(hFile);
- }
- }