📄 利用gdi+的双缓冲技术来提高绘图效率.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0049)http://tech.bc0436.com/dev/msdn/113/2112113.shtml -->
<HTML lang=zh-CN xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>利用GDI+的双缓冲技术来提高绘图效率</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=Content-Language content=zh-CN>
<META content=all name=robots>
<META content="index, follow" name=googlebot>
<META content="MSHTML 6.00.2900.2180" name=GENERATOR>
<META content="天极Yesky | 全球中文IT第一门户" name=author>
<META content="天极Yesky | 全球中文IT第一门户" name=Copyright>
<META content=硬件,软件,数码,游戏,e时代,下载,社区,评测,笔记本; name=keywords><LINK media=all
href="利用GDI+的双缓冲技术来提高绘图效率.files/yesky-content-cs.css" type=text/css
rel=stylesheet></HEAD>
<BODY>
<SCRIPT language=JavaScript>
function sendemail(){
OpenWindow = window.open('http://other.chinabyte.com/chinabyte/qin/sendemail-new.shtm?url=/dev//msdn/113/2112113.shtml&title=利用GDI+的双缓冲技术来提高绘图效率&channel=324','emailwin','toolbar=no,location=no,scrollbars=no,menubar=no,width=462,height=450')
}</SCRIPT>
<DIV class=width>
<DIV class=minwidth>
<DIV class=container><!--头部-->
<DIV class=hidden>yesky内容</DIV>
<SCRIPT language=JavaScript src="" type=text/JavaScript></SCRIPT>
<SPAN id=ad1 style="DISPLAY: none"></SPAN>
<DIV align=center>
<SCRIPT>document.write(navigator.userAgent.indexOf('MSIE')==-1?"<SCRIPT src='http://219.239.88.50:80/adsunion/get/;pl=pl-1-survey2005-column-051118;tp=js;sk=0;ck=0;/?'><\/SCRIPT>":"<IFRAME MARGINHEIGHT=0 MARGINWIDTH=0 FRAMEBORDER=0 BORDER=0 VSPACE=0 WIDTH=760 NORESIZE HEIGHT=90 SCROLLING=NO SRC='http://219.239.88.50:80/adsunion/get/;pl=pl-1-survey2005-column-051118;tp=if;sk=0;ck=0;/?'></IFRAME>");</SCRIPT>
</DIV>
<DIV class=clear1></DIV>
<DIV class=newbox><SPAN id=ad8></SPAN></DIV><!--导航条-->
<DIV class=newbox>
<DIV class=newbgpic>
<DIV class=left>您现在的位置:<A href="http://tech.bc0436.com/"> Yesky</A>><A
href="http://tech.bc0436.com/soft"> 软件</A>><A
href="http://tech.bc0436.com/dev"> 开发者网络</A>><A
href="http://tech.bc0436.com/dev/msdn"> 微软开发专栏</A> </DIV>
<DIV class=right>
<DIV class=space1><SPAN id=ad3></SPAN></DIV></DIV></DIV></DIV>
<DIV class=clear1></DIV><!--结束-->
<DIV class=clear1></DIV><!--文章内容区-->
<DIV class=newbox>
<DIV class=newleft>
<DIV class=newleft1>
<DIV class=newsmallbox>利用GDI+的双缓冲技术来提高绘图效率 </DIV>
<DIV class=clear1></DIV>
<DIV class=newsmall1box>
<DIV class=newtitle1>作者: </DIV>
<DIV class=newtitle2>出处: BLOG </DIV>
<DIV class=newtitle3>责任编辑: <A title=向本编辑提问
href="http://comments.yesky.com/t/·½ÖÛ/6,324/2112113.shtml">方舟</A> </DIV>
<DIV class=newtitle4>[ 2005-09-12 17:01 ]</DIV></DIV>
<DIV class=clear1></DIV>
<STYLE type=text/css>A.zhy0815button_content {
BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 8px; BORDER-TOP: white 1px solid; PADDING-LEFT: 8px; FONT-SIZE: 12px; PADDING-BOTTOM: 4px; BORDER-LEFT: white 1px solid; COLOR: #000000; LINE-HEIGHT: 300%; PADDING-TOP: 4px; BORDER-BOTTOM: black 1px solid; FONT-FAMILY: "宋体"; TEXT-ALIGN: center; min-width: 75px
}
A.zhy0815button_content:link {
COLOR: #000; LINE-HEIGHT: 300%; BACKGROUND-COLOR: #d9d9d9; TEXT-DECORATION: none
}
A.zhy0815button_content:visited {
COLOR: #000000; LINE-HEIGHT: 300%; BACKGROUND-COLOR: #d9d9d9; TEXT-DECORATION: none
}
A.zhy0815button_content:hover {
BORDER-LEFT-COLOR: black; BORDER-BOTTOM-COLOR: white; COLOR: #333333; BORDER-TOP-COLOR: black; LINE-HEIGHT: 300%; BACKGROUND-COLOR: #cccccc; TEXT-DECORATION: none; BORDER-RIGHT-COLOR: white
}
A.zhy0815button_content:active {
COLOR: #000000; LINE-HEIGHT: 300%; BACKGROUND-COLOR: #d9d9d9; TEXT-DECORATION: none
}
</STYLE>
<CENTER><A class=zhy0815button_content title=编辑推荐你阅读
href="http://tech.bc0436.com/dev/msdn/113/2112113.shtml#1" target=_self>推荐阅读</A>
<A class=zhy0815button_content title=本栏目的最新更新
href="http://tech.bc0436.com/dev/msdn/113/2112113.shtml#2" target=_self>最新更新</A>
<A class=zhy0815button_content title=阅读此文章的读者还看了以下文章
href="http://tech.bc0436.com/dev/msdn/113/2112113.shtml#3" target=_self>继续阅读</A>
<A class=zhy0815button_content title=相关主题的关键字
href="http://tech.bc0436.com/dev/msdn/113/2112113.shtml#4" target=_self>相关主题</A>
<A class=zhy0815button_content title=本文编辑方舟的其他文章
href="http://tech.bc0436.com/dev/msdn/113/2112113.shtml#5" target=_self>编辑新作</A>
<A class=zhy0815button_content title=本栏目最近最受欢迎的文章
href="http://tech.bc0436.com/dev/msdn/113/2112113.shtml#6" target=_self>阅读排行</A>
<A class=zhy0815button_content id=comment_url_tag title=读者、编辑互动讨论区
href="http://comments.yesky.com/t/GDI+/6,324/2112113.shtml">讨论本文</A><A
class=zhy0815button_content id=comment_url_art title=读者、编辑互动讨论区
href="http://comments.yesky.com/a/6,324/2112113.shtml">讨论本文</A> <A
class=zhy0815button_content title=本文相关产品和厂商
href="http://tech.bc0436.com/dev/msdn/113/2112113.shtml#8"
target=_self>相关产品</A></CENTER>
<DIV class=space2><SPAN id=ad5></SPAN></DIV><BR>
<DIV class=newmiddlebox style="OVERFLOW: hidden">
<DIV class=guanggao><SPAN id=ad4></SPAN></DIV>
<TABLE cellSpacing=1 cellPadding=2 width=600 align=center bgColor=#ff3300
border=0>
<TBODY>
<TR bgColor=#ffffff>
<TD bgColor=#cc0000 colSpan=4><A
href="http://soft.yesky.com/lesson/"><FONT
color=#ffffff><B>天极软件专题专区精选</B></FONT></A> <A
href="http://comments.yesky.com/t/100913/6,324/2069547.shtml"
target=_blank><FONT color=#ffffff><B>到天极软件“读编交流区”畅所欲言</B></FONT></A></TD></TR>
<TR bgColor=#ffffff>
<TD width="25%"><A href="http://soft.yesky.com/tools/google">Google专区</A>
</TD>
<TD width="23%"><A href="http://soft.yesky.com/tools/popo">POPO专区</A></TD>
<TD width="24%"><A href="http://qq.yesky.com/">QQ专区</A> <A
href="http://soft.yesky.com/lesson/401/2047901.shtml">QQ挂机</A></TD>
<TD width="28%"><A
href="http://soft.yesky.com/327/2055327.shtml">了解Web2.0</A></TD></TR>
<TR bgColor=#ffffff>
<TD width="25%"><A href="http://design.yesky.com/flash/mx/">Flash MX
视频教程</A></TD>
<TD width="23%"><A
href="http://soft.yesky.com/SoftChannel/72348986094125056/20040623/1823629.shtml">Photoshop视频教程</A></TD>
<TD width="24%"><A
href="http://www.yesky.com/259/1926759.shtml">网页设计视频教程</A></TD>
<TD width="28%"><A
href="http://www.yesky.com/SoftChannel/72348986094125056/20050430/1943944.shtml">照片处理数字暗房</A></TD></TR>
<TR bgColor=#ffffff>
<TD width="25%"><A
href="http://www.yesky.com/SoftChannel/72348968914255872/20050517/1950125.shtml">PPT动画演示教程</A></TD>
<TD width="23%"><A
href="http://soft.yesky.com/SoftChannel/72348968914255872/20050126/1905902.shtml">Excel动画教程集</A></TD>
<TD width="24%"><A
href="http://soft.yesky.com/SoftChannel/72348968914255872/20050223/1914058.shtml">Word动画演示教程</A></TD>
<TD width="28%"><A href="http://soft.yesky.com/os/vista/">Windows
Vista专区</A> </TD></TR>
<TR bgColor=#ffffff>
<TD width="25%" height=18><A
href="http://soft.yesky.com/security/muma/">特洛伊木马专区</A></TD>
<TD width="23%" height=18><A
href="http://soft.yesky.com/security/hkjc/">黑客知识教程专区</A></TD>
<TD width="24%" height=18><A
href="http://soft.yesky.com//security/firewall/">防火墙应用专区</A></TD>
<TD width="28%" height=18><A
href="http://soft.yesky.com/os/zcb/">注册表应用专区</A></TD></TR>
<TR bgColor=#ffffff>
<TD width="25%"><A href="http://soft.yesky.com/lesson/vbapi/"
target=_blank>Windows API开发专区</A></TD>
<TD width="23%"><A href="http://soft.yesky.com/lesson/network/"
target=_blank>网络编程专区</A></TD>
<TD width="24%"><A href="http://soft.yesky.com/lesson/vbdatabase/"
target=_blank>VB数据库编程专区</A></TD>
<TD width="28%"><A
href="http://soft.yesky.com/lesson/multimedia/">图像处理与多媒体编程</A></TD></TR></TBODY></TABLE>
<P> <STRONG>前言<BR></STRONG><BR> 进入.NET时代,Windows的绘图技术也从<A class=bluekey
href="http://www.yesky.com/key/494/20494.html"
target=_blank>GDI</A>升级到了GDI+,从名字就能知道GDI+是对以前传统GDI绘图技术的一次升级,不过在微软几乎把所有的新技术都冠之.NET的情况下,GDI+竟然不叫做GDI.NET,还真让我感到有点意外了。<BR><BR></P>
<DIV class=guanggao><SPAN
id=contentAdv></SPAN></DIV> GDI+在一种与设备无关的环境下提供了一套统一的绘图编程模型,极大的提高了Windows绘图编程的方便性,我们再也不用创建什么各种各样复杂的设备环境了,说实话,我现在想起来都头疼。
<P></P>
<P
class=text4> 题归正传,关于如何进行GDI+的基本编程,我不能过多的加以描述,如果有对此概念还不太清楚的朋友,建议先去了解一下相关的资料,我们在这里主要讨论的是一种提高绘图效率(主要是动画效率)的双<A
class=bluekey href="http://www.yesky.com/key/530/80530.html"
target=_blank>缓冲</A>技术在GDI+中的应用和实现。</P>
<P class=text4> <STRONG>实现目的</STRONG></P>
<P class=text4> 为了能清楚的对比应用双缓冲技术前后的效果,我编写了一段程序来进行测试。首先,我创建了一个普通的Windows
Application,在主Form中,我放置了一个定时器:timer1,然后将它的Interval属性设置为10,然后在Form上放置两个按纽,分别用来控制定时器的开启和关闭,最后,我还放置了一个label<A
class=bluekey href="http://www.yesky.com/key/392/75392.html"
target=_blank>控件</A>,用来显示绘图的帧数。</P>
<P class=text4> <STRONG>测试程序</STRONG></P>
<P class=text4> 在timer1的timer1_Tick事件中,我写下了如下的代码(其中<A class=bluekey
href="http://www.yesky.com/key/3373/38373.html"
target=_blank>flag</A>是一个bool型标志变量):</P>
<DIV style="BACKGROUND-COLOR: #eeeeee"><PRE class=text5>DateTime t1 = DateTime.Now;
Graphics g = this.CreateGraphics();
if(flag)
{
brush = new LinearGradientBrush(new PointF(0.0f, 0.0f),
new PointF(700.0f, 300.0f), Color.Red, Color.Blue);
flag = false;
}
else
{
brush = new LinearGradientBrush(new PointF(0.0f, 0.0f),
new PointF(700.0f, 300.0f), Color.Blue, Color.Red);
flag = true;
}
for(int j = 0; j < 60; j ++)
{
for(int i = 0; i < 60; i++)
{
g.FillEllipse(brush, i * 10, j * 10, 10, 10);
}
}
DateTime t2 = DateTime.Now;
TimeSpan sp = t2 - t1;
float per = 1000 / sp.Milliseconds;
this.label1.Text = "速度:" + per.ToString() + "帧/秒";
</PRE></DIV>
<P class=text4>运行后,我点击“开始”按纽,效果如下图所示:</P>
<P align=center><IMG alt="" src="利用GDI+的双缓冲技术来提高绘图效率.files/j4if2yz6gc81.gif"
align=top><BR>应用双缓冲以前的效果图(帧数:5帧/秒)</P>
<P
class=text4> 正如大家所看到的,我在程序中使用循环画了几百个圆形,然后在每次的定时器脉冲事件中使用不同方向的线性渐变来对它们进行填充,形成了一个动画效果。不过不幸的是,程序运行起来闪烁很严重,几乎每次刷新的时候都可以看到一条很明显的扫描线从上慢慢的刷到下来完成整幅图形的刷新动作。如果你不是要模拟老式雷达的区域扫描的话,这种速度不会满足你的要求。</P>
<P class=text4>###adv### <STRONG>改进代码</STRONG></P>
<P class=text4> 下面是我改进以后的代码:</P>
<DIV style="BACKGROUND-COLOR: #eeeeee"><PRE class=text5>DateTime t1 = DateTime.Now;
Bitmap <A class=bluekey href="http://www.yesky.com/key/4223/14223.html" target=_blank>bmp</A> = new Bitmap(600, 600);
Graphics g = Graphics.FromImage(bmp);
if(flag)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -