📄 200533014228.htm
字号:
<HTML><HEAD><TITLE>用VC实现将自绘图形输出为bmp文件-http://www.koyee.com</TITLE>
<SCRIPT src="../../ips.asp"></SCRIPT>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=http://www.koyee.com content=no-cache>
<META content=可以网络在线,ASP技术,.NET技术,delphi技术,JSP技术,VB技术! name=KEYWORDS>
<STYLE type=text/css>
BODY {
BACKGROUND-IMAGE: url(images/webtop_bg3.gif); MARGIN: 0px
}
.style2 {
FONT-SIZE: 12px
}
BODY {
FONT-SIZE: 12px;
COLOR: #333333;
background-image: url();
}
TD {
FONT-SIZE: 12px; COLOR: #333333
}
TH {
FONT-SIZE: 12px; COLOR: #333333
}
A:link {
COLOR: #333333; LINE-HEIGHT: normal; FONT-STYLE: normal; FONT-FAMILY: "宋体"; TEXT-DECORATION: none
}
A:visited {
COLOR: #cc3399; FONT-STYLE: normal; FONT-FAMILY: "宋体"; TEXT-DECORATION: none
}
A:hover {
COLOR: #cc66ff; FONT-STYLE: normal; FONT-FAMILY: "宋体"; TEXT-DECORATION: none
}
A:active {
COLOR: #ff9999; FONT-STYLE: normal; FONT-FAMILY: "宋体"; TEXT-DECORATION: none
}
.style4 {color: #FFFFFF}
.style5 {color: #000000}
.style6 {color: #FF0000}
.style7 {
color: #FF0000;
font-size: 16px;
font-weight: bold;
}
</STYLE></head>
<script language="JavaScript">
var timerID = null;
var timerRunning = false;
function stopclock (){
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;}
function startclock () {
stopclock();
showtime();}
function showtime () {
var now = new Date();
var year = now.getYear();
var month = now.getMonth()+1;
var day = now.getDate();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds()
var timeValue =year +"年"
timeValue += month+"月"
timeValue += day+"日 "
timeValue += "" +((hours >= 12) ? "下午 " : "上午 " )
timeValue += ((hours >12) ? hours -12 :hours)
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += ((seconds < 10) ? ":0" : ":") + seconds
liveclock.innerHTML=timeValue
timerID = setTimeout("showtime()",1000);
timerRunning = true;}
</script>
<body onload="startclock()">
<TABLE height=60 cellSpacing=0 cellPadding=0 width="800"
align=center border=0>
<TBODY>
<TR>
<td background="../../images/webtop_bg.gif" width="332"><a target=blank href=http://wpa.qq.com/msgrd?V=1&Uin=61637663&Site=http://www.koyee.com&Menu=yes><img border="0" SRC=http://wpa.qq.com/pa?p=1:61637663:13 alt="有事就给我打电话,拼命的打"></a></td>
<td background="../../images/webtop_bg.gif" width="455"><script src="http://www.4.ads99.net/banner.php?userid=chdujian"></script></td>
</TR></TBODY></TABLE>
<TABLE height=25 cellSpacing=0 cellPadding=0 width="800"
align=center border=0>
<TBODY>
<TR>
<td background="../../images/22.gif"> <a href=../../index.htm>首页</a> | <a href=../../artical/69/1.htm>程序设计</a> | <a href=../../artical/77/1.htm>IT行情</a> | <a href=../../artical/78/1.htm>黑客攻防</a> | <a href=../../artical/79/1.htm>电脑硬件</a></td>
</TR></TBODY></TABLE>
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td scope="col"><img src="http://www.asp315.com/study/images/index_r1_c1.jpg" width="800" height="83"></td>
</tr>
</table>
<TABLE height=25 cellSpacing=0 cellPadding=0 width="800"
align=center border=0>
<TBODY>
<TR>
<td width="624" bgcolor="#CCCCCC">当前位置:<a href=../../index.htm>首页</a>--><a href=../../artical/69/1.htm>程序设计</a>--><a href=../../artical/81/1.htm>C/C++</a>-->显示文章内容</td><td width="176" bgcolor="#CCCCCC"><span id="liveclock"></span></td>
</TR></TBODY></TABLE>
<table width="800" height="406" border="1" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td width="166" height="200" valign="top" scope="col"><table width="100%" height="163" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="119" valign="top" scope="col"><table width="180" border="0" cellspacing="0" cellpadding="0" align="center">
<form action="../../search.asp" method="post" name="searchLeftForm">
<tr bgcolor="CAF6F4" align="center">
<td height="25" bgcolor="#CC9900"><font color="#cc0000"><b>□
站 内 搜 索 □</b></font></td>
</tr>
<tr align="center">
<td style="line-height:150%" height=60> 请输入查询的字符串:<br>
<input name="search" type="text" class="button1" id="search" style="width:150">
<select name="seaguan" size="1" class="button1" id="seaguan" style="width:150">
<option value="">==> 综合查询 <==</option>
<option value="title">标题</option>
<option value="江上飞鸟">作者</option>
<option value="artical">内容</option>
<%call getNewsBoardTree()%>
</select>
</td>
</tr>
<tr align="center">
<td>
<input type="SUBMIT" name="Action" value=" 查 询 " class="button2">
<input type="RESET" name="Clear" value=" 重 写 " class="button2">
</td>
</tr>
<tr align="center">
<td height="10"></td>
</tr>
</form>
</table></td>
</tr>
<tr>
<td height="20"><table width="181" height="20" cellpadding="0" cellspacing="0">
<tr><td width="181" height="20" align="center" bgcolor="#CC9900">分类最新文章</td>
</tr>
<tr><td><a href=200533014228.htm target=_blank>用VC实现将自绘图形输出为b</a></td></tr>
</table></td>
</tr>
<tr>
<td height="20"><table width="181" height="20" cellpadding="0" cellspacing="0">
<tr><td width="181" height="20" align="center" bgcolor="#CC9900">分类热门文章</td>
</tr>
<tr><td><a href=200533014228.htm target=_blank>用VC实现将自绘图形输出为b</a></td></tr>
</table></td>
</tr>
</table></td>
<td width="628" valign="top" scope="col"><table width="100%" border="0" align="center" cellspacing="0" cellpadding="5" bordercolordark="#FFFFFF" bordercolorlight="#000000" style="word-break:break-all;">
<tr>
<td width="100%">
</td>
</tr>
<tr>
<td width="100%">
<div align="center">
<p class="style7"><SPAN class=style1>用VC实现将自绘图形输出为bmp文件</SPAN></p>
<hr size="1" color="#0a778b" width="100%">
发布时间:2005-3-30 被阅览数:<SCRIPT src="../../counter.asp?id=472"></SCRIPT> 次 作者:江上飞鸟
</div>
</td>
</tr>
<tr>
<td width="100%"><p class="style6"><FONT size=3> 在用vc做程序时候,经常需要把输出的文本和图形保存到位图文件,当然可以有现成的控件来实现,但总不能如自己所愿,还是自己动手写吧!如下图: <BR><BR></FONT>
<TABLE width="90%" align=center border=0>
<TBODY>
<TR>
<TD>
<DIV align=center><IMG style="WIDTH: 488px; HEIGHT: 352px" height=331 onerror="this.src='/Article/UploadFiles/200502/20050209015520804.gif';" hspace=3 src="http://www.77169.org/Article/UploadFiles/200502/20050209015521441.gif" width=456 align=center vspace=1 border=1 orig_onmouseout="null" orig_onmouseover="null"></DIV></TD></TR></TBODY></TABLE><BR> 一、实现方法<BR><BR> 要把文本和图形保存到位图文件,只要对掌握位图结构有一定的了解,一切都ok呢。先必须要创建内存设备环境,然后内存设备环境创建的DIB区域,别忘了还要创建个CBitmap对象,CBitmap对象必须和DIB区域关联起来,把CBitmap对象选择到当前设备环境,然后在当前设备环境输出文本和图形就可以了。<BR><BR> 二、具体实现代码如下 <BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#dadacf border=1>
<TBODY>
<TR>
<TD>void CTestSaveBmpView::SaveAsBmp(CString filename)<BR>{<BR> //定义图形大小<BR> int iWidth = 800;<BR> int iHeight = 600;<BR> int iPixel = 16;<BR> //图形格式参数<BR> LPBITMAPINFO lpbmih = new BITMAPINFO;<BR> lpbmih->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);<BR> lpbmih->bmiHeader.biWidth = iWidth;<BR> lpbmih->bmiHeader.biHeight = iHeight;<BR> lpbmih->bmiHeader.biPlanes = 1;<BR> lpbmih->bmiHeader.biBitCount = iPixel;<BR> lpbmih->bmiHeader.biCompression = BI_RGB;<BR> lpbmih->bmiHeader.biSizeImage = 0;<BR> lpbmih->bmiHeader.biXPelsPerMeter = 0;<BR> lpbmih->bmiHeader.biYPelsPerMeter = 0;<BR> lpbmih->bmiHeader.biClrUsed = 0;<BR> lpbmih->bmiHeader.biClrImportant = 0;<BR><BR> //创建位图数据<BR> HDC hdc,hdcMem;<BR> HBITMAP hBitMap = NULL;<BR> CBitmap *pBitMap = NULL;<BR> CDC *pMemDC = NULL;<BR> BYTE *pBits;<BR><BR> hdc = CreateIC(TEXT("DISPLAY"),NULL,NULL,NULL);<BR> hdcMem = CreateCompatibleDC(hdc);<BR> hBitMap = CreateDIBSection(hdcMem,lpbmih,DIB_PAL_COLORS,(void **)&pBits,NULL,0);<BR> pBitMap = new CBitmap;<BR> pBitMap->Attach(hBitMap);<BR> pMemDC = new CDC;<BR> pMemDC->Attach(hdcMem);<BR> pMemDC->SelectObject(pBitMap);<BR> //<BR> CRect rc(0,0,iWidth,iHeight);<BR> pMemDC->SetBkMode(TRANSPARENT);<BR> //添加自绘图形 <BR> DrawCurve(pMemDC,rc);<BR> //保存到文件并创建位图结构<BR> BITMAPFILEHEADER bmfh;<BR> ZeroMemory(&bmfh,sizeof(BITMAPFILEHEADER));<BR> *((char *)&bmfh.bfType) = ''B'';<BR> *(((char *)&bmfh.bfType) + 1) = ''M'';<BR> bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);<BR> bmfh.bfSize = bmfh.bfOffBits + (iWidth * iHeight) * iPixel / 8;<BR><BR> TCHAR szBMPFileName[128];<BR> int iBMPBytes = iWidth * iHeight * iPixel / 8;<BR> strcpy(szBMPFileName,filename);<BR> CFile file;<BR> if(file.Open(szBMPFileName,Cfile::modeWrite | Cfile::modeCreate))<BR> {<BR> file.Write(&bmfh,sizeof(BITMAPFILEHEADER));<BR> file.Write(&(lpbmih->bmiHeader),sizeof(BITMAPINFOHEADER));<BR> file.Write(pBits,iBMPBytes);<BR> file.Close();<BR> }<BR><BR> pMemDC->DeleteDC();<BR> delete pMemDC; pMemDC = NULL;<BR> delete pBitMap; pBitMap = NULL;<BR> delete lpbmih; lpbmih = NULL;<BR>}<BR><BR>//输出文本和图形<BR><BR>void CTestSaveBmpView::DrawCurve(CDC *pDC, CRect rcClient)<BR>{<BR> //页面背景色<BR> CBrush brushCtl;<BR> brushCtl.CreateSolidBrush(RGB(255,255,255));<BR> pDC->Rectangle(rcClient);<BR> pDC->FillRect(rcClient,&brushCtl) ;<BR> brushCtl.DeleteObject();<BR> <BR> CPen pen;<BR> pen.CreatePen(PS_SOLID, 1, RGB(255,0,0));<BR> CPen *oldPen = pDC->SelectObject(&pen); <BR><BR> double xMin = 10.00f, xMax = 100.00f;<BR> double yMin = 10.00f, yMax = 200.00f;<BR><BR> double dbX1 = (xMax- xMin)/100 + xMin;<BR> double dbY1 = 600/dbX1 ;<BR> for (int i=1; i<100; i++) //曲线<BR> {<BR> double dbX2 = (xMax- xMin)*i/100 + xMin;<BR> double dbY2 = 600/dbX2 ;<BR><BR> pDC->MoveTo(int(rcClient.left+(dbX1 - xMin)*rcClient.Width()/(xMax- xMin)), <BR> int(rcClient.bottom-(dbY1- yMin)*rcClient.Height()/(yMax- yMin)));<BR> pDC->LineTo(int(rcClient.left+(dbX2 - xMin)*rcClient.Width()/(xMax- xMin)), <BR> int(rcClient.bottom-(dbY2- yMin)*rcClient.Height()/(yMax- yMin))); <BR><BR> dbX1=dbX2;<BR> dbY1=dbY2;<BR> }<BR> pDC->SelectObject(oldPen); <BR> pen.DeleteObject();<BR> oldPen = NULL;<BR>}<BR><BR>//保存的实现<BR>void CTestSaveBmpView::OnRButtonDown(UINT nFlags, CPoint point) <BR>{<BR><BR> CFileDialog dlg(false,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "位图文件(*.bmp)|*.bmp|",NULL);<BR> if (dlg.DoModal()!= IDOK) return;<BR> CString filename = dlg.GetFileName() + ".bmp";<BR> SaveAsBmp(filename); <BR><BR> CView::OnRButtonDown(nFlags, point);<BR>}</TD></TR></TBODY></TABLE></p>
<p><img src="http://www.koyee.com/images/dgg.gif" height="70" width="587"> </p></td>
</tr>
<tr>
<td width="628" bordercolor="0" class="font1"><p>
<hr size="1" color="#0a778b" width="100%">
<table><tr><td width="269" align="center">上一篇: <a href=../69/20053301399.htm title=C#学习第二天>C#学习第二天</a> </td>
<td width="285" align="center">下一篇: <a href=../90/200542823048.htm title=gew>gew</a></td>
</tr></table>
<hr size="1" color="#0a778b" width="100%">
<div align="right"> <a href="javascript:window.print()"><img src="../../images/printer.gif" width="16" height="14" border="0" align="absmiddle">打印本页</a> | <a href="javascript:window.close()"><img src="../../images/close.gif" width="14" height="14" border="0" align="absmiddle">关闭窗口</a> </div>
<p></p></td>
</tr>
</table></td>
</tr>
</table>
<TABLE height=62 cellSpacing=0 cellPadding=0 width="800"
align=center background=../../images/webtop_bg.gif
border=0>
<TBODY>
<TR>
<TD><div align="center" class="style4">Copyright @ 2004-2008 http://www.koyee.com 可以网络在线版权所有<br>
公司地址:江西南昌 电话:13879173467 邮编:330029<br>
email:chenxueyan9999@163.com</div></TD>
</TR></TBODY></TABLE>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -