bmp格式的图片是绝不可能透明的,目前支持透明的图象格式只有GIF和PNG GIF的透明图片可以经常看到,PNG的不常见。 你看到的透明可能是通过某种方法把BMP图中的某种颜色作为透明色之后将其去掉而形成的,比较常用的方法就是做MASK图,而在WIN98以上版本的WINDOWS中可以在VB中声明以下API函数,方便的做出透明效果:Public 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 用法类似Bitblt,只是最后的crTransparent是作为透明颜色的颜色值。
上面的API可以在MSDN中找到详细的
以下是我自己写的代码,与TransparentBlt 功能差不多,不过少简单点,少了两个参数,呵呵 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 Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long Public Sub SuperBlt(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 TC As Long) 'TC表示透明色 Dim OrigColor As Long Dim OrigMode As Long Dim SaveDC As Long Dim maskDC As Long Dim invDC As Long Dim resDC As Long Dim hSaveBmp As Long Dim hMaskBmp As Long Dim hInvBmp As Long Dim hResBmp As Long Dim hSavePrevBmp As Long Dim hMaskPrevBmp As Long Dim hInvPrevbmp As Long Dim hDestPrevBmp As Long
'TransparentBlt 'This project needs 2 pictureboxes 'Picturebox1 must contain a picture with a lot of white pixels (we're going to use white as transparent color) Private Declare Function TransparentBlt Lib "msimg32.dll" (ByVal hdc 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 nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal crTransparent As Long) As Boolean Private Sub Form_Load() 'KPD-Team 1999 'URL: http://www.allapi.net/ 'E-Mail: [email protected] Picture1.AutoSize = True 'API uses pixels Picture1.ScaleMode = vbPixels Picture2.ScaleMode = vbPixels End Sub Private Sub Picture2_Paint() 'If we don't call DoEvents first, our transparent image will be completely wrong DoEvents TransparentBlt Picture2.hdc, 0, 0, Picture2.ScaleWidth, Picture2.ScaleHeight, Picture1.hdc, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, vbWhite End Sub
GIF的透明图片可以经常看到,PNG的不常见。
你看到的透明可能是通过某种方法把BMP图中的某种颜色作为透明色之后将其去掉而形成的,比较常用的方法就是做MASK图,而在WIN98以上版本的WINDOWS中可以在VB中声明以下API函数,方便的做出透明效果:Public 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
用法类似Bitblt,只是最后的crTransparent是作为透明颜色的颜色值。
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
Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long
Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Public Sub SuperBlt(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 TC As Long)
'TC表示透明色
Dim OrigColor As Long
Dim OrigMode As Long
Dim SaveDC As Long
Dim maskDC As Long
Dim invDC As Long
Dim resDC As Long
Dim hSaveBmp As Long
Dim hMaskBmp As Long
Dim hInvBmp As Long
Dim hResBmp As Long
Dim hSavePrevBmp As Long
Dim hMaskPrevBmp As Long
Dim hInvPrevbmp As Long
Dim hDestPrevBmp As Long
SaveDC = CreateCompatibleDC(hDestDC)
maskDC = CreateCompatibleDC(hDestDC)
invDC = CreateCompatibleDC(hDestDC)
resDC = CreateCompatibleDC(hDestDC)
hMaskBmp = CreateBitmap(nWidth, nHeight, 1, 1, ByVal 0&)
hInvBmp = CreateBitmap(nWidth, nHeight, 1, 1, ByVal 0&)
hResBmp = CreateCompatibleBitmap(hDestDC, nWidth, nHeight)
hSaveBmp = CreateCompatibleBitmap(hDestDC, nWidth, nHeight)
hSavePrevBmp = SelectObject(SaveDC, hSaveBmp)
hMaskPrevBmp = SelectObject(maskDC, hMaskBmp)
hInvPrevbmp = SelectObject(invDC, hInvBmp)
hDestPrevBmp = SelectObject(resDC, hResBmp)
OrigColor = SetBkColor(hSrcDC, TC)
BitBlt maskDC, 0, 0, nWidth, nHeight, hSrcDC, xSrc, ySrc, vbSrcCopy
TC = SetBkColor(hSrcDC, OrigColor)
BitBlt invDC, 0, 0, nWidth, nHeight, maskDC, 0, 0, vbNotSrcCopy
BitBlt resDC, 0, 0, nWidth, nHeight, hDestDC, x, y, vbSrcCopy
BitBlt resDC, 0, 0, nWidth, nHeight, maskDC, 0, 0, vbSrcAnd
BitBlt SaveDC, 0, 0, nWidth, nHeight, hSrcDC, xSrc, ySrc, vbSrcCopy
BitBlt SaveDC, 0, 0, nWidth, nHeight, invDC, 0, 0, vbSrcAnd
BitBlt resDC, 0, 0, nWidth, nHeight, SaveDC, 0, 0, vbSrcInvert
BitBlt hDestDC, x, y, nWidth, nHeight, resDC, 0, 0, vbSrcCopy
SelectObject SaveDC, hSavePrevBmp
SelectObject resDC, hDestPrevBmp
SelectObject maskDC, hMaskPrevBmp
SelectObject invDC, hInvPrevbmp
DeleteObject hSaveBmp
DeleteObject hMaskBmp
DeleteObject hInvBmp
DeleteObject hResBmp
DeleteDC SaveDC
DeleteDC invDC
DeleteDC maskDC
DeleteDC resDC
End Sub
请问你的这个代码是如何使用的呀?如何设置SuperBlt的参数??
'This project needs 2 pictureboxes
'Picturebox1 must contain a picture with a lot of white pixels (we're going to use white as transparent color)
Private Declare Function TransparentBlt Lib "msimg32.dll" (ByVal hdc 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 nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal crTransparent As Long) As Boolean
Private Sub Form_Load()
'KPD-Team 1999
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Picture1.AutoSize = True
'API uses pixels
Picture1.ScaleMode = vbPixels
Picture2.ScaleMode = vbPixels
End Sub
Private Sub Picture2_Paint()
'If we don't call DoEvents first, our transparent image will be completely wrong
DoEvents
TransparentBlt Picture2.hdc, 0, 0, Picture2.ScaleWidth, Picture2.ScaleHeight, Picture1.hdc, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, vbWhite
End Sub