Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


In this article we will see how to use new AlphaBlend API which is only available in win 2k and later , Win 98 and later. In this example we have function called DoAlphablend and this function takes 3 arguments : source picturebox, destination picture box and AlphaVal. Alphavalue determine the transparancy of image.

Step-By-Step Example

- Create a standard exe project
- Add 2 picture box and 2 timers on the form1
- Assign picture property of both picture box and make sure both imagess are different.
- Copy and Paste the following code into form1 code window.

Click here to copy the following block
Const AC_SRC_OVER = &H0

Private Type BLENDFUNCTION
 BlendOp As Byte
 BlendFlags As Byte
 SourceConstantAlpha As Byte
 AlphaFormat As Byte
End Type

Private Declare Function AlphaBlend Lib "msimg32.dll" _
  (ByVal hdc As Long, _
  ByVal lInt As Long, _
  ByVal lInt As Long, _
  ByVal lInt As Long, _
  ByVal lInt As Long, _
  ByVal hdc As Long, _
  ByVal lInt As Long, _
  ByVal lInt As Long, _
  ByVal lInt As Long, _
  ByVal lInt As Long, _
  ByVal BLENDFUNCT As Long) As Long
  
Private Declare Sub RtlMoveMemory Lib "kernel32.dll" _
  (Destination As Any, _
  Source As Any, _
  ByVal Length As Long)

Dim BlendVal As Integer
Dim BF As BLENDFUNCTION, lBF As Long

'//Variable to hold images so we can swap
Dim tempPic1 As New StdPicture, tempPic2 As New StdPicture

Private Sub Form_Load()
    
  '//Load images from file
  'Picture2.Picture = LoadPicture(App.Path & "\" & "ash01.jpg")
  'Picture1.Picture = LoadPicture(App.Path & "\" & "ash02.jpg")
  
  'Set the graphics mode to persistent
  Picture1.AutoRedraw = True
  Picture2.AutoRedraw = True
  
  'API uses pixels
  Picture1.ScaleMode = vbPixels
  Picture2.ScaleMode = vbPixels
  
  'set the parameters
  'AlphaBlend the picture from Picture1 over the picture of Picture2
  BlendVal = 1
  Set tempPic1 = Picture1.Picture
  Set tempPic2 = Picture2.Picture
  
  Timer1.Interval = 200
  Timer2.Interval = 200
  
  Timer1.Enabled = True
  Timer2.Enabled = False
    
End Sub

Public Sub DoAlphablend(SrcPicBox As PictureBox, DestPicBox As PictureBox, AlphaVal As Integer)
  With BF
    .BlendOp = AC_SRC_OVER
    .BlendFlags = 0
    .SourceConstantAlpha = AlphaVal
    .AlphaFormat = 0
  End With
  'copy the BLENDFUNCTION-structure to a Long
  RtlMoveMemory lBF, BF, 4
  
  'AlphaBlend the picture from Picture1 over the picture of Picture2
  AlphaBlend DestPicBox.hdc, 0, 0, DestPicBox.ScaleWidth, DestPicBox.ScaleHeight, SrcPicBox.hdc, 0, 0, SrcPicBox.ScaleWidth, SrcPicBox.ScaleHeight, lBF
End Sub
Private Sub Timer1_Timer()
  Picture1.Refresh
  Picture2.Refresh

  BlendVal = BlendVal + 5
  If BlendVal >= 155 Then
    flag = True
    Timer1.Enabled = False
    Picture2.Picture = tempPic1
    Timer2.Enabled = True
    BlendVal = 1
  End If
  
  DoAlphablend Picture2, Picture1, BlendVal

  Me.Caption = CStr(BlendVal)
End Sub
Private Sub Timer2_Timer()
  Picture1.Refresh
  Picture2.Refresh

  BlendVal = BlendVal + 5
  If BlendVal >= 155 Then
    BlendVal = 1
    Timer1.Enabled = True
    Timer2.Enabled = False
    Picture2.Picture = tempPic2
  End If
  DoAlphablend Picture2, Picture1, BlendVal

  Me.Caption = CStr(BlendVal)
End Sub

- Now press F5 to run the project
- Observe that how image becomes semi-transperent.


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.