C++ Builder 中的图像显示技巧

来源:百度文库 编辑:神马文学网 时间:2024/05/02 04:05:10
http://www.sina.com.cn 2002/01/16 13:24 赛迪网-中国计算机报
文/黄建志
在C++ Builder中,实现一幅图像的显示非常简单,只要在Form窗体中定义一个TImage组件,设置其Picture属性,然后选择任何有效的.ico、.bmp、.emf或.wwf文件,加载进来,所选文件就显示在TImage组件中。但这只是直接将图形显示在窗体中,毫无技巧可言,给人感觉是一种枯燥乏味。为了使图形显示有别具一格的效果。按下列步骤实现:
1、 定义一个TImage组件,把要显示的图形先加载到TImage组件中,也就是说,把图
形内容从磁盘载入内存中,作为图形缓存。
2、 创建一新的位图对象,其尺寸跟TImage组件中的图形一样。
3、 利用画布(Canvas)的CopyRect功能(将一个画布的矩形区域拷贝到另一个画布的矩形区域),以达到动态显示位图。
下面介绍各种图形显示技巧的具体实现方法。
上拉效果

图1
实现原理:首先将暂存图形的第一条水平线,搬移至要显示位图的最后一条,接着再将暂存图形的前两条水平线,依次搬移至要显示位图的最后两条水平线,然后搬移前三条、前四条直到全部图形数据搬完为止。在搬移的过程中即可看到显示的位图由下而上浮起,而达到上拉的效果(如图1)。
程序算法:
void _fastcall TFor-
m1::Button1Click(TObject *Sender)
{int i,width,height;
Graphics::TBitmap *newbmp;
newbmp=new Graphics::TBitmap;
newbmp-〉Width=Image1-〉Width;
newbmp-〉Height=Image1-〉Height;
width=Image1-〉Width;
height=Image1-〉Height;
for(i=0;i
{newbmp-〉Canvas-〉CopyRect(Rect(0,height-i,width,height),
Image1-〉Canvas,Rect(0,0,width,i));
Form1-〉Canvas-〉Draw(10,10,newbmp);}
delete newbmp;}
从左向右平铺显示效果

图2
实现原理:首先将暂存图形的最后一条竖线,搬移至要显示位图的第一条竖线,接着再将暂存图形的最后两条竖线,依序搬移至要显示位图的前两条竖线,然后搬移最后三条、四条竖线直到全部图形数据搬完为止。在搬移的过程中即可看到显示的位图由左向右浮起,而达到从左向右平铺显示的效果(如图2)。
程序算法:
void _fastcall TForm1::Button2Click(TObject *Sender)
{int i,width,height;
Graphics::TBitmap *newbmp;
newbmp=new Graphics::TBitmap;
newbmp-〉Width=Image1-〉Width;
newbmp-〉Height=Image1-〉Height;
width=Image1-〉Width;
height=Image1-〉Height;
for(i=0;i<=width;i++)
{ newbmp-〉Canvas-〉
CopyRect(Rect(0,0,i,height),
Image1-〉Canvas,Rect(width-i,0,width,height));
Form1-〉Canvas-〉Draw(10,10,newbmp); }
delete newbmp;}
垂直交错效果

图3
实现原理:将要显示的图形拆成两部分,奇数条扫描线由上往下搬移,偶数条扫描线则由下往上搬移,而且两者同时进行。便可看到分别由上下两端出现的较淡图形向屏幕中央移动,直到完全清楚为止(如图3)。
程序算法:
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{Graphics::TBitmap *newbmp;
int i,j,height,width;
newbmp=new Graphics::TBitmap;
newbmp-〉Width=Image1-〉Width;
newbmp-〉Height=Image1-〉Height;
height=Image1-〉Height;
width=Image1-〉Width;
i=0;
while(i<=height)
{for(j=0;j
{newbmp-〉Canvas-〉CopyRect(Rect(j*2,0,j*2+1,i),Image1-〉Canvas,
Rect(j*2,0,j*2+1,i));
newbmp-〉Canvas-〉CopyRect(Rect(j*2+1,height,j*2+2,height-i),
Image1-〉Canvas, Rect(j*2+1,height,j*2+2,height-i)); }
Form1-〉Canvas-〉Draw(10,10,newbmp);
i+=2; }
delete newbmp;}
水平交错效果

图4
实现原理:同垂直交错效果原理一样,将要显示的图形拆成两部分,奇数条扫描线由左往右搬移,偶数条扫描线则由右往左搬移,两者同时进行。从屏幕上便可看到分别由左右两端出现的较淡图形向屏幕中央移动,直到完全清楚为止(如图4)。
程序算法:
void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{int i,j,height,width;
Graphics::TBitmap *newbmp;
newbmp=new Graphics::TBitmap;
newbmp-〉Width=Image1-〉Width;
newbmp-〉Height=Image1-〉Height;
height=Image1-〉Height;
width=Image1-〉Width;
i=0;
while(i<=height)
{for(j=0;j
{newbmp-〉Canvas-〉CopyRect(Rect(0,j*10,i,j*10+5),Image1-〉Canvas,
Rect(0,j*10,i,j*10+5));
newbmp-〉Canvas-〉CopyRect(Rect(width-i,j*10+5,width,j*10+10),
Image1-〉Canvas, Rect(width-i,j*10+5,width,j*10+10)); }
Form1-〉Canvas-〉Draw(10,10,newbmp);
i+=2; }
delete newbmp;}
从左到右圆筒滚动效果

图5
实现原理:图形复制过程中,把目标图形的坐标按照曲线方式移动,以达到圆筒滚动效果(如图5)。
程序算法:
void __fastcall TForm1::BitBtn5Click(TObject *Sender)
{int i,j,height,width;
Graphics::TBitmap *newbmp;
newbmp=new Graphics::TBitmap;
newbmp-〉Width=Image1-〉Width;
newbmp-〉Height=Image1-〉Height;
height=Image1-〉Height;
width=Image1-〉Width;
i=0;
int intr=50;
for(i=0;i<=width;i+=5)
{for(j=1;j<=2*intr;j++)
{newbmp-〉Canvas-〉CopyRect(Rect(i+j,-sqrt(2*intr*j-j*j),i+j+1,-sqrt(2*intr*j-j*j)+height),Image1-〉Canvas,Rect(i+j,0,i+j+1,height));}
newbmp-〉Canvas-〉CopyRect(Rect(i,0,i+5,height),Image1-〉Canvas,Rect(i,0,i+5,height));
Form1-〉Canvas-〉Draw(10,10,newbmp);
Sleep(10); }}
所有程序算法都在C++ Builder 4.0和5.0调试通过。