Results 1 to 40 of 716

Thread: [vb6]Alpha Image Control v2 - Final Update (15 Jan 2012)

Threaded View

  1. #11

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

    Re: [vb6]Alpha Image Control (PNG, AniGIFs, TIFF, & more)

    I've been asked twice now... "How do you apply multiple version 1.1 effects to an image?" Honestly, I don't know the answer. Can it be done in one call? The key function only accepts one effects handle. So, I don't think it can be done in one call. But since this control exposes all, a little GDI+ knowledge and some fun, we can do it

    Note: v1.1 of GDI+ required. Vista or better. See posts #1 thru #3 for a bit more on v1.1 of GDI+
    1. Add two alpha image controls to your form and one command button. Leave default control names
    2. Place an image into the 1st alpha image control. Size 2nd control to 1st control's size
    3. Copy and paste this code
    Code:
    Option Explicit
    
    Private Declare Function GdipGetImageGraphicsContext Lib "gdiplus.dll" (ByVal pImage As Long, ByRef graphics As Long) As Long
    Private Declare Function GdipDeleteGraphics Lib "gdiplus.dll" (ByVal mGraphics As Long) As Long
    Private Declare Function GdipGraphicsClear Lib "gdiplus.dll" (ByVal graphics As Long, ByVal pColor As Long) As Long
    
    Private Sub Command1_Click()
    
        ' Tip. If the image is to be rotated, grayscaled, or any other non v1.1 effects, do that
        ' either first or last via a call to SavePictureGDIplus and filling out a SAVESTRUCT.
    
        Dim cFX As GDIpEffects
        Set cFX = New GDIpEffects
        ' create the v1.1 effects you want to apply
        cFX.CreateBlurEffect 2, True
        cFX.CreateBrightnessContrastEffect 40, -10
        cFX.CreateTintEffect 180, 40
        ' call this makeshift function & pass the effects in order that you want them applied
        Set AlphaImgCtl2.Picture = MultiEffectToImage(AlphaImgCtl1.Picture, cFX, lvicBrightnessContrastFX, lvicTintFX, lvicBlurFX)
    
    End Sub
    
    
    Private Function MultiEffectToImage(Source As GDIpImage, FXclass As GDIpEffects, ParamArray Effects() As Variant) As GDIpImage
    
        If GDIplusTokenVersion < 1.1 Then Exit Function ' need to have right version for effects
    
        Dim X As Long, Index As Long
        Dim hGraphics As Long
        Dim cImages(0 To 1) As GDIpImage
        Dim SS As SAVESTRUCT
        
        ' want a graphics object for this function to work. Can't have one if image is paletted.
        ' Need a copy anyway, so might as well just create a saved version in the right bit depth
        If Source.UsesTransparency Then SS.ColorDepth = lvicConvert_TrueColor32bpp_ARGB Else SS.ColorDepth = lvicConvert_TrueColor24bpp
        Set cImages(0) = New GDIpImage
        If SavePictureGDIplus(Source, cImages(0), lvicSaveAsPNG, SS) = False Then Exit Function
        Set cImages(1) = LoadPictureGDIplus(cImages(0)) ' copy the one we just made
        
        For X = 0 To UBound(Effects)
            ' now toggle between the two images, rendering the effects
            If GdipGetImageGraphicsContext(cImages(Index Xor 1).Handle, hGraphics) = 0& Then
                GdipGraphicsClear hGraphics, 0&
                cImages(Index).Render 0&, , , , , , , , , , , hGraphics, FXclass.EffectsHandle(CLng(Effects(X)))
                GdipDeleteGraphics hGraphics
            Else
                Exit For
            End If
            Index = Index Xor 1
        Next
        If X > UBound(Effects) Then Set MultiEffectToImage = cImages(Index)
    
    End Function
    Edited: Another version, no GDI+ knowledge needed. Just needed to put my thinkin' cap back on.
    Code:
    Private Sub Command2_Click()
    
        ' Tip. If the image is to be rotated, grayscaled, or any other non v1.1 effects, do that
        ' either first or last via a call to SavePictureGDIplus and filling out a SAVESTRUCT.
    
        Dim SS As SAVESTRUCT, cImage As GDIpImage
        Set SS.RSS.Effects = New GDIpEffects
        Set cImage = New GDIpImage
        ' create the v1.1 effects you want to apply
        SS.RSS.Effects.CreateBrightnessContrastEffect 40, -10: SS.RSS.EffectType = lvicBrightnessContrastFX
        SavePictureGDIplus AlphaImgCtl1.Picture, cImage, lvicSaveAsPNG, SS
        ' ^^ using PNG as destination to avoid issues if original image format doesn't support colors/effects
        
        SS.RSS.Effects.CreateTintEffect 180, 40: SS.RSS.EffectType = lvicTintFX
        SavePictureGDIplus cImage, cImage, lvicSaveAsCurrentFormat, SS
        
        SS.RSS.Effects.CreateBlurEffect 2, True: SS.RSS.EffectType = lvicBlurFX
        SavePictureGDIplus cImage, cImage, lvicSaveAsCurrentFormat, SS
        
        ' any other effects? If not, then...
        Set AlphaImgCtl2.Picture = cImage
    
    End Sub
    The above code is an example only. You should provide more error checking as needed/wanted.

    And yet another method which is what I'd personally use if I wanted faster results.
    1. Create a GDI+ bitmap 2x height and same width of source
    2. Set clipping rect to top half, render source to top half using effects#1
    3. Remove clipping rect & set new clipping rect to bottom half, clear bottom half, render top half with effects#2 to bottom half
    4. Now continue toggling between top & bottom halves for remainder of effects, clear it before rendering
    5. Create new GDI+ bitmap same size as source. Render last drawn half to it & clean up.
    This method should be fastest I'd think. Oherwise, method 1 above will be faster than method 2 above because you aren't saving the image after each effect is rendered.
    Last edited by LaVolpe; Feb 18th, 2011 at 10:24 AM. Reason: removed extaneous api declaration
    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}

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
  •  



Click Here to Expand Forum to Full Width