您的位置:首页 > 其它

VC将自定义画图区域保存为BMP文件

2007-08-05 23:56 351 查看
//引用函数部分

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

//保存图形的函数

void CGraDlg::SaveAsBmp(CString filename)
{
//定义图形大小
int iWidth = 620;
int iHeight = 450;
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);
//Draw2D();

//保存到文件并创建位图结构
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(filename,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 CGraDlg::DrawCurve(CDC *pDC, CRect rcClient)
{
//页面背景色
CBrush brushCtl;
brushCtl.CreateSolidBrush(RGB(255,255,205));
pDC->Rectangle(rcClient);
pDC->FillRect(rcClient,&brushCtl) ;
brushCtl.DeleteObject();

CPen cpen,*pPen;
cpen.CreatePen(PS_SOLID,2,RGB(0,0,255));
pPen=pDC->SelectObject(&cpen);

const int nOrgX=50;//原点
const int nOrgY=380;

const int nWidth=500;//坐标轴
const int nHeight=320;

int nPI=60;

//指定原点
pDC->SetViewportOrg(nOrgX,nOrgY);
pDC->SetTextColor(RGB(255,0,0));

//画原点处的突出部分
pDC->MoveTo(-8,0);
pDC->LineTo(0,0);
pDC->MoveTo(0,8);
pDC->LineTo(0,0);

//绘制横坐标
int n=0,nTmp=0;
CString sPIText[30];

CString cTmp;

for(int i=0;i<30;i++)
{
cTmp.Format(_T("%d"),i);
sPIText[i] = cTmp;
}

while(true)
{
pDC->LineTo(nPI*n,0);//画每一段

if(n)
pDC->LineTo(nPI*n,-5);//画刻度

pDC->MoveTo(nPI*n,0);

pDC->SetBkMode(TRANSPARENT);

pDC->TextOut(nPI*n-sPIText
.GetLength()*3,10,sPIText
);

n++;
nTmp=nTmp+nPI;
if(nTmp>=nWidth)
{
pDC->LineTo(nPI*n,0);
break;
}
}

//画箭头
pDC->LineTo(nPI*n-5,5);//画每一段
pDC->MoveTo(nPI*n,0);
pDC->LineTo(nPI*n-5,-5);//画每一段

for(int i=0;i<30;i++)
{
cTmp.Format(_T("%d"),i*2);
sPIText[i] = cTmp;
}

pDC->MoveTo(0,0);
n=0;

int k=0;
nPI=50;

//绘制纵坐标
while(true)
{
pDC->LineTo(0,nPI*n);
pDC->LineTo(5,nPI*n);

pDC->MoveTo(0,nPI*n);

pDC->SetBkMode(TRANSPARENT);

pDC->TextOut(-20,nPI*n-sPIText[k].GetLength()*3,sPIText[k]);

k++;
n--;

if(-nPI*n>nHeight)
{
pDC->LineTo(0,nPI*n);
break;
}
}

//画箭头
pDC->LineTo(5,nPI*n+5);//画每一段
pDC->MoveTo(0,nPI*n);
pDC->LineTo(-5,nPI*n+5);//画每一段

//画出图形
double rate,p;

for(p=min_p;p<max_p*60;p=p+1)
{
rate=0.8*pow(p,0.8);

pDC->MoveTo(int(p),-rate);
pDC->LineTo(int(p),-rate+1);
}

cpen.DeleteObject();
}

//绘制图形的函数

void CGraDlg::DrawCurve(CDC *pDC, CRect rcClient)
{
//页面背景色
CBrush brushCtl;
brushCtl.CreateSolidBrush(RGB(255,255,205));
pDC->Rectangle(rcClient);
pDC->FillRect(rcClient,&brushCtl) ;
brushCtl.DeleteObject();

CPen cpen,*pPen;
cpen.CreatePen(PS_SOLID,2,RGB(0,0,255));
pPen=pDC->SelectObject(&cpen);

const int nOrgX=50;//原点
const int nOrgY=380;

const int nWidth=500;//坐标轴
const int nHeight=320;

int nPI=60;

//指定原点
pDC->SetViewportOrg(nOrgX,nOrgY);
pDC->SetTextColor(RGB(255,0,0));

//画原点处的突出部分
pDC->MoveTo(-8,0);
pDC->LineTo(0,0);
pDC->MoveTo(0,8);
pDC->LineTo(0,0);

//绘制横坐标
int n=0,nTmp=0;
CString sPIText[30];

CString cTmp;

for(int i=0;i<30;i++)
{
cTmp.Format(_T("%d"),i);
sPIText[i] = cTmp;
}

while(true)
{
pDC->LineTo(nPI*n,0);//画每一段

if(n)
pDC->LineTo(nPI*n,-5);//画刻度

pDC->MoveTo(nPI*n,0);

pDC->SetBkMode(TRANSPARENT);

pDC->TextOut(nPI*n-sPIText
.GetLength()*3,10,sPIText
);

n++;
nTmp=nTmp+nPI;
if(nTmp>=nWidth)
{
pDC->LineTo(nPI*n,0);
break;
}
}

//画箭头
pDC->LineTo(nPI*n-5,5);//画每一段
pDC->MoveTo(nPI*n,0);
pDC->LineTo(nPI*n-5,-5);//画每一段

for(int i=0;i<30;i++)
{
cTmp.Format(_T("%d"),i*2);
sPIText[i] = cTmp;
}

pDC->MoveTo(0,0);
n=0;

int k=0;
nPI=50;

//绘制纵坐标
while(true)
{
pDC->LineTo(0,nPI*n);
pDC->LineTo(5,nPI*n);

pDC->MoveTo(0,nPI*n);

pDC->SetBkMode(TRANSPARENT);

pDC->TextOut(-20,nPI*n-sPIText[k].GetLength()*3,sPIText[k]);

k++;
n--;

if(-nPI*n>nHeight)
{
pDC->LineTo(0,nPI*n);
break;
}
}

//画箭头
pDC->LineTo(5,nPI*n+5);//画每一段
pDC->MoveTo(0,nPI*n);
pDC->LineTo(-5,nPI*n+5);//画每一段

//画出图形
double rate,p;

for(p=min_p;p<max_p*60;p=p+1)
{
rate=0.8*pow(p,0.8);

pDC->MoveTo(int(p),-rate);
pDC->LineTo(int(p),-rate+1);
}

cpen.DeleteObject();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: