Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

transparency in image boxes.... 3

Status
Not open for further replies.

OrthoDocSoft

Programmer
May 7, 2004
291
0
0
US
I've seen this answered to the negative on this forum, but wanted to make sure that that was correct....

I have two or more images that I move around on a white background. Sometimes the images partially overlap each other. The images have "fuzzy" edges, which I have partially extracted and made transparent.

What I need to happen is to see the visable parts of each image through the transparent parts of the other, like a blend of the two pictures.

Can this be done in VB6?

Thanks

Ortho [lookaround]
 
>I've seen this answered to the negative on this forum

Where?

>which I have partially extracted and made transparent

In what sense transparent?

>to see the visable parts of each image through the transparent parts of the other

You mean one image has holes in it through which you'd like to see the image beneath?

>Can this be done in VB6?

Yes
 
Hi.

1. I assume you meant images and not picture boxes.
Picture boxes are windows and have an hWnd, and it is hard to make them tranparent. Images are windowless.

2. You can create icons (up to 128x128) with an icon creator like microangelo. There you can define transparent pixels.

3. When you place the images on the form you should see other images through the "holes" (tranparent pixels) of the other images.

4. Take care of the z-order!

SmallTalk
 
> and it is hard to make them tranparent

No, it isn't
 
Approaches making windows or controls with windows transparent.

1. Regions (slow and much code because you have to loop though your bitmap row after row and crate a region)

2. User Control and Mask (a lot of code too)

Win2000 and later versions support alpha blending - this is making pixels semitransparent.

I am a very lazy programmer and it is "hard" to write much code not to solve hard problems.

strongm:

If you are very assiduous then it's not hard for you.
 
:) SmallTalker, at the risk of stepping into a hornet's nest, I must observe that you might not be serving the best interests of the community at large to suggest that something is difficult, if you're making such a determination on the basis of your own laziness. After all, you surely don't want to have that laziness serve as a basis for examples of best practice, now do you?

Perhaps you will accept my inviation to have a change of heart, and in future assume a native assiduousness (which of course, you are free not to share) on the part of querents to whom you respond. You wouldn't want that laziness to be catching, after all.

All in good fun,

Bob
 
I've decided this is not worth the trouble (and BOY did I stir up some, LOL), but actually, it would be better if there was an interpolation of the overlapping pixels, such that:

white + white = white
white + light red = light pink
white + dark red = light red
light red + light red = darker red, etc.,

If THIS is easy, please enlighten me.

Thanks,

Ortho [lookaround]
 
Yes

And, as I said before, doing full transparency is easy. And doing holes in your image is easy.

No, really.

Here's how to get holes in more-or-less any image you like. For this example you'll need a form with a ImageList and a commandbutton. Load your two images into the ImageList at design time (for the sake of this example this means that the second image will be stretched to the same size as the first image). Then all you need is:
Code:
[blue]Option Explicit

Private Sub Command1_Click()
    Form1.Cls
    With ImageList1
        .MaskColor = vbWhite 'Desired transparent color for first image; you may want something different for your image
        .BackColor = .MaskColor
        PaintPicture ImageList1.ListImages(1).ExtractIcon, 0, 0
        .MaskColor = RGB(192, 192, 192) 'Desired transparent color for second image; again you may want something different for your image
        .BackColor = .MaskColor
        PaintPicture ImageList1.ListImages(2).ExtractIcon, 0, 0
    End With
End Sub[/blue]

For transparency add a second commandbutton, a picturebox and the following code to what we already have:
Code:
[blue]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 CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)


Private Sub Command2_Click()
    Dim BF As BLENDFUNCTION, lBF As Long

    Form1.ScaleMode = vbPixels ' API is in pixels
    Picture1.ScaleMode = vbPixels ' API is in pixels

    With BF
        .BlendOp = AC_SRC_OVER
        .BlendFlags = 0
        .SourceConstantAlpha = 128
        .AlphaFormat = 0
    End With

    Form1.Cls
    PaintPicture ImageList1.ListImages(1).Picture, 0, 0
    Picture1 = ImageList1.ListImages(2).Picture
    CopyMemory lBF, BF, 4
    'AlphaBlend the picture from Picture1 over the picture we've just painted to the form
    AlphaBlend Me.hdc, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, Picture1.hdc, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, lBF
End Sub[/blue]
 
That's a pretty cool snippet. Have another star on me Mr. Strongm.

***You can't change your past, but you can change your future***
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top