To give you an ad-free browsing experience, this website is completely funded by donations. If you use the information in this article to solve a problem, entertain yourself, or otherwise improve your life, please consider a small donation to help pay for server costs. Many thanks, and enjoy the site!

Graphics Code

Real-Time Image Blending/Transparency (VB6)

Real-time transparency has become so commonplace in modern games and PC applications that it’s almost taken for granted.  However, the specific formula that performs this now-ubiquitous effect is worth understanding.

Transparency is simply a weighted average between the color values of two pixels.  As an example, consider the following two colors:

Pure red, or RGB(255, 0, 0)

Pure blue, or RGB(0, 0, 255)

At 50% transparency – or a pure blending between these two colors – we would simply add the individual color amounts together and divide by two.  This gives us a new color -

Purple, or RGB(127, 0, 127)

Not entirely surprising, eh?

The effect is certainly more impressive when applied pixel-by-pixel across an entire image.  The provided code does exactly that using VB6, and an .exe is included for those merely interested in the effect itself.

Download code (144kb)

Squall and Rinoa meet a large flower (insert Garden joke of your choice :)

Squall and Rinoa meet a large flower (insert Garden joke of your choice :)

 

BSD License
All source code in the above zip file has been released under a BSD License.

 

Related articles:

Discussion (Oldest Comments Displayed First)

3 comments on “Real-Time Image Blending/Transparency (VB6)”

  1. your code is excelent… but i need ask these: how can i show 1 image transparency in UC(UserControl)?
    i’m sorry and thanks

    Posted by Joaquim | March 27, 2009, 5:51 pm
  2. Hi Joaquim – I’m afraid I don’t fully understand your question. The DIB code above should work on any control that has an hDC property. You’ll have to tweak the functions themselves a bit (as they currently accept Picture Boxes, then extract the hDC from there), but there is no reason that you can’t use DIB sections to modify the pixel data returned from any hDC.

    Posted by Tanner | April 9, 2009, 1:17 pm
  3. i was asking if i can do it in image(pixel by pixel) or in UC(object itself)?
    thanks

    Posted by Joaquim | November 18, 2009, 4:17 pm

Post a comment

Feel free to use any of these tags in your comment: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>