Rubrik: Grafik und Font · Grafische Effekte | VB-Versionen: VB4, VB5, VB6 | 15.04.01 |
AlphaBlending Dieser Tipp zeigt das Software-Alphablending zum Überlagern zweier Bitmaps. | ||
Autor: LonelySuicide666 | Bewertung: | Views: 23.160 |
www.vbapihelpline.de | System: Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Der nachfolgende Tipp zeigt das Software-Alphablending zum Überlagern zweier Bitmaps, wobei eines eine gewisse Transparenz (Alpha-Value) besitzt. Der Alphawert kann variabel im Bereich 0.01 und 0.99 eingestellt werden. Dieser Wert bestimmt die Intensität des Hauptbildes.
Wir empfehlen Ihnen das Beispiels-Projekt zu laden, welches das AlphaBlending sehr gut demonstriert.
' zunächst die benötigten API-Deklarationen Private Declare Function StretchBlt Lib "gdi32" ( _ 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 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 ' (DWORD) dest = source Private Const SRCCOPY = &HCC0020 Private Type RGBCol Red As Long Green As Long Blue As Long End Type
Die Hauptroutine:
Als Parameter werden die beiden Quell-Bildfelder, das Ziel-Bildfeld, der Alpha-Wert und ein Label-Steuerelement erwartet. Der Alpha-Wert muß im Bereich 0.01 und 0.99 liegen. Das Label-Steuerelement zeigt den Fortschritt des AlphaBlendings in Prozent an.
' Die Alpha Blending Funktion Public Function AlphaBlend(ByRef Src1Pic As PictureBox, _ ByRef Src2Pic As PictureBox, _ ByRef DestPic As PictureBox, _ ByVal AlphaValue As Currency, ByRef Progress As Label) Dim j As Long, k As Long Dim BackCol As Long, BackRGB As RGBCol Dim ForeCol As Long, ForeRGB As RGBCol Dim NewCol As Long, NewRGB As RGBCol Dim RBitMask As Long, GBitMask As Long Dim BBitMask As Long ' Labelfeld auf 0% setzen und Ziel-Pictureobjekt leeren Progress.Caption = "0%" DestPic.Cls ' Die Hauptschleife für jedes Pixel For j = 0 To DestPic.ScaleWidth For k = 0 To DestPic.ScaleHeight ' Hintergrund-Farbwerte ermitteln BackCol = GetPixel(Src1Pic.hdc, j, k) With BackRGB ' Enthält die Alphafarbe für den Hintergrund .Red = (BackCol And &HFF&) * (1 - AlphaValue) .Green = (BackCol \ &H100& And &HFF&) * _ (1 - AlphaValue) .Blue = (BackCol \ &H10000 And &HFF&) * _ (1 - AlphaValue) End With ' Fordergrund Farbwerte bekommen ForeCol = GetPixel(Src2Pic.hdc, j, k) With ForeRGB ' Enthält die Alphafarbe für den Vordergrund .Red = (ForeCol And &HFF&) * AlphaValue .Green = (ForeCol \ &H100& And &HFF&) * _ AlphaValue .Blue = (ForeCol \ &H10000 And &HFF&) * _ AlphaValue End With ' Farbwerte addieren With NewRGB .Red = ForeRGB.Red + BackRGB.Red .Green = ForeRGB.Green + BackRGB.Green .Blue = ForeRGB.Blue + BackRGB.Blue End With ' Setzt das Ziel-Pixel SetPixel DestPic.hdc, j, k, _ RGB(NewRGB.Red, NewRGB.Green, NewRGB.Blue) ' Labelfeld aktualisieren Progress.Caption = CStr(CInt(Round((100 / _ DestPic.ScaleWidth) * j))) & "%" DoEvents Next k Next j ' Ziel-Pictureobjekt "refreshen" DestPic.Refresh End Function ' Stretcht ein Bitmap mit Hilfe eines temporären Picture-Objektes Public Function StretchPicture(ByRef SrcPic As _ PictureBox, ByRef TmpPic As PictureBox) Set TmpPic.Picture = SrcPic.Picture StretchBlt SrcPic.hdc, 0, 0, SrcPic.ScaleWidth, _ SrcPic.ScaleHeight, TmpPic.hdc, 0, 0, _ TmpPic.ScaleWidth, TmpPic.ScaleHeight, SRCCOPY End Function
Wie bereits eingangs erwähnt, empfehlen wir Ihnen das Beispiels-Projekt zu anzuschauen. Hier ist sehr schön zu sehen, wie das AlphaBlending eingesetzt werden kann.