Author : Amr zakaria zaki
Date Submitted : 10/7/2006
Category : Graphics
Compatibility : VB 6,VB 5,VB 3
This code has been accessed 6071 times.
Task : To convert a image to array of byte's and add a header for it.
Declarations
Code
Option Explicit
' Date : 05/10/2006.
' Time : 08:10:00 PM.
' Created by : Amr zakaria zaki.
' Work's with VB 0.6 and VB 0.5.
' To Read picture and convert it to byte's.
' E-Mail : amrzakaria73@gmail.com , amrzakaria73@yahoo.com
'***********************************************************************
' 1 = clsImage
' 1 = Function , 2 = Sub , 3 = Property Get , 4 = Property Let
' 1 = ConvertRGB => Function
' Private = 0 , Public = 1
'-------- My new UDT
' Regsvr32 C:\WINDOWS\SYSTEM\Cam.dll
'? len("00/00/0000") + len("00:00:00") + len(String$(255," " )) + len("000.000.000.000") + len("32000")+ len("32000")
' picRead.PaintPicture picCls.Image, 0, 0, pic.Width + 200, pic.Height + 150
Private Type Image_To_Byte
Msg As String * 254
IDate As String * 10
ITime As String * 8
PortMsg As Long
PortData As Long
MyIP As String * 15
End Type
Private Type RGB_
Rd As Byte
Gn As Byte
Bl As Byte
End Type
Private Type ObjectPicture
pPicture As Object
ReadByte() As Byte
ReadByteIndex As Long
HeaderByte() As Byte
Header As Image_To_Byte
RHeade As Image_To_Byte
End Type
'---------------------
Private Type BITMAP
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type
'-------- API Calling
'Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
'---------------------
'To fire this event, use RaiseEvent with the following syntax:
'RaiseEvent clsErr[(arg1, arg2, ... , argn)]
Public Event clsErr(ByVal vSouce As String, vErrId As String, ByVal vErrMsg As String)
Public Event clsMsg(ByVal vSouce As String, vMsg As String)
'---------------------
Private pObj As ObjectPicture
Public Property Get GUI_Picture() As Object
' 1.3.1.2 = clsImage.Property_Get.GUI_Picture
GUI_Picture = pObj.pPicture
RaiseEvent clsMsg("1.3.1.2", "Object is returned.")
End Property
Public Property Let GUI_Picture(ByVal vData As Object)
' 1.4.1.3 = clsImage.Property_Let.GUI_Picture
On Error GoTo ErrGUI_Picture
With vData
.ScaleMode = vbPixels
.AutoRedraw = True
.Width = 100
.Height = 66
' .PaintPicture TmpPic, 0, 0, 100, 66
End With
''-------------------------
Set pObj.pPicture = vData
pObj.pPicture.ScaleMode = vbPixels ' I have to use pixel size for video confronc
If ReadPixels2() <> True Then
Err.Raise 143, "1.4.3", "Can not read Pixel from image."
End If
'--------------------------
RaiseEvent clsMsg("1.4.1.3", "Object is passed.")
Exit Property
ErrGUI_Picture:
Set pObj.pPicture = Nothing
RaiseEvent clsErr("1.4.1.3", Err.Number, Err.Description)
Err.Clear
End Property
Private Function ReadPixels2() As Boolean
'1.1.0.8 = clsImage.Function.ReadPixels2
On Error GoTo ErrReadPixels2
Dim PicInfo As BITMAP
Dim PicBits() As Byte
GetObject pObj.pPicture.Image, Len(PicInfo), PicInfo
ReDim PicBits(0 To PicInfo.bmWidth * PicInfo.bmHeight * 4) As Byte
GetBitmapBits pObj.pPicture.Image, UBound(PicBits), PicBits(1)
DoEvents
pObj.ReadByteIndex = UBound(PicBits)
pObj.ReadByte = PicBits()
ReadPixels2 = True
Erase PicBits()
RaiseEvent clsMsg("1.1.0.8", "Read Image as byte's.")
Exit Function
ErrReadPixels2:
Erase PicBits()
ReadPixels2 = False
RaiseEvent clsErr("1.1.0.8", Err.Number, Err.Description)
Err.Clear
End Function
Public Function GUI_ShowPicture(Optional ByVal vObject As Object = Nothing, _
Optional ByVal UseHeader As Boolean = True) As Boolean
' 1.1.1.4 = clsImage.Function.GUI_ShowPicture
On Error GoTo ErrGUI_ShowPicture
Dim HB() As Byte, DB() As Byte
Dim L As Long, C As Long
Dim PicInfo As BITMAP
Dim x As Image_To_Byte
'588 , 297
'C = 0
'C = 9
'C = 10
'C = 17
'C = 18
'C = 271
'C = 272
'C = 286
'C = 287
'C = 291
'C = 292
'C = 296
C = 0
For L = 0 To UBound(pObj.HeaderByte)
If L <= 296 Then
ReDim Preserve HB(L)
HB(L) = pObj.HeaderByte(L)
Else
ReDim Preserve DB(C)
DB(C) = pObj.HeaderByte(L)
C = C + 1
End If
' DoEvents
Next L
'--------------------------
If UseHeader = True Then
pObj.Header.IDate = ReadByte_From_To(pObj.HeaderByte(), 0, 9)
pObj.Header.ITime = ReadByte_From_To(pObj.HeaderByte(), 10, 17)
pObj.Header.Msg = Trim(ReadByte_From_To(pObj.HeaderByte(), 18, 271))
pObj.Header.MyIP = ReadByte_From_To(pObj.HeaderByte(), 272, 286)
If IsNumeric(Trim(ReadByte_From_To(pObj.HeaderByte(), 287, 291))) = True Then
pObj.Header.PortData = Trim(ReadByte_From_To(pObj.HeaderByte(), 287, 291))
Else
pObj.Header.PortData = 0
End If
If IsNumeric(Trim(ReadByte_From_To(pObj.HeaderByte(), 292, 296))) = True Then
pObj.Header.PortMsg = Trim(ReadByte_From_To(pObj.HeaderByte(), 292, 296))
Else
pObj.Header.PortMsg = 0
End If
End If
'--------------------------
'Debug.Print "clsImage.GUI_ShowPicture2 = ", UBound(HB), UBound(DB), UBound(pObj.HeaderByte), LenB(pObj.Header)
'Stop
'CopyMemory pObj.Header, HB(0), LenB(pObj.Header)
'CopyMemory X, HB(0), LenB(X)
'pObj.Header = X
'Stop
'----------------
vObject.ScaleMode = vbPixels ' I have to use pixel size for video confronc
SetBitmapBits vObject.Image, UBound(DB), DB(1)
vObject.Refresh
'----------------
GUI_ShowPicture = True
Erase HB()
Erase DB()
RaiseEvent clsMsg("1.1.1.4", "Image displed on its owner.")
Exit Function
ErrGUI_ShowPicture:
GUI_ShowPicture = False
Erase HB()
Erase DB()
RaiseEvent clsErr("1.1.1.4", Err.Number, Err.Description)
Err.Clear
End Function
Private Function ConvertRGB(ByVal lColour As Long) As RGB_
' 1.1.0.1 = clsImage.Function.ConvertRGB
On Error GoTo ErrConvertRGB
Dim iRed As Integer, iGreen As Integer, iBlue As Integer
lColour = lColour And &HFFFFFF
ConvertRGB.Rd = lColour And &HFF
ConvertRGB.Gn = (lColour \ &H100) And &HFF
ConvertRGB.Bl = lColour \ &H10000
RaiseEvent clsMsg("1.1.0.1", "Color converted to (R,G,B).")
Exit Function
ErrConvertRGB:
ConvertRGB.Bl = 0: ConvertRGB.Gn = 0: ConvertRGB.Rd = 0
RaiseEvent clsErr("1.1.0.1", Err.Number, Err.Description)
Err.Clear
End Function
Private Sub Class_Initialize()
'1.2.0.17 = clsImage.Sub.Class_Initialize
pObj.RHeade.IDate = Date
pObj.RHeade.ITime = Format$(Time, "hh:mm:ss")
pObj.RHeade.Msg = "Noting"
pObj.RHeade.MyIP = "000.000.000.000"
pObj.RHeade.PortData = 2777
pObj.RHeade.PortMsg = 2888
RaiseEvent clsMsg("1.2.0.17", "Class Initialize.")
End Sub
Private Sub Class_Terminate()
'1.2.0.18 = clsImage.Sub.Class_Terminate
Erase pObj.ReadByte()
Erase pObj.HeaderByte()
pObj.ReadByteIndex = 0
Set pObj.pPicture = Nothing
RaiseEvent clsMsg("1.2.0.18", "Class Terminate.")
End Sub
Public Function InfoAboutMe(Optional ByVal vDate As String = "00/00/0000", _
Optional ByVal vTime As String = "00:00:00", _
Optional ByVal vMsg As String = "", _
Optional ByVal vMyIP As String = "000.000.000.000", _
Optional ByVal vPortData As Long = 0, _
Optional ByVal vPortMsg As Long = 0) As Boolean
'1.1.1.5 = clsImage.Function.InfoAboutMe
On Error GoTo ErrInfoAboutMe
Dim TB() As Byte
Dim TH() As Byte
Dim L As Long
Dim C As Long
'------ I have to send data from picture or form to my UDT.
If IsDate(vDate) = True Then
pObj.RHeade.IDate = FormatDateTime(vDate, vbShortDate)
Else
pObj.RHeade.IDate = FormatDateTime(Date, vbShortDate)
End If
'---------------------------
If IsDate(vTime) = True Then
pObj.RHeade.ITime = Format$(vTime, "hh:mm:ss")
Else
pObj.RHeade.ITime = Format$(Time, "hh:mm:ss")
End If
'---------------------------
pObj.RHeade.Msg = vMsg
pObj.RHeade.MyIP = vMyIP
pObj.RHeade.PortData = vPortData
pObj.RHeade.PortMsg = vPortMsg
'------------- 588 old , new 297
'ReDim TB(LenB(pObj.Header)) As Byte
'Call CopyMemory(TB(0), pObj.RHeade, LenB(pObj.RHeade))
TB = UDT_To_Byte(pObj.RHeade.IDate, pObj.RHeade.ITime, vMsg, vMyIP, vPortData, vPortMsg)
'-----------------------------------
ReDim TH(UBound(TB) + pObj.ReadByteIndex + 1)
C = 0
For L = 0 To (UBound(TB) + pObj.ReadByteIndex + 1)
If L <= UBound(TB) Then
TH(L) = TB(L)
Else
TH(L) = pObj.ReadByte(C)
C = C + 1
End If
' DoEvents
Next L
'-------------
pObj.HeaderByte = TH()
InfoAboutMe = True
'-------------
Erase TH()
Erase TB()
RaiseEvent clsMsg("1.1.1.5", "Info passed to the class.")
Exit Function
ErrInfoAboutMe:
InfoAboutMe = False
Erase TH()
Erase TB()
RaiseEvent clsErr("1.1.1.5", Err.Number, Err.Description)
Err.Clear
End Function
Public Property Get RH_IP() As String
'1.3.1.11 = clsImage.Property_Get.RH_IP
RH_IP = pObj.Header.MyIP
RaiseEvent clsMsg("1.3.1.11", "IP returned.")
End Property
Public Property Get RH_Msg() As String
'1.3.1.12 = clsImage.Property_Get.RH_Msg
RH_Msg = pObj.Header.Msg
RaiseEvent clsMsg("1.3.1.12", "Msg returned.")
End Property
Public Property Get RH_Date() As String
'1.3.1.10 = clsImage.Property_Get.RH_Date
RH_Date = pObj.Header.IDate
RaiseEvent clsMsg("1.3.1.10", "Date returned.")
End Property
Public Property Get RH_Time() As String
'1.3.1.15 = clsImage.Property_Get.RH_Time
RH_Time = pObj.Header.ITime
RaiseEvent clsMsg("1.3.1.15", "Time returned.")
End Property
Public Property Get RH_PortMsg() As Long
'1.3.1.14 = clsImage.Property_Get.RH_PortMsg
RH_PortMsg = pObj.Header.PortMsg
RaiseEvent clsMsg("1.3.1.14", "Date returned.")
End Property
Public Property Get RH_PortData() As Long
'1.3.1.13 = clsImage.Property_Get.RH_PortData
RH_PortData = pObj.Header.PortData
RaiseEvent clsMsg("1.3.1.13", "Date returned.")
End Property
Private Function UDT_To_Byte(Optional ByVal vDate As String = "00/00/0000", _
Optional ByVal vTime As String = "00:00:00", _
Optional ByVal vMsg As String = "", _
Optional ByVal vMyIP As String = "000.000.000.000", _
Optional ByVal vPortData As Long = 0, _
Optional ByVal vPortMsg As Long = 0) As Byte()
'1.1.0.16 = clsImage.Function.UDT_To_Byte
On Error GoTo ErrUDT_To_Byte
Dim L As Integer
Dim C As Integer
Dim Tmp() As Byte
Dim TP As String
' 297
C = 0
' Debug.Print "C= "; C
For L = 1 To Len(vDate)
ReDim Preserve Tmp(C)
Tmp(C) = CByte(Asc(Mid(vDate, L, 1)))
' Debug.Print Tmp(C), Chr(Tmp(C)), L, Len(vDate)
C = C + 1
Next L
' Debug.Print "C= "; C - 1
'--------------------------------------
' Debug.Print "C= "; C
For L = 1 To Len(vTime)
ReDim Preserve Tmp(C)
Tmp(C) = CByte(Asc(Mid(vTime, L, 1)))
' Debug.Print Tmp(C), Chr(Tmp(C)), L, Len(vDate)
C = C + 1
Next L
' Debug.Print "C= "; C - 1
'--------------------------------------
If Len(vMsg) < 254 Then
vMsg = vMsg & String$((254 - Len(vMsg)), " ")
End If
'----
' Debug.Print "C= "; C
For L = 1 To Len(vMsg)
ReDim Preserve Tmp(C)
Tmp(C) = CByte(Asc(Mid(vMsg, L, 1)))
' Debug.Print Tmp(C), L, Len(vMsg)
C = C + 1
Next L
' Debug.Print "C= "; C - 1
'--------------------------------------
If Len(vMyIP) < 15 Then
vMyIP = vMyIP & String$((15 - Len(vMyIP)), " ")
End If
'----
' Debug.Print "C= "; C
For L = 1 To Len(vMyIP)
ReDim Preserve Tmp(C)
Tmp(C) = CByte(Asc(Mid(vMyIP, L, 1)))
' Debug.Print Tmp(C), L, Len(vMyIP)
C = C + 1
Next L
' Debug.Print "C= "; C - 1
'--------------------------------------
TP = CStr(vPortData)
'-----------
If Len(TP) < 5 Then
TP = String$((5 - Len(TP)), "0") & TP
End If
'-----------
' Debug.Print "C= "; C
For L = 1 To Len(TP)
ReDim Preserve Tmp(C)
Tmp(C) = CByte(Asc(Mid(TP, L, 1)))
' Debug.Print Tmp(C), L, Len(TP)
C = C + 1
Next L
' Debug.Print "C= "; C - 1
'--------------------------------------
TP = CStr(vPortMsg)
'-----------
If Len(TP) < 5 Then
TP = String$((5 - Len(TP)), "0") & TP
End If
'-----------
' Debug.Print "C= "; C
For L = 1 To Len(TP)
ReDim Preserve Tmp(C)
Tmp(C) = CByte(Asc(Mid(TP, L, 1)))
' Debug.Print Tmp(C), L, Len(TP)
C = C + 1
Next L
' Debug.Print "C= "; C - 1
UDT_To_Byte = Tmp()
Erase Tmp()
RaiseEvent clsMsg("1.1.0.16", "Data type's converted to byte's.")
Exit Function
ErrUDT_To_Byte:
ReDim Tmp(0)
UDT_To_Byte = Tmp()
Erase Tmp()
RaiseEvent clsErr("1.1.0.16", Err.Number, Err.Description)
Err.Clear
End Function
Private Function ReadByte_From_To(ByRef vByte() As Byte, ByVal vStart As Integer, ByVal vEnd As Integer) As String
'1.1.0.7 = clsImage.Function.ReadByte_From_To
On Error GoTo ErrReadByte_From_To
Dim C As Integer
Dim Tmp As String
For C = vStart To vEnd
Tmp = Tmp & Chr$(vByte(C))
Next C
ReadByte_From_To = Tmp
RaiseEvent clsMsg("1.1.0.7", "Image displed on its owner.")
Exit Function
ErrReadByte_From_To:
ReadByte_From_To = "Error"
RaiseEvent clsErr("1.1.0.7", Err.Number, Err.Description)
Err.Clear
End Function
Public Function Return_Header_Byte() As Byte()
'1.1.1.9 = clsImage.Function.Return_Header_Byte
Return_Header_Byte = pObj.HeaderByte()
RaiseEvent clsMsg("1.1.1.9", "Return the header as byte's.")
End Function
Public Function Read_Header_Byte(ByRef vByte() As Byte) As Boolean
'1.1.1.6 = clsImage.Function.Read_Header_Byte
pObj.HeaderByte = vByte()
Read_Header_Byte = True
RaiseEvent clsMsg("1.1.1.6", "Import the header as byte's.")
End Function
Public Function Regsvr32_clsImage(Optional ByVal vUser As Boolean = False) As Boolean
'1.1.1.19 = clsImage.Function.Regsvr32_clsImage
On Error GoTo ErrRegsvr32_clsImage
Dim Tmp As String
If vUser = True Then
Tmp = "Regsvr32 " & App.Path & "\" & App.EXEName & ".dll"
Call Shell(Tmp)
' Debug.Print App.Path, App.EXEName
Regsvr32_clsImage = True
Else
Regsvr32_clsImage = False
End If
RaiseEvent clsMsg("1.1.1.19", "To [Regsvr32] My Class.")
Exit Function
ErrRegsvr32_clsImage:
Regsvr32_clsImage = False
RaiseEvent clsErr("1.1.1.19", Err.Number, Err.Description)
Err.Clear
End Function
Public Function Image_Height_Width() As String
'1.1.1.20 = clsImage.Function.Image_Height_Width
Image_Height_Width = "Image : Height = 66 , Width = 100"
RaiseEvent clsMsg("1.1.1.20", "Return the size of picture.")
End Function