Hi again Peter,
I have attempted to use this hashing code in the following scenario:
Get an image, base64 encode and write to xml file.
Reverse this process to get image file back.
Compare hashes of before and after - they do not match... image looks
identical visually, and has same filesize and other properties... can you
suggest a way I can store these images to an XML file and get the exact same
file back when I extract the image again...?
The XML code is something like this:
Private Sub WriteXMLImage(B yVal strSourceFilePa thAndName As String,
Optional ByVal strTargetFilePa thAndName As String = "")
If strSourceFilePa thAndName <> "" Then
Dim strSourceExtens ion As String = ""
strSourceExtens ion = strSourceFilePa thAndName.Subst ring
(strSourceFileP athAndName.Leng th - 3)
'Destination filename not supplied so make one up
If strTargetFilePa thAndName = "" Then
strTargetFilePa thAndName =
strSourceFilePa thAndName.Subst ring(0, strSourceFilePa thAndName.Lengt h
- 3)
& "xml"
End If
Dim xmlTextWriter As New XmlTextWriter
(strTargetFileP athAndName, Encoding.UTF8)
xmlTextWriter.F ormatting = Formatting.Inde nted
xmlTextWriter.W riteStartDocume nt()
' Copy the picture into a MemoryStream.
Dim memory_stream As New MemoryStream
Dim objImageOrigina l As Image
objImageOrigina l = New Bitmap(strSourc eFilePathAndNam e)
'objImageOrigin al = objImageOrigina l.FromFile
(strSourceFileP athAndName)
objImageOrigina l.Save(memory_s tream, GetFormat
(strSourceExten sion))
' Copy the MemoryStream data into a Byte array.
Dim objBytes(memory _stream.Length - 1) As Byte
objBytes = memory_stream.T oArray()
' Make a Picture element.
xmlTextWriter.W riteStartElemen t("Picture")
xmlTextWriter.W riteAttributeSt ring("Encoding" ,
Nothing,
"Base64")
' Save Image details and encoding as Attributes
xmlTextWriter.W riteAttributeSt ring("Bytes",
objBytes.Length )
xmlTextWriter.W riteAttributeSt ring("Height",
objImageOrigina l.Height)
xmlTextWriter.W riteAttributeSt ring("Width",
objImageOrigina l.Width)
xmlTextWriter.W riteAttributeSt ring("Type",
strSourceExtens ion)
xmlTextWriter.W riteBase64(objB ytes, 0, objBytes.Length )
xmlTextWriter.W riteEndElement( )
xmlTextWriter.W riteEndDocument ()
xmlTextWriter.C lose()
objImageOrigina l.Dispose()
MsgBox("XML file created successfully ! ",
MsgBoxStyle.Inf ormation, "Success !")
Else
MsgBox("A target file name must be supplied.",
MsgBoxStyle.OKO nly + MsgBoxStyle.Exc lamation, "Error")
Exit Sub
End If
End Sub
Private Sub ReadXMLImage(By Val strSourceFilePa thAndName As String)
If strSourceFilePa thAndName <> "" Then
Dim objImageOrigina l As Image
Dim blnValidXML As Boolean = False
Dim strImageExtensi on As String = ""
Dim strTargetFilePa thAndName As String = ""
' Load the picture from XML
Dim xmlTextReader As New XmlTextReader
(strSourceFileP athAndName)
' Skip until we find the Picture node.
Do While xmlTextReader.R ead()
' See if this is the Picture node.
If xmlTextReader.N ame = "Picture" Then
blnValidXML = True
' Allocate room for the byte data and get all
attribute
values.
Dim intNumBytes As Integer =
xmlTextReader.G etAttribute
("Bytes")
Dim h As Integer =
xmlTextReader.G etAttribute("He ight")
Dim w As Integer =
xmlTextReader.G etAttribute("Wi dth")
strImageExtensi on =
xmlTextReader.G etAttribute("Ty pe")
'Refresh the target file according to parameters
read
from XML file Type Attribute
strTargetFilePa thAndName = Replace
(strSourceFileP athAndName, ".xml", "New." &
strImageExtensi on)
'lbl_Message.Te xt = "Source File : " &
strSourceFilePa thAndName & vbCrLf & "Target File : " &
strTargetFilePa thAndName
Dim bytes(intNumByt es - 1) As Byte
' Translate the encoded data back into byte data.
Select Case xmlTextReader.G etAttribute
("Encoding").To Lower
Case "base64"
xmlTextReader.R eadBase64(bytes , 0,
intNumBytes)
Case "binhex"
xmlTextReader.R eadBinHex(bytes , 0,
intNumBytes)
Case Else
MsgBox("Unknown image encoding
'" &
xmlTextReader.G etAttribute("En coding") & "'", _
MsgBoxStyle.Exc lamation, "Unknown
Encoding")
Exit Sub
End Select
' Allocate a MemoryStream and a BinaryWriter
attached
to it.
Dim memory_stream As New MemoryStream
Dim binary_writer As BinaryWriter = New
BinaryWriter
(memory_stream)
' Copy the bytes into the BinaryWriter and
MemoryStream.
binary_writer.W rite(bytes, 0, intNumBytes)
binary_writer.F lush()
' Load the picture from the memory stream and
save it
and show in the Picture box.
memory_stream.P osition = 0
objImageOrigina l = Image.FromStrea m(memory_stream )
'objImageOrigin al.Save(strTarg etFilePathAndNa me,
GetFormat(strIm ageExtension))
objImageOrigina l.Save(strTarge tFilePathAndNam e)
'Me.PB_sample.I mage =
Image.FromStrea m(memory_stream )
'PB_sample.Size Mode =
PictureBoxSizeM ode.CenterImage
binary_writer.C lose()
Exit Do
End If
Loop
xmlTextReader.C lose()
If blnValidXML Then
MsgBox(strImage Extension.ToUpp er & " file created
successfully ! ", MsgBoxStyle.Inf ormation, "Success !")
Else
MsgBox("Invalid XML file format ! ",
MsgBoxStyle.Cri tical,
"Error !")
End If
Else
MsgBox("An xml file containing the image must be
selected.",
MsgBoxStyle.OKO nly + MsgBoxStyle.Inf ormation, "File error")
Exit Sub
End If
End Sub
Private Function GetFormat(ByVal ImageType As String) As
System.Drawing. Imaging.ImageFo rmat
Select Case ImageType.ToUpp er
Case "JPG"
Return ImageFormat.Jpe g
Case "GIF"
Return ImageFormat.Gif
Case "PNG"
Return ImageFormat.Png
Case "BMP"
Return ImageFormat.Bmp
End Select
End Function
Also, I want (need) to do this for image formats that aren't recognised by
the image object (Wavelet Scalar Quantization .wsq format)... any idea how I
can achieve this?
Cheers again for you help...
"Peter Huang" <v-******@online.m icrosoft.com> wrote in message
news:7y******** ******@cpmsftng xa06.phx.gbl...
Hi Bob,
If you have any question on this issue please post here.
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.