VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - VB中实现图形柔和化
发表评论(0)作者:俞永江, 平台:VB6.0+Win98, 阅读:9699, 日期:2000-11-29
VB中实现图形柔和化
--------------------------------------------------------------------------------


柔化可使原来的图形变成模糊化,其效果就好像透过毛玻璃看一物体(如图):

?原理:位图是显示在不同控件上的对像素进行处理的图形,而位图中任何可以想像的颜色都可以表示为红、绿、蓝三原色来表示,我们可以通过VB中的Point 方法在图形中读取像素值,然后分析出这个像素值的三原颜色值,并把这些颜色值放在一个三维数组中。图形的柔化,就是使相邻像素间的颜色差别减小。若取9个像素为一组(如图),其中像素5就是要处理的像素,它的红、绿、蓝三值分别取其周围8个像素红、绿、蓝三值的平均值,使像素5的颜色向周围像素靠近,从而达到模糊的效果。
?主要控件设计:
?对像 属性 设置值
Picture1 Scalemode 3
Autoredraw True
?Autosize True
Form1 Windowstate 0
?程序代码:
?Di mi magepls(2,1000,1000)As Integer‘数组用于存放各像素的三原颜色值?Di mx,y As Integer?Private Sub Command1_Click()?Command1.Visible =False?Di mi ,j?Di mred As Integer ,green As Integer ,blue As Integer?Di mpl &?CommonDialog1.Filter ="位图(*.BMP)|*.BMP"‘用对话框装入图形文件?CommonDialog1.ShowOpen?Picture1.Picture =LoadPicture(CommonDialog1.filename)?Form1.Refresh?x =Picture1.Scale Width‘获取图形X 坐标值?y =Picture1.ScaleHeight‘获取图形Y 坐标值?Form1.Width =Form1.ScaleX(Picture1.Width ,vbPixels ,vbTwips)?Form1.Height =Form1.ScaleY(Picture1.Height ,vbPixels ,vbTwips)?Form1.Refresh?‘分析各像素的颜色,并放在数组i magepls 中?For i =0To y -1?For j =0To x -1?pl &=Form1.Picture1.Point(j ,i)‘获取像素的颜色值(长整形)?red =pl &Mod 256‘获取像素的红色值?green =((pl &And &HFF00)/256&)Mod 256&‘获取像素的绿色值?blue =(pl &And &HFF0000)/65536‘获取像素的蓝色值?i magepls(0,i ,j)=red?i magepls(1,i ,j)=green?i magepls(2,i ,j)=blue?Next?ProgressBar1.Value =i *100/(y -1)‘分析图形的进程显示?Next?‘模糊过程?For i =1To y -2?For j =1To x -2?red =i magepls(0,i -1,j -1)+i magepls(0,i-1,j)+i magepls(0,i_-1,j +1)+i magepls(0,i ,j -1)+i magepls(0,i ,j)+i magepls(0,i ,j +1)+_i magepls(0,i +1,j -1)+i magepls(0,i +1,j)+i magepls(0,i +1,j +1)?green =i magepls(1,i -1,j -1)+i magepls(1,i-1,j)+i magepls(1,_i -1,j +1)+i magepls(1,i ,j -1)+i magepls(1,i ,j)+i magepls(1,i ,j +1)+_i magepls(1,i +1,j -1)+i magepls(1,i +1,j)+i magepls(1,i +1,j +1)?blue =i magepls(2,i -1,j -1)+i magepls(2,i-1,j)+i magepls(2,_i -1,j +1)+i magepls(2,i ,j -1)+i magepls(2,i ,j)+i magepls(2,i ,j +1)+_i magepls(2,i +1,j -1)+i magepls(2,i +1,j)+i magepls(2,i +1,j +1)?Picture1.PSet (j ,i ),RGB(red /9,green /9,blue /9)?Next?Picture1.Refresh?Next? End Sub?Private Sub Command2_Click()‘结束演示?End? End Sub?(浙江省新昌中学俞永江312500)