Results 1 to 2 of 2

Thread: [RESOLVED] How to get alpha value of PNG and TIFF image?

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    7

    Resolved [RESOLVED] How to get alpha value of PNG and TIFF image?

    Hi guys, hope you can help me
    how to get aplha value of PNG and TIFF image?

    thanks in advance

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

    Re: How to get alpha value of PNG and TIFF image?

    Use GDI+ to load the PNG/TIFF. Then use GdipBitmapLockBits to get the bits in 32bpp ARGB format. The alpha value will be the 4th byte of each pixel.

    Forum search key words: GDIpBitmapLockBits

    Edited: Not many threads relating to this question. So I'll provide much of the answer here

    1. As mentioned, use GDI+ to load the PNG/TIFF. If needed search for examples using key words: GDI+ PNG
    2. Then use the following to retrieve the pixel B,G,R,Alpha values to a byte array
    GdipBitmapLockBits has similar functionality to GDI's GetDIBits/SetDIBits
    Code:
    ' API declarations first
    Private Declare Function GdipBitmapLockBits Lib "GdiPlus.dll" (ByVal mBitmap As Long, ByRef mRect As Any, ByVal mFlags As Long, ByVal mPixelFormat As Long, ByRef mLockedBitmapData As BitmapData) As Long
    Private Declare Function GdipBitmapUnlockBits Lib "GdiPlus.dll" (ByVal mBitmap As Long, ByRef mLockedBitmapData As BitmapData) As Long
    Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
    Private Type BitmapData              ' GDI+ lock/unlock bits structure
        Width As Long
        Height As Long
        stride As Long
        PixelFormat As Long
        Scan0Ptr As Long
        ReservedPtr As Long
    End Type
    Private Enum LockModeConstants
        ImageLockModeRead = &H1
        ImageLockModeWrite = &H2
        ImageLockModeUserInputBuf = &H4
    End Enum
    Private Const PixelFormat32bppARGB As Long = &H26200A 
    
    ' simple sample. The hImage is a reference to the handle you got when you loaded your PNG/TIFF
    
    Dim tmpBMPdata As BitmapData, bBits() As Byte
    If GdipBitmapLockBits(hImage, ByVal 0&, ImageLockModeRead, PixelFormat32bppARGB, tmpBMPdata) = 0 Then ' success
        ReDim bBits(0 To tmpBMPdata.Height * Abs(tmpBMPdata.Stride) - 1)
        If tmpBMPdata.Scan0Ptr > 0 Then
             CopyMemory bBits(0), ByVal tmpBMPdata.Scan0Ptr, UBound(bBits) + 1
        Else
             CopyMemory bBits(0), ByVal tmpBMPdata.Scan0Ptr + (tmpBMPdata.Height - 1&) * tmpBMPdata.stride, UBound(bBits) + 1
        End If
        GdipBitmapUnlockBits hImage, tmpBMPdata
    End If
    
    ' now you can loop thru your array; every 4th byte is the alpha value
    ' i.e., For X = 3 To UBound(bBits) Step 4: ' alpha is bBits(X) : Next X
    ' Note that if tmpBMPdata.Stride is a positive value, the array is the image top down; else it is bottom up
    Last edited by LaVolpe; Dec 30th, 2011 at 09:55 AM.
    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}

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