【VB开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。
网学会员整理了VB开源代码-VB程序例子 - 技术总结的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
我们知道GIF类型的图像格式支持透明显示而BMP类型的图像格式不支持透明显示。
然而在
VB中我们只需通过简单的编程就能实现位图透明显示。
透明位图与透明GIF图像相比有一个很大的优点即GIF图像格式最大支持256种颜色位图却可以支持真彩色。
下面我们以将一幅飞鸟前景图象透明显示于天空背景为例来说明
VB中编程显示透明位图的方法。
在
VB中新建工程在Form1窗体上放置控件 名称 类型 属性 Command1 CommandButton Caption: 用对象方法绘图 Command2 CommandButton Caption: 用API函数绘图 Text1 TextBox Text: 0 Text2 TextBox Text: 0 Picture1 PictureBox picture: 载入蓝天的位图Clouds.bmp Picture2 PictureBox picture: 载入飞鸟的位图hawks.bmp Label1 Label Caption: Top Label2 Label Caption: Left 一、使用
VB对象方法
VB中为PictureBox控件对象提供了Point和PSet方法前者用于取得指定点上的RGB颜色值后者用于将对象上的点设置为指定颜色。
使用这两个方法从前景图中读取各像素点的RGB颜色值滤除设置成透明颜色的点再重绘于背景图上即可实现位图透明显示的效果。
双击窗体填写
代码将PictureBox对象坐标的度量单位设为象素 Private Sub Form_Load Picture1.ScaleMode vbPixels Picture2.ScaleMode vbPixels wid Picture2.ScaleWidth hei Picture2.ScaleHeight End Sub 单击Command1显示透明位图其
代码如下 Private Sub Command1_Click bgcol Picture2.Point1 1 ’以位图11处的象素的颜色为透明色 px Text1.Text py Text2.Text For i 0 To wid - 1 For j 0 To hei - 1 If Picture2.Pointi j bgcol Then Picture1.PSet px i py j Picture2.Pointi j End If Next j Next i End Sub 二、使用API函数 尽管用Point和PSet方法就可以很容易达到目的但如果要有更快运行速度则应该考虑使用 Windows API 函数GetPixel和PutPixel 声明部分 Private Declare Function GetPixel Lib gdi32 ByVal hdc As Long ByVal x As Long ByVal y As Long As Long Private Declare Function SetPixel Lib gdi32 ByVal hdc As Long ByVal x As Long ByVal y As Long ByVal crColor As Long As Long Command2调用API函数来显示透明位图 Private Sub Command2_Click bgcol GetPixelPicture2.hdc 1 1 px Text1.Text py Text2.Text For i 0 To wid - 1 For j 0 To hei - 1 fgCol GetPixelPicture2.hdc i j If fgCol bgcol Then temp SetPixelPicture1.hdc px i py j fgCol End If Next j Next i End SUB