Hi guys, hope you can help me
how to get aplha value of PNG and TIFF image?
thanks in advance :):)
Printable View
Hi guys, hope you can help me
how to get aplha value of PNG and TIFF image?
thanks in advance :):)
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