如何显示背景透明的图片?

gougou6 2003-05-15 07:53:42
如上
...全文
282 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
xfyxq 2003-05-15
楼上的可以实现透明,还有一种方法,更加简单,只要一个函数就行了,并且不需要为透明处理制作掩码图,只需说明要透明处理的颜色值就行了。

例:在窗体上放一个按钮、两个PictureBox,分别用于放背景图和要透明插入的源图。

Private Declare Function TransparentBlt Lib "msimg32.dll" (ByVal hdcDest As Long, ByVal nXOriginDest As Long, ByVal nYOriginDest As Long, ByVal nWidthDest As Long, ByVal nHeightDest As Long, ByVal hdcSrc As Long, ByVal nXOriginSrc As Long, ByVal nYOriginSrc As Long, ByVal nWidthSrc As Long, ByVal nHeightSrc As Long, ByVal crTransparent As Long) As Long

Private Sub Command1_Click()
TransparentBlt Picture1.Hdc, 0, 0, 100, 100, Picture2.hdc, 0, 0, 100, 100, 0 '最后一个参数是指出要透明化的颜色值,这里是黑色
End Sub
回复
LoveBH 2003-05-15
在开始编写程序之前,要对准备透明放置的图片进行一些处理。将图片中要透明的地方设置为黑色。本例主要使用API函数BitBlt来实现位图的透明放置效果,基本设计步骤如下:

(1)首先在窗体上放置3个图片框,名称分别为Picture1、Picture2和Picture3。其中Picture1中放置准备透明放置的图片;Picture2图片框用来放置该图片的黑白蒙板;Picture3图片框用来放置另一副图片。

(2)将Picture1图片框中的图片拷贝到Picture2上,然后在Picture2上进行工作。即在Picture2上逐行逐点扫描位图信息,凡是非黑色象素点将其置成白色,这样在Picture2上就会产生图片的一个黑白蒙板。

(3)再将Picture1和Picture2作反相运算产生的图像存于Picture1中。

(4)然后用或运算将Picture2图像贴在Picture3中的图片上。

(5)最后用异或运算将Picture1图像贴再Picture3中的图片上即可。



Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
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
Private Const srccopy = &HCC0020
Private Const srcinvert = &H660046
Private Const srcpaint = &HEE0086

Private Sub Command1_Click()
Dim w As Long
Dim h As Long
black = RGB(0, 0, 0)
white = RGB(255, 255, 255)
'将度量单位转换为象素
w = Picture1.Width / Screen.TwipsPerPixelX
h = Picture1.Height / Screen.TwipsPerPixelY
'拷贝Picture1到Picture2上
r% = BitBlt(Picture2.hdc, 0, 0, w, h, Picture1.hdc, 0, 0, srccopy)
'将Picture2中的图像制作成蒙板
For i = 0 To h
For j = 0 To w
currentcolor = GetPixel(Picture2.hdc, j, i)
If currentcolor <> black Then
retlong = SetPixel(Picture2.hdc, j, i, white)
End If
Next j
Next i
'Picture1和Picture2作反相运算产生的图像存于Picture1中
r% = BitBlt(Picture1.hdc, 0, 0, w, h, Picture2.hdc, 0, 0, srcinvert)
'用或运算将Picture2图像贴于背景Picture3
r% = BitBlt(Picture3.hdc, 30, 30, w - 5, h - 5, Picture2.hdc, 0, 0, srcpaint)
'用异或运算再将Picture1图像贴于背景Picture3
r% = BitBlt(Picture3.hdc, 30, 30, w - 5, h - 5, Picture1.hdc, 0, 0, srcinvert)
End Sub

Private Sub Command2_Click()
End
End Sub


Private Sub Form_Load()
Picture2.Width = Picture1.Width
Picture2.Height = Picture1.Height
End Sub


欢迎光临电脑爱好者论坛 bbs.cfanclub.net
回复
相关推荐
发帖
VB基础类
创建于2007-09-28

7489

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2003-05-15 07:53
社区公告
暂无公告