Results 1 to 3 of 3

Thread: Can GDIplus be used to find out the designated Transparent Color in a GIF file?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    275

    Can GDIplus be used to find out the designated Transparent Color in a GIF file?

    We can use GDIplus to establish whether the palette in a file is of grayscale; I am curious whether there is a way to find out the designated Transparent Color if any in a GIF file, using GDIplus.

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,948

    Re: Can GDIplus be used to find out the designated Transparent Color in a GIF file?

    I'm sure it can, you just need to work out the code.

    In WIA 2.0 which wraps GDI+ you can get the info via the Properties collection of an ImageFile object:

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Dim PaletteVector As WIA.Vector
        Dim TranspIndex As Byte
        Dim RGBIndex As Long
        Dim ColorRef As Long
    
        AutoRedraw = True
        With New WIA.ImageFile
            .LoadFile "sample.gif"
            If .IsAlphaPixelFormat Then
                With .Properties
                    Set PaletteVector = .Item("20738").Value
                    TranspIndex = .Item("20740").Value
                End With
                RGBIndex = CLng(TranspIndex) * 3 + 1
                With PaletteVector
                    ColorRef = .Item(RGBIndex + 2) * &H10000 _
                           Or .Item(RGBIndex + 1) * &H100& _
                           Or .Item(RGBIndex)
                End With
                Print "TranspIndex ="; TranspIndex; " ColorRef = &H"; Hex$(ColorRef)
                Line Step(0, 0)-Step(240, 240), ColorRef, BF 'Paint a swatch.
            Else
                Print "Not transparent"
            End If
        End With
        AutoRedraw = False
    End Sub
    So all you have to do is work out the equivalent GDI+ Flat API calls.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    275

    Re: Can GDIplus be used to find out the Transparent Color in an animated GIF file?

    I've added the word "animated" to the title to make it more specific. For those who would like to try it (with GDIplus), the code below which is applicable to a single-frame GIF does not apply to an animated GIF:
    Code:
           'mPalette below is with the struct:
           'Private Type ColorPalette
           '    flags As Long
           '    count As Long
           '    Entries(0 To 255) As Long
           'End Type
    
        Call GdipGetImagePaletteSize(hImg, mSize)
        
         '12 - 8, at least 4 bytes for one color
        If mSize < 12 Then
             Call GdipDisposeImage(hImg)
             Exit Function
        End If
    
        Call GdipGetImagePalette(hImg, mPalette, mSize)
    
          'Then copy bytes in mPalette.Entries to an arrPal(), according to mSize. arrPal() is Long, for color with an apha byte.
    
        For i = 0 to UBound(arrPal)          'Can be up to 256 entries for a single-frame ordinary GIF file
               If arrPal(i) >= 1 Then
                     mPalIndex = i
                     mTranspColor = arrPal(i)
                     Exit For
               End If
        Next i
    
          'The above won't work for an animated GIF, even if you intend to obtain the transparent color for the first frame.
          'So for an animated GIF, we have to process the following before ascertaing the mSize
    
        CLSIDFromString StrPtr(FrameDimensionTime), uCLSID
        GdipImageGetFrameCount hImg, uCLSID, mPageCount
        ret = GdipImageSelectActiveFrame(hImg, uCLSID, inPageIndex)        'Say, inPageIndex = 0 as we intend to get the info of the first page
    
          'Now you obtain the arrPal() as earlier-said.  Then loop arrPal() - actually there won't be much to loop about, as there is only one entry (4 bytes) there.
          'The entry is to give you the index to the palette.
    
        For i = 0 to UBound(arrPal)
              If arrPal(i) = 0 Then
                   mPalIndex = i
                      'Only palette index obtainable
                      ' so we got the index, but where is the palette to go through to obtain the color value
                      'mTranspColor = arrPal(i)
                   Exit For
              End If
        Next i
    So we end up in a situation, for an animated GIF file, that we get a palette index, but where are the palette entries?

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