출처 : http://www.taeyo.pe.kr/Forum/Content.aspx?SEQ=1328&TBL=KNOWHOW&PGN=8
Class ImageClass
Private m_Width
Private m_Height
Private m_ImageType
Private BinFile
Private BUFFERSIZE
Private objStream
Private Sub class_initialize()
BUFFERSIZE = 65535
' Set all properties to default values
m_Width = 0
m_Height = 0
m_Depth = 0
m_ImageType = Null
Set objStream = Server.CreateObject("ADODB.Stream")
End Sub
Private Sub class_terminate()
Set objStream = Nothing
End Sub
Public Property Get Width()
Width = m_Width
End Property
Public Property Get Height()
Height = m_Height
End Property
Public Property Get ImageType()
ImageType = m_ImageType
End Property
Private Function Mult(lsb, msb)
Mult = lsb + (msb * CLng(256))
End Function
Private Function BinToAsc(ipos)
BinToAsc = AscB(MidB(BinFile, (ipos+1), 1))
End Function
Public Sub LoadFilePath(strPath)
If InStr(strPath, ":") = 0 Then
strPath = Server.MapPath(strPath)
End If
objStream.Open
objStream.LoadFromFile(strPath)
BinFile = objStream.ReadText(-1)
End Sub
Public Sub LoadBinary(BinaryFile)
BinFile = BinaryFile
End Sub
Public Sub ImageRead
If BinToAsc(0) = 137 And BinToAsc(1) = 80 And BinToAsc(2) = 78 Then
' this is a PNG file
m_ImageType = "png"
' get bit depth
Select Case BinToAsc(25)
Case 0
' greyscale
Depth = BinToAsc(24)
Case 2
' RGB encoded
Depth = BinToAsc(24) * 3
Case 3
' Palette based, 8 bpp
Depth = 8
Case 4
' greyscale with alpha
Depth = BinToAsc(24) * 2
Case 6
' RGB encoded with alpha
Depth = BinToAsc(24) * 4
Case Else
' This value is outside of it's normal range, so we'll assume that this is not a valid file
m_ImageType = Null
End Select
If not IsNull(m_ImageType) Then
' if the image is valid then
' get the width
m_Width = Mult(BinToAsc(19), BinToAsc(18))
' get the height
m_Height = Mult(BinToAsc(23), BinToAsc(22))
End If
End If
If BinToAsc(0) = 71 And BinToAsc(1) = 73 And BinToAsc(2) = 70 Then
' this is a GIF file
m_ImageType = "gif"
' get the width
m_Width = Mult(BinToAsc(6), BinToAsc(7))
' get the height
m_Height = Mult(BinToAsc(8), BinToAsc(9))
' get bit depth
m_Depth = (BinToAsc(10) And 7) + 1
End If
If BinToAsc(0) = 66 And BinToAsc(1) = 77 Then
' this is a BMP file
m_ImageType = "bmp"
' get the width
m_Width = Mult(BinToAsc(18), BinToAsc(19))
' get the height
m_Height = Mult(BinToAsc(22), BinToAsc(23))
' get bit depth
m_Depth = BinToAsc(28)
End If
If IsNull(m_ImageType) Then
' if the file is not one of the above type then
' check to see if it is a JPEG file
Dim lPos : lPos = 0
Do
' loop through looking for the byte sequence FF,D8,FF
' which marks the begining of a JPEG file
' lPos will be left at the postion of the start
If (BinToAsc(lPos) = &HFF And BinToAsc(lPos + 1) = &HD8 _
And BinToAsc(lPos + 2) = &HFF) _
Or (lPos >= BUFFERSIZE - 10) Then Exit Do
' move our pointer up
lPos = lPos + 1
' and continue
Loop
lPos = lPos + 2
If lPos >= BUFFERSIZE - 10 Then Exit Sub
Do
' loop through the markers until we find the one
' starting with FF,C0 which is the block containing the
' image information
Do
' loop until we find the beginning of the next marker
If BinToAsc(lPos) = &HFF And BinToAsc(lPos + 1) _
<> &HFF Then Exit Do
lPos = lPos + 1
If lPos >= BUFFERSIZE - 10 Then Exit Sub
Loop
' move pointer up
lPos = lPos + 1
If (BinToAsc(lPos) >= &HC0 And BinToAsc(lPos) <= &HC3) Or _
(BinToAsc(lPos) >= &HC5 And BinToAsc(lPos) <= &HC7) Or _
(BinToAsc(lPos) >= &HC9 And BinToAsc(lPos) <= &HCB) Or _
(BinToAsc(lPos) >= &HCD And BinToAsc(lPos) <= &HCF) Then
Exit Do
End If
' otherwise keep looking
lPos = lPos + Mult(BinToAsc(lPos + 2), BinToAsc(lPos + 1))
' check for end of buffer
If lPos >= BUFFERSIZE - 10 Then Exit Sub
Loop
' If we've gotten this far it is a JPEG and we are ready
' to grab the information.
m_ImageType = "jpg"
' get the height
m_Height = Mult(BinToAsc(lPos + 5), BinToAsc(lPos + 4))
' get the width
m_Width = Mult(BinToAsc(lPos + 7), BinToAsc(lPos + 6))
' get the color depth
m_Depth = BinToAsc(lPos + 8) * 8
End If
End Sub
End Class
사용법입니다.
Dim Image
Set Image = new ImageClass
With Image
.LoadFilePath("가상경로 or 물리적 경로 어떤것을 입력하던 관계없습니다.")
.ImageRead
iType = .ImageType
iWidth = .Width
iHeight = .Height
End With
Set Imaeg = Nothing
또는...
Dim Image
Set Image = new ImageClass
With Image
Image.LoadBinary("바이너리로 읽었을때...")
.ImageRead
iType = .ImageType
iWidth = .Width
iHeight = .Height
End With
Set Imaeg = Nothing