![]() |
|
||||||||||||||
| | 网站首页 | 数据库教程 | web编程 | 服务器 | 程序设计 | | ||
|
||
|
||||||
| VB6下的BitMap示例:模拟雨点程序 | ||||||
作者:佚名 文章来源:不详 点击数: 更新时间:2007-8-6 ![]() |
||||||
|
窗体部分 Dim N As tpBitMapApplic 模块部分 Public Type tpBitMapFileHeader
'返回:Boolean tOutBool 逻辑值。如pBitMapApplic有效则为真。 '说明:本函数仅接受24bit位图。 Dim tOutBool As Boolean With pBitMapApplic.bmaHeader tOutBool = (.bhFileHeader.bfType = &H4D42) And (.bhInfoHeader.biBitCount = 24) End With BitMapApplicIsBitMap = tOutBool End Function Public Function BitMapApplicPutToFile(ByVal pFileName As String, ByRef pBitMapApplic As tpBitMapApplic) As Long 'BitMapApplicPutToFile函数 '语法:[tOutLength]=BitMapApplicPutToFile(pFileName, pBitMapApplic) '功能:将一个BitMapApplic存储到文件中。 '参数:string pFileName 必要参数。有效文件名 ' tpBitMapApplic pBitMapApplic 必要参数。位图的BitMapApplic '返回:long tOutLength 位图文件的长度 '说明:本函数并不检测BMP文件是否是正确的格式,仅接受24bit位图。 Dim tOutLength As Long Dim tBitMapHeader As tpBitMapHeader Dim tBytes() As Byte Dim tBytesCount As Long Dim tFileNumber As Integer Dim tOffBits As Long tFileNumber = FreeFile tBitMapHeader = pBitMapApplic.bmaHeader tBytes() = pBitMapApplic.bmaBytes tOffBits = tBitMapHeader.bhFileHeader.bfOffBits Open pFileName For Binary As #tFileNumber Put #tFileNumber, 1, tBitMapHeader Put #tFileNumber, tOffBits + 1, tBytes() tOutLength = LOF(tFileNumber) Close #tFileNumber BitMapApplicPutToFile = tOutLength End Function Public Function BitMapApplicGetByFile(ByVal pFileName As String) As tpBitMapApplic 'BitMapApplicGetByFile函数 '语法:[tOutBitMapApplic]=BitMapApplicGetByFile(pFileName) '功能:从文件中获得一个BitMapApplic '参数:string pFileName 必要参数。有效文件名 '返回:tpBitMapApplic tOutBitMapApplic '说明:本函数并不检测BMP文件是否是正确的格式,仅接受24bit位图。 Dim tOutBitMapApplic As tpBitMapApplic Dim tBitMapHeader As tpBitMapHeader Dim tBytes() As Byte Dim tBytesCount As Long Dim tFileNumber As Integer Dim tOffBits As Long tFileNumber = FreeFile Open pFileName For Binary As #tFileNumber Get #tFileNumber, 1, tBitMapHeader With tBitMapHeader tOffBits = .bhFileHeader.bfOffBits .bhInfoHeader.biWidth = .bhInfoHeader.biWidth + (CBool(.bhInfoHeader.biWidth Mod 2) And 1) tBytesCount = .bhInfoHeader.biWidth * .bhInfoHeader.biHeight * .bhInfoHeader.biBitCount \ 8 End With ReDim tBytes(tBytesCount - 1) Get #tFileNumber, tOffBits + 1, tBytes() Close #tFileNumber With tOutBitMapApplic .bmaHeader = tBitMapHeader .bmaBytes = tBytes() End With BitMapApplicGetByFile = tOutBitMapApplic End Function '[Pixels] Function PixelsShow(ByRef pPixels() As tpPixelRGB24, pBitMapInfo As tpBitMapInfo) End Function Function PixelsGetByBytes(ByRef pBytes() As Byte) As tpPixelRGB24() 'PixelsGetByBytes函数 '语法:[tOutPixels()]=PixelsGetByBytes(pBytes()) '功能:将Byte数组表示的位图数据转换成tpPixelRGB24数组。 '参数:byte pBytes() 必要参数。包含有位图数据的Byte数组 '返回:tpPixelRGB24 tOutPixels() 必要参数。包含有位图数据的tpPixelRGB24数组 Dim tOutPixels() As tpPixelRGB24 Dim tOutPixelsLength As Long Dim tBytesLength As Long tBytesLength = UBound(pBytes) + 1 tOutPixelsLength = tBytesLength \ 3 ReDim tOutPixels(tOutPixelsLength - 1) CopyMemory tOutPixels(0), pBytes(0), tBytesLength PixelsGetByBytes = tOutPixels() End Function Function PixelGetBySet(ByVal pRed As Byte, ByVal pGreen As Byte, ByVal pBlue As Byte) As tpPixelRGB24 Dim tOutPixel As tpPixelRGB24 With tOutPixel .rgbBlue = pBlue .rgbGreen = pGreen .rgbRed = pRed End With PixelGetBySet = tOutPixel End Function Function PixelAlphaMix(ByRef pBackPix As tpPixelRGB24, ByRef pOverPix As tpPixelRGB24, ByVal pAlpha As Byte, Optional ByVal pAlphaLevel As Byte = 100) As tpPixelRGB24 Dim tOutPixel As tpPixelRGB24 With tOutPixel .rgbBlue = ByteLayersAlphaMix(pBackPix.rgbBlue, pOverPix.rgbBlue, pAlpha, pAlphaLevel) .rgbGreen = ByteLayersAlphaMix(pBackPix.rgbGreen, pOverPix.rgbGreen, pAlpha, pAlphaLevel) .rgbRed = ByteLayersAlphaMix(pBackPix.rgbRed, pOverPix.rgbRed, pAlpha, pAlphaLevel) End With PixelAlphaMix = tOutPixel End Function Function PixelGetByPixels(ByVal pX As Long, ByVal pY As Long, pPixels() As tpPixelRGB24, pBitMapInfoHeader As tpBitMapInfoHeader) As tpPixelRGB24 Dim tBytesIndex As Long Dim tPixelIndex As Long Dim tX As Long Dim tY As Long Dim tWidth As Long Dim tHeight As Long With pBitMapInfoHeader tWidth = .biWidth tHeight = .biHeight End With tX = pX Mod tWidth tY = tHeight - (pY Mod tHeight) - 1 tPixelIndex = tY * tWidth + tX PixelGetByPixels = pPixels(tPixelIndex) End Function Function PixelSetToPixels(ByVal pX As Long, ByVal pY As Long, pPixels() As tpPixelRGB24, pBitMapInfoHeader As tpBitMapInfoHeader, pPixel As tpPixelRGB24) Dim tBytesIndex As Long Dim tPixelIndex As Long Dim tX As Long Dim tY As Long Dim tWidth As Long Dim tHeight As Long With pBitMapInfoHeader tWidth = .biWidth tHeight = .biHeight End With tX = pX Mod tWidth tY = tHeight - (pY Mod tHeight) - 1 tPixelIndex = tY * tWidth + tX pPixels(tPixelIndex) = pPixel End Function '[Bytes] Function BytesGetByPixels(ByRef pPixels() As tpPixelRGB24) As Byte() 'BytesGetByPixels函数 '语法:[tOutBytes()]=BytesGetByPixels(pPixels()) '功能:将tpPixelRGB24数组表示的位图数据转换成Byte数组。 '参数:tpPixelRGB24 pPixels() 必要参数。包含有位图数据的tpPixelRGB24数组 '返回:Byte tOutBytes() 必要参数。包含有位图数据的Byte数组 Dim tOutBytes() As Byte Dim tOutBytesLength As Long Dim tPixelsLength As Long tPixelsLength = UBound(pPixels) + 1 tOutBytesLength = tPixelsLength * 3 ReDim tOutBytes(tOutBytesLength - 1) CopyMemory tOutBytes(0), pPixels(0), tOutBytesLength BytesGetByPixels = tOutBytes() End Function Function ByteLayersAlphaMix(pBackValue As Byte, pOverValue As Byte, pAlpha As Byte, Optional pAlphaLevel As Byte = 100) As Byte 'ByteLayersAlphaMix函数 '语法:[tOutByte]=ByteLayersAlphaMix(pBackValue, pOverValue, pAlpha, [pAlphaLevel]) '功能:将两个Byte类型的值进行Alpha混合运算,此函数是对像素进行Alpha混合的基础函数。 '参数:byte pBackValue 必要参数。做底色的亮度数据。 ' byte pOverValue 必要参数。做覆盖色的亮度数据。 ' byte pAlpha 必要参数。覆盖色的Alpha透明度,须对应pAlphaLevel的规定。 ' byte pAlphaLevel 可选参数。Alpha的透明度级别,最大可到255。 '返回:byte tOutByte 混合后的Byte数据 Dim tOutByte As Byte Dim tBackAlpha As Long Dim tMixValue As Long tBackAlpha = Abs(pAlphaLevel - pAlpha) tMixValue = (CLng(pBackValue) * tBackAlpha + CLng(pOverValue) * CLng(pAlpha)) \ CLng(pAlphaLevel) tOutByte = tMixValue Mod 256 ByteLayersAlphaMix = tOutByte End Function '[Other] Function PixelSetToBitMapApplic(ByVal pX As Long, ByVal pY As Long, pBytes() As Byte, pBitMapInfoHeader As tpBitMapInfoHeader, pPixel As tpPixelRGB24) Dim tBytesIndex As Long Dim tPixelIndex As Long Dim tX As Long Dim tY As Long Dim tWidth As Long Dim tHeight As Long With pBitMapInfoHeader tWidth = .biWidth tHeight = .biHeight End With tX = pX Mod tWidth tY = pY Mod tHeight tPixelIndex = tY * tWidth + tX tBytesIndex = tPixelIndex * 3 With pPixel pBytes(tBytesIndex) = .rgbBlue pBytes(tBytesIndex + 1) = .rgbGreen pBytes(tBytesIndex + 2) = .rgbRed End With 'Form_Test.Text1.Text = tBytesIndex End Function Function PixelGetByBitMapApplic(ByVal pX As Long, ByVal pY As Long, pBitMapApplic As tpBitMapApplic) As tpPixelRGB24 Dim tOutPixel As tpPixelRGB24 Dim tBytes() As Byte Dim tBytesIndex As Long Dim tPixelIndex As Long Dim tX As Long Dim tY As Long Dim tWidth As Long Dim tHeight As Long tBytes() = pBitMapApplic.bmaBytes With pBitMapApplic.bmaHeader.bhInfoHeader tWidth = .biWidth tHeight = .biHeight End With tX = pX Mod tWidth tY = pY Mod tHeight tPixelIndex = tY * tHeight + tX tBytesIndex = tPixelIndex * 3 With tOutPixel .rgbBlue = tBytes(tBytesIndex) .rgbGreen = tBytes(tBytesIndex + 1) .rgbRed = tBytes(tBytesIndex + 2) End With PixelGetByBitMapApplic = tOutPixel End Function Function BytesAddLandBlur(pBytes As Variant, pLandWidth As Integer) As Byte() Dim tLoop As Long Dim tBytesSur() As Byte Dim tBytesDes() As Byte Dim tLoopOn As Long Dim tLoopEnd As Long Dim tIndex As Long Dim tIndexB As Long Dim tIndexP As Long Dim tPixByte(2) As Long tBytesSur() = pBytes tBytesDes() = pBytes tLoopOn = LBound(tBytesIn) tLoopEnd = UBound(tBytesIn) Form_Test.Text1.Text = ((tLoopEnd - tLoopOn) + 1) Mod 3 For tLoop = tLoopOn To tLoopEnd tIndex = tLoop * 3 tIndexB = (tLoop - 1) * 3 tIndexP = (tLoop + 1) * 3 tB1 = (CLng(tBytesIn(tIndex)) + CLng(tBytesIn(tIndexB)) + CLng(tBytesIn(tIndexP))) \ 3 tB2 = (CLng(tBytesIn(tIndex + 1)) + CLng(tBytesIn(tIndexB + 1)) + CLng(tBytesIn(tIndexP + 1))) \ 3 tB3 = (CLng(tBytesIn(tIndex + 2)) + CLng(tBytesIn(tIndexB + 2)) + CLng(tBytesIn(tIndexP + 2))) \ 3 Next BytesAddLandBlur = tBytesOut End Function Function ValueSetDefault(ByVal pValue As Long, ByVal pDefValue As Long) As Long Dim tOutLong As Long tOutLong = pValue + (pDefValue And (Not CBool(pValue))) ValueSetDefault = tOutLong End Function Function RainDraw(pX As Long, pY As Long, pPixels() As tpPixelRGB24, pBitMapInfo As tpBitMapInfo, pLineLong As Long, pColorPixel As tpPixelRGB24, Optional pAlpha As Byte = 100) Dim tLoop As Long Dim tY As Long Dim tPixel As tpPixelRGB24 Dim tBackPixel As tpPixelRGB24 Dim tAlpha As Byte Dim tBitMapInfoHeader As tpBitMapInfoHeader tBitMapInfoHeader = pBitMapInfo.bmiHeader For tLoop = -pLineLong To pLineLong tY = pY + tLoop If tY < tBitMapInfoHeader.biHeight And tY >= 0 Then tAlpha = (100 * (pLineLong - Abs(tLoop)) * pAlpha) \ pLineLong * 100 tBackPixel = PixelGetByPixels(pX, tY, pPixels(), tBitMapInfoHeader) tPixel = PixelAlphaMix(tBackPixel, pColorPixel, tAlpha) PixelSetToPixels pX, tY, pPixels(), tBitMapInfoHeader, tPixel End If Next End Function 本文来源:http://blog.csdn.net/KiteGirl/archive/2007/07/11/1684894.aspx
|
||||||
| 文章录入:admin 责任编辑:admin | ||||||
| 【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 | ||||||
| 网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!) |
| | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告 | 网站地图 | 管理登录 | | |||
|