1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| Option Explicit
Private Type PNGHeader
pngType(7) As Byte
End Type
Private Type PNGChunkHeader
Length(3) As Byte
CType(3) As Byte
End Type
Private Type PNGChunkData
value As Byte
End Type
Private Type PNGImageData
PixelData As Byte
End Type
Private Type PNGImageHDData
WidthP(3) As Byte
HeightP(3) As Byte
BitDepthP As Byte
ColorP As Byte
CompressionP As Byte
FilterP As Byte
InterlaceP As Byte
End Type
Private Type PNGChunkCRC
value(3) As Byte
End Type
Private Sub Form_Load()
Dim x As Long, y As Long, index As Long
Dim iFileNum As Integer
Dim path As String
Dim n As Integer
iFileNum = FreeFile()
path = "C:\Users\bla.png"
Open path For Binary As iFileNum
'FileOpen(iFileNum, path, OpenMode.Binary) in VB.net
Dim dataFlag As Boolean
dataFlag = False
Dim DataLength As Integer
Dim PixelList() As Single
Dim pngHd As PNGHeader
Dim pngChHd As PNGChunkHeader
Dim pngChData() As PNGChunkData
Dim pngChCRC As PNGChunkCRC
Dim pngIHDRData As PNGImageHDData
Dim pngIDATData() As PNGImageData
Dim ChunkSize As Integer
Dim ChunkType As String
Dim ImageWidth As Integer
Dim ImageHeight As Integer
Get #iFileNum, , pngHd
While dataFlag = False
Get #iFileNum, , pngChHd
ChunkSize = Val(Hex(pngChHd.Length(0)) * 2 ^ (8 * 3)) + Val(Hex(pngChHd.Length(1)) * 2 ^ (8 * 2)) + Val(Hex(pngChHd.Length(2)) * 2 ^ (8 * 1)) + pngChHd.Length(3)
ChunkType = hex2ascii(Hex(pngChHd.CType(0))) & hex2ascii(Hex(pngChHd.CType(1))) & hex2ascii(Hex(pngChHd.CType(2))) & hex2ascii(Hex(pngChHd.CType(3)))
Select Case ChunkType
Case "IHDR"
ReDim pngChData(ChunkSize - 1)
Get #iFileNum, , pngIHDRData
Get #iFileNum, , pngChCRC
ImageWidth = Val(Hex(pngIHDRData.WidthP(0)) * 2 ^ (8 * 3)) + Val(Hex(pngIHDRData.WidthP(1)) * 2 ^ (8 * 2)) + Val(Hex(pngIHDRData.WidthP(2)) * 2 ^ (8 * 1)) + pngIHDRData.WidthP(3)
ImageHeight = Val(Hex(pngIHDRData.HeightP(0)) * 2 ^ (8 * 3)) + Val(Hex(pngIHDRData.HeightP(1)) * 2 ^ (8 * 2)) + Val(Hex(pngIHDRData.HeightP(2)) * 2 ^ (8 * 1)) + pngIHDRData.HeightP(3)
Case "IDAT"
ReDim pngIDATData(ChunkSize - 1)
Get #iFileNum, , pngIDATData
Get #iFileNum, , pngChCRC
DataLength = ChunkSize
dataFlag = True
Case Else
ReDim pngChData(ChunkSize - 1)
Get #iFileNum, , pngChData
Get #iFileNum, , pngChCRC
End Select
Wend
ReDim PixelList(ImageWidth * ImageHeight - 1)
index = 0
For n = 0 To ImageWidth * ImageHeight - 1
PixelList(n) = pngIDATData(index).PixelData * 2 ^ 8 + pngIDATData(index + 1).PixelData
index = index + 2
Next n
index = 0
For x = 1 To 5
For y = 1 To 5
Sheets("Sheet1").Cells(x, y) = PixelList(index)
index = index + 1
Next y
Next x
Close iFileNum
End Sub
Public Function hex2ascii(ByVal hextext As String) As String
Dim y As Integer
Dim value As String
For y = 1 To Len(hextext)
value = value & Chr(Val("&h" & Mid(hextext, y, 2)))
y = y + 1
Next y
hex2ascii = value
End Function |
Partager