Results 1 to 11 of 11

Thread: Draw a Gradient with Transparency [ALPHA] with BitBlt

  1. #1

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    Question Draw a Gradient with Transparency [ALPHA] with BitBlt

    Hi,

    Ok I do have a function, with BitBlt where it can draw Gradients, but its only from one colour to another. So e.g. From Red to Green.

    But how can I make it go from Red to Tranparency - [ALPHA] ?

    Can bitblt know how to not fully draw the pixel colour but vary its alpha level ?
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Draw a Gradient with Transparency [ALPHA] with BitBlt

    AlphaBlend API Might be helpful, but can you post the code you use to draw a gradient with Bitblt? maybe it can be modified to do you need.

  3. #3
    Addicted Member
    Join Date
    Jan 2009
    Location
    Norway
    Posts
    185

    Re: Draw a Gradient with Transparency [ALPHA] with BitBlt

    some1uk03

    Did you find any solution to this issue? I'm trying to make this work myself, but no luck so far..

  4. #4
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Draw a Gradient with Transparency [ALPHA] with BitBlt

    Okay this is an older thread but...

    BitBlt can handle a variety of bitwize operations but it cannot handle a graded transparency, AlphaBlend as jcis mentions can.

    GradientFill Api can create a gradient with an alpha channel.

    This is not something I have done but I am fairly confident the steps you would take is...

    Create a 32bit DIB (or preferably DDB if available)
    Use GradientFill to render the alpha gradient to the DIB/DDB.
    Use Alphablend to render the DIB/DDB to the target device.

    Although GradientFill supports alpha channels I don't think it can actually alphablend.

  5. #5
    Addicted Member
    Join Date
    Jan 2009
    Location
    Norway
    Posts
    185

    Re: Draw a Gradient with Transparency [ALPHA] with BitBlt

    Quote Originally Posted by Milk
    Use Alphablend to render the DIB/DDB to the target device.
    Ok.. I guess what I'm after, is to have a translucent to opaque gradient form. Such that the gradient goes from one color to translucent. The point beeing to see the background behind the form, not the form itself. Obviously different also from the normal form opacity, which is uniform over the whole form.

    Trying to alphablend gives me only a blend with the form background color. Even if it's sat on transparency key, the alphablend will still be between the picture and the background.

  6. #6

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    Re: Draw a Gradient with Transparency [ALPHA] with BitBlt

    Quite an old thread this one, but I'll post the solution up tomorrow morning, because I'm on another machine now.
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  7. #7
    Addicted Member
    Join Date
    Jan 2009
    Location
    Norway
    Posts
    185

    Re: Draw a Gradient with Transparency [ALPHA] with BitBlt

    Awsome. Looking forward to that

  8. #8
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Draw a Gradient with Transparency [ALPHA] with BitBlt

    I'm looking forward to see how some1uk03 did resolve this. What you are wanting is in a way quite different from the original question. It's one thing rendering an image with an alpha channel, it's another creating transparent windows.

    SetLayeredWindowAttributes can be used for a semi transparent windows but it can't do a gradient as such (i don't think.)

    I'm sure there is a way I just don't know what it is.

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Draw a Gradient with Transparency [ALPHA] with BitBlt

    I am curious also.

    SetLayeredWindowAttributes can make the form semi-transparent as mentioned, but it won't create the desired effect I fear. This is because if you do a semi-translucent gradient over the form, visually, it will go from the gradient color to the form's backcolor since the far end of the gradient will blend to the form background. SetLayeredWindowAttributes will then show this artifact also.

    So, an obvious solution would seem to use UpdateLayeredWindow which can perfectly satisify the requirement. But there is one major downside. Any window using that API cannot support child controls (no buttons, textboxes, etc). Though the form can contain these, they will never be seen, though they are there and active/clickable. This is a major issue.

    The most common workaround is to use 2 windows: 1 with just the background, using UpdateLayeredWindows. The other contains just controls and a color to be used with SetLayeredWindowAttributes' transparency key. These 2 windows are then overlayed with each other. Though this looks perfect, the issues come when moving one and keeping the other moving in-sync. Depending on PC speed, choppiness is common.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10
    Hyperactive Member su ki's Avatar
    Join Date
    Oct 2007
    Posts
    354

    Re: Draw a Gradient with Transparency [ALPHA] with BitBlt

    yes LaVolpe u r absolutely right i also had worked around on this issue and sync of two layers always create problem and the solution is to hide window contents while dragging.

  11. #11

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    Re: Draw a Gradient with Transparency [ALPHA] with BitBlt

    Milk & LaVolpe let your curiosity end : )

    Ok.. I guess what I'm after, is to have a translucent to opaque gradient form. Such that the gradient goes from one color to translucent.
    Well I guess I hadn't read that properly, but its not a Translucent form that I needed in my original proble, but rather a function which returns the ALPHA value of another colour:

    Code:
    Private Function BlendColor(ByVal oColorFrom As OLE_COLOR, _
                                ByVal oColorTo As OLE_COLOR, _
                                Optional ByVal Alpha As Long = 128) As Long
    
      Dim lCFrom As Long
      Dim lCTo   As Long
      Dim lSrcR  As Long
      Dim lSrcG  As Long
      Dim lSrcB  As Long
      Dim lDstR  As Long
      Dim lDstG  As Long
      Dim lDstB  As Long
    
       lCFrom = oColorFrom
       lCTo = oColorTo
       lSrcR = lCFrom And &HFF
       lSrcG = (lCFrom And &HFF00&) \ &H100&
       lSrcB = (lCFrom And &HFF0000) \ &H10000
       lDstR = lCTo And &HFF
       lDstG = (lCTo And &HFF00&) \ &H100&
       lDstB = (lCTo And &HFF0000) \ &H10000
    
       BlendColor = RGB(((lSrcR * Alpha) / 255) + ((lDstR * (255 - Alpha)) / 255), _
                   ((lSrcG * Alpha) / 255) + ((lDstG * (255 - Alpha)) / 255), _
                   ((lSrcB * Alpha) / 255) + ((lDstB * (255 - Alpha)) / 255))
    
    End Function
    However, I do still have something In mind.. but Ill get back to this thread soon.
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width