Fading A Bitmap / PictureBox

This short bit of code shows how to fade a picture inside a PictureBox using a few simple WinAPI methods.

Screen-shot

The code works by creating a memory compatible DC and Bitmap to that of the picture within the PictureBox. The PictureBox is then cleared and has it's background set to various shades of gray (eventually black) - the bitmap is then 'blitted' to the PictureBox and each pixel is 'AND'ed with the background colour to achieve the fade effect.


'
' Form1 with a PictureBox (picture1) and a command button (command1)
'
'
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 CreateCompatibleDC Lib "gdi32" _
    (byval hdc as Long) as Long
private Declare Function CreateCompatibleBitmap Lib "gdi32" _
    (byval hdc as Long, byval nWidth as Long, byval nHeight as Long) _
        as Long
private Declare Function DeleteDC Lib "gdi32" (byval hdc as Long) as Long
private Declare Function DeleteObject Lib "gdi32" _
    (byval hObject as Long) as Long
private Declare Function SelectObject Lib "gdi32" (byval hdc as Long, _
    byval hObject as Long) as Long
'
private Declare Sub Sleep Lib "kernel32" (byval dwMilliseconds as Long)
'
private Const SRCAND = &H8800C6
private Const SRCCOPY = &HCC0020
'
private Sub Command1_Click()
    Dim lDC as Long
    Dim lBMP as Long
    Dim W as Integer
    Dim H as Integer
    Dim lColor as Long
'    
    Screen.MousePointer = vbHourglass
'    
    W = ScaleX(Picture1.Picture.Width, vbHimetric, vbPixels)
    H = ScaleY(Picture1.Picture.Height, vbHimetric, vbPixels)
    '
    ' Create Memory Compatible Bitmap to that in Picture1
    '
    lBMP = CreateCompatibleBitmap(Picture1.hdc, W, H)
    '
    ' Create Compatible DC in memory
    '
    lDC = CreateCompatibleDC(Picture1.hdc)
    '
    ' Select the Bitmap into the memory DC
    '
    Call SelectObject(lDC, lBMP)
    BitBlt lDC, 0, 0, W, H, Picture1.hdc, 0, 0, SRCCOPY
    '
    ' Quickly clear the Picture in Picture1
    '
    Picture1 = LoadPicture("")
    
    for lColor = 255 to 0 step -3
        '
        ' set the backcolor to a gray scale -> black
        '
        Picture1.BackColor = RGB(lColor, lColor, lColor)
        '
        ' Copy the bitmap into the picturebox 'AND' with the backcolor
        '
        BitBlt Picture1.hdc, 0, 0, W, H, lDC, 0, 0, SRCAND
        '
        ' Pause for a bit
        '
        Sleep 15
    next
    '
    ' Clear up our DC's and Bitmaps
    '
    Call DeleteDC(lDC)
    Call DeleteObject(lBMP)
    Screen.MousePointer = vbDefault
'    
End Sub
'

Download Zipped Project Files (3k)



Comments

  • Question

    Posted by Legacy on 03/13/2003 12:00am

    Originally posted by: saeed

    Good STuff

    How would you go about gradually fading the forground to the color of back group ?

    Reply
  • cool code

    Posted by Legacy on 03/07/2003 12:00am

    Originally posted by: sadashiva

    any modification to the code so that the same effect can be obtained using img control

    Reply
  • nice and useful code

    Posted by Legacy on 07/04/2002 12:00am

    Originally posted by: Ranganath

    Very good and intresting code

    Reply
  • for image control

    Posted by Legacy on 12/01/2000 12:00am

    Originally posted by: vishal

    the demo is very interesting and can the effect be achieved on the image control and how?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: March 19, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT The 2015 Enterprise Mobile Application Survey asked 250 mobility professionals what their biggest mobile challenges are, how many employees they are equipping with mobile apps, and their methods for driving value with mobility. Join Dan Woods, Editor and CTO of CITO Research, and Alan Murray, SVP of Products at Apperian, as they break down the results of this survey and discuss how enterprises are using mobile application management and private …

  • On-demand Event Event Date: February 12, 2015 The evolution of systems engineering with the SysML modeling language has resulted in improved requirements specification, better architectural definition, and better hand-off to downstream engineering. Agile methods have proven successful in the software domain, but how can these methods be applied to systems engineering? Check out this webcast and join Bruce Powel Douglass, author of Real-Time Agility, as he discusses how agile methods have had a tremendous …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date