[RESOLVED] Transparency AND Alphablend-VBForums
Results 1 to 5 of 5

Thread: [RESOLVED] Transparency AND Alphablend

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2015
    Posts
    25

    Resolved [RESOLVED] Transparency AND Alphablend

    Hi, i imagine it would be nice and usefull effect if i make those two ways of image rendering possible at same time. So i have this code which makes picture transparency possible:
    Code:
    Option Explicit
    Dim xMOVE
    Private Declare Function TransparentBlt Lib "msimg32.dll" (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 crTransparent As Long) As Boolean
    Private Sub Form_Load()
    Picture1.AutoSize = True
        Picture1.ScaleMode = vbPixels
        Picture2.ScaleMode = vbPixels
        Picture1.AutoRedraw = True
        Picture2.AutoRedraw = True
        Picture3.ScaleMode = vbPixels
        Picture3.AutoRedraw = True
    End Sub
    Private Sub Timer1_Timer()
    xMOVE = xMOVE + 1
    'creating background from picture2 on picture3
    Picture3.PaintPicture Picture2.Image, 0, 0, ScaleWidth, ScaleHeight, 0, 0, ScaleWidth, ScaleHeight
    'putting picture1 with transparency on picture3
    TransparentBlt Picture3.hdc, xMOVE, 0, Picture3.ScaleWidth, Picture3.ScaleHeight, Picture1.hdc, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, RGB(255, 0, 0)
    'sending merged pictures from picture3 to form1
    Form1.PaintPicture Picture3.Image, 0, 0, ScaleWidth, ScaleHeight, 0, 0, ScaleWidth, ScaleHeight
    End Sub
    or

    Code:
    Option Explicit
    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
    Dim a, x
    Private Sub Form_Load()
    x = 300
    End Sub
    Private Sub Form_Resize()
    picBuffer.Move 0, 0, ScaleWidth, ScaleHeight
    End Sub
    Private Sub Timer1_Timer()
    x = x - 1
    End Sub
    Private Sub Timer3_Timer()
    'creating background
    BitBlt picBuffer.hDC, 0, 0, ScaleWidth, ScaleHeight, frmBitBlt.Land.hDC, 0, 0, vbSrcCopy
    'putting picture with transparency-using mask on backgroud
    BitBlt picBuffer.hDC, x, 80, picture5.Width, picture5.Height, frmBitBlt.pictureMask.hDC, 0, 0, vbSrcAnd
    BitBlt picBuffer.hDC, x, 80, picture5.Width, picture5.Height, frmBitBlt.picture5.hDC, 0, 0, vbSrcPaint
    'sending all to form picture
    BitBlt hDC, 0, 0, ScaleWidth, ScaleHeight, picBuffer.hDC, 0, 0, vbSrcCopy
    End Sub
    and code for alpha blending making picture semi-transparent:

    Code:
    Option Explicit
    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)
    Private Sub Form_Load()
        Dim BF As BLENDFUNCTION, lBF As Long
        Picture1.AutoRedraw = True
        Picture2.AutoRedraw = True
        Picture1.ScaleMode = vbPixels
        Picture2.ScaleMode = vbPixels
        With BF
            .BlendOp = AC_SRC_OVER
            .BlendFlags = 0
            .SourceConstantAlpha = 128
            .AlphaFormat = 0
        End With
        RtlMoveMemory lBF, BF, 4
        AlphaBlend Picture2.hdc, 0, 0, Picture2.ScaleWidth, Picture2.ScaleHeight, Picture1.hdc, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, lBF
    End Sub
    Problem now is how i could display picture with transparent color (border of thing\sprite on picture) And semi-transparent of what is left visible (thing\sprite itself) at same time?
    Any idea how make it possible?

  2. #2
    PowerPoster
    Join Date
    Jun 2013
    Posts
    3,105

    Re: Transparency AND Alphablend

    Quote Originally Posted by Krzysztof# View Post
    Problem now is how i could display picture with transparent color (border of thing\sprite on picture) And semi-transparent of what is left visible (thing\sprite itself) at same time?
    When the Source-Image does contain an Alpha-Channel already, then
    the AlphaBlend-API is entirely sufficient to do both "at the same time".

    Any extra-On/Off-Masking-Tricks are not necessary at all - a PNG which contains a proper Alpha-Channel
    will "mask itself", when rendered with "full Opacity" using the AlphaBlend-API - and when you then
    reduce the Opacity-Param in the AlphaBlend-call, it will respect this setting as well (in addition to the Masking).

    Olaf

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2015
    Posts
    25

    Re: Transparency AND Alphablend

    Thanks for suggestion but it seems is not so simple to load png files into vb6. Fortunately i have found solution on this forum i suppose here:
    http://www.vbforums.com/showthread.p...round-a-sphere
    I will test it and looks like answer to my problem.

  4. #4
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    290

    Re: Transparency AND Alphablend

    you can use gdi+ to load a raster png. theres a lot of examples on the forum, just search GDI PNG and you will find what you need.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2015
    Posts
    25

    Re: Transparency AND Alphablend

    Yes, i have found few examples here of what i need, but all of them too elaborated\complicated for my understanding to extract exact code for use:/ The simplest way to accomplish i have found here: http://www.freevbcode.com/ShowCode.asp?ID=4645 So for now im ok with it, much thanks for your time.

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.