程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> VC >> 關於VC++ >> VC實現自繪圖形輸出到bmp文件

VC實現自繪圖形輸出到bmp文件

編輯:關於VC++

在用vc做程序時候,經常需要把輸出的文本和圖形保存到位圖文件,當然可以 有現成的控件來實現,但總不能如自己所願,還是自己動手寫吧!如下圖:

一、實現方法

要把文本和圖形保存到位圖文件,只要對掌握位圖結構有一定的了解,一切都 ok呢。先必須要創建內存設備環境,然後內存設備環境創建的DIB區域,別忘了還 要創建個CBitmap對象,CBitmap對象必須和DIB區域關聯起來,把CBitmap對象選 擇到當前設備環境,然後在當前設備環境輸出文本和圖形就可以了。

二、具體實現代碼如下

void CTestSaveBmpView::SaveAsBmp(CString filename)
{
  //定義圖形大小
  int iWidth = 800;
  int iHeight = 600;
  int iPixel = 16;
  //圖形格式參數
  LPBITMAPINFO lpbmih = new BITMAPINFO;
  lpbmih->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  lpbmih->bmiHeader.biWidth = iWidth;
  lpbmih->bmiHeader.biHeight = iHeight;
  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 hdc,hdcMem;
  HBITMAP hBitMap = NULL;
  CBitmap *pBitMap = NULL;
  CDC *pMemDC = NULL;
  BYTE *pBits;

  hdc = CreateIC(TEXT("DISPLAY"),NULL,NULL,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);
  pMemDC->SelectObject(pBitMap);
  //
  CRect rc(0,0,iWidth,iHeight);
  pMemDC->SetBkMode(TRANSPARENT);
  //添加自繪圖形
  DrawCurve(pMemDC,rc);
  //保存到文件並創建位圖結構
  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 + (iWidth * iHeight) * iPixel / 8;

  TCHAR szBMPFileName[128];
  int iBMPBytes = iWidth * iHeight * iPixel / 8;
  strcpy(szBMPFileName,filename);
  CFile file;
  if(file.Open(szBMPFileName,CFile::modeWrite | CFile::modeCreate))
  {
    file.Write(&bmfh,sizeof(BITMAPFILEHEADER));
    file.Write(&(lpbmih->bmiHeader),sizeof (BITMAPINFOHEADER));
    file.Write(pBits,iBMPBytes);
    file.Close();
  }

  pMemDC->DeleteDC();
  delete pMemDC; pMemDC = NULL;
  delete pBitMap; pBitMap = NULL;
  delete lpbmih; lpbmih = NULL;
}
//輸出文本和圖形
void CTestSaveBmpView::DrawCurve(CDC *pDC, CRect rcClient)
{
  //頁面背景色
  CBrush brushCtl;
  brushCtl.CreateSolidBrush(RGB(255,255,255));
  pDC->Rectangle(rcClient);
  pDC->FillRect(rcClient,&brushCtl) ;
  brushCtl.DeleteObject();

  CPen pen;
  pen.CreatePen(PS_SOLID, 1, RGB(255,0,0));
  CPen *oldPen = pDC->SelectObject(&pen);

  double xMin = 10.00f, xMax = 100.00f;
  double yMin = 10.00f, yMax = 200.00f;

  double dbX1 = (xMax- xMin)/100 + xMin;
  double dbY1 = 600/dbX1 ;
  for (int i=1; i<100; i++) //曲線
  {
    double dbX2 = (xMax- xMin)*i/100 + xMin;
    double dbY2 = 600/dbX2 ;

    pDC->MoveTo(int(rcClient.left+(dbX1 - xMin)*rcClient.Width ()/(xMax- xMin)),
      int(rcClient.bottom-(dbY1- yMin)*rcClient.Height()/(yMax- yMin)));
    pDC->LineTo(int(rcClient.left+(dbX2 - xMin)*rcClient.Width ()/(xMax- xMin)),
      int(rcClient.bottom-(dbY2- yMin)*rcClient.Height()/(yMax- yMin)));

    dbX1=dbX2;
    dbY1=dbY2;
  }
  pDC->SelectObject(oldPen);
  pen.DeleteObject();
  oldPen = NULL;
}
//保存的實現
void CTestSaveBmpView::OnRButtonDown(UINT nFlags, CPoint point)
{

  CFileDialog dlg(false,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
    "位圖文件(*.bmp)|*.bmp|",
    NULL);
  if (dlg.DoModal()!= IDOK) return;
  CString filename = dlg.GetFileName() + ".bmp";
  SaveAsBmp(filename);

  CView::OnRButtonDown(nFlags, point);
}

本文配套源碼

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved